栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初学spring事务(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初学spring事务(一)

applicationContext.xml,spring声明式事务配置



    
        
        
        
        
    

    
        
    

    
        
    

    

pom.xml,jar包配置

    
        UTF-8
        5.2.12.RELEASE
        11
        11
    

    
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
                
        
            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            ${spring.version}
            test
        
        
            mysql
            mysql-connector-java
            5.1.49
        
    
AccountService.java
package com.mazh.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SuppressWarnings("ALL")
@Service
public class AccountService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    
    @Transactional
    public void add1() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',1000)");
        int i = 1 / 0;
    }

    
    @Transactional
    public void add2() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',1000)");
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    @Transactional
    public void add3() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',1000)");
        try {
            int i = 1 / 0;
        } catch (ArithmeticException e) {
            throw new ArithmeticException(e.getMessage());
        }
    }

    
    @Transactional
    public void add4() throws FileNotFoundException {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',1000)");
        try {
            new FileInputStream(new File(""));//java.io.FileNotFoundException
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException();
        }
    }

    
    @Transactional
    public final void add5() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',1000)");
        int i = 1 / 0;
    }

    
    public void add6() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',2000)");
        add7();
    }

    @Transactional
    public void add7() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',3000)");
        int i = 1 / 0;
    }

    
    @Transactional
    public void add8() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',2000)");
        add9();
    }

    @Transactional
    public void add9() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',3000)");
        int i = 1 / 0;
    }

    
    @Transactional
    public void add10() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',2000)");
        add11();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void add11() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',3000)");
        int i = 1 / 0;
    }

    
    @Transactional
    public void add12() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',2000)");
        add13();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void add13() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',3000)");
        int i = 1 / 0;
    }

    
//    @Transactional(propagation = Propagation.SUPPORTS)
    @Transactional
    public void add14() {
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',2000)");
        add15();
//        int i = 1 / 0;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void add15() {
        int i = 1 / 0;
        jdbcTemplate.execute("insert into account(name,money) values ('hangman',3000)");
    }


}
AccountServiceTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountServiceTest {

    @Autowired
    private AccountService accountService;

    @Test
    public void test01() {
        accountService.add();
    }

}

account 表

CREATE TABLE `account` (
`id`  int(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`money`  int(11) NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=2
ROW_FORMAT=COMPACT
;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号