SpringBoot事务简单操作及手动回滚

一、引入依赖

<!-- 核心启动器, 包括auto-configuration、logging and YAML -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- using Spring Data JDBC, JdbcTemplate或NamedParameterJdbcTemplate都是由spring jdbc提供的 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!-- 数据库操作需要的mysql 驱动包 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
</dependency>

<!-- testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

二、application.properties

spring.datasource.url=jdbc:mysql://192.168.178.5:12345/mydb?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

三、dao和service代码

1. dao

student接口

public interface StudentDao {
    void saveStudent();
}

student实现类

@Repository
public class StudentDaoImpl implements StudentDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void saveStudent(){
        String sql = "insert into student(name, s_class) values(?,?"; //这里会出现一个错误,少了右括号
        List<Object> sqlParamList = new ArrayList<Object>();
        sqlParamList.add("stn-"+Math.random());
        sqlParamList.add(20);
        jdbcTemplate.update(sql, sqlParamList.toArray(new Object[sqlParamList.size()]));
    }
}

user接口

public interface UserDao {
    void saveUser();
}

user实现类

@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void saveUser(){
        String sql = "insert into user(name, age) values(?,?)";
        List<Object> sqlParamList = new ArrayList<Object>();
        sqlParamList.add("sn-"+Math.random());
        sqlParamList.add(20);
        jdbcTemplate.update(sql, sqlParamList.toArray(new Object[sqlParamList.size()]));
    }
}

2. service

接口:

public interface OperatorService {
    void saveEntity() throws Exception;
}

接口实现类

@Service
public class OperatorServiceImpl implements OperatorService{

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private UserDao userDao;

    @Override
    public void saveEntity() throws Exception{
        userDao.saveUser(); 
        studentDao.saveStudent();
    }
}

当执行以上saveEntity()代码时,因StudentDaoImpl 插入语句的一个错误,会导致事务不一致,user表成功插入一条记录,student表没有。

为了使事务一致,在SpringBoot项目中,我们只需要在saveEntity()上添加@Transactional注解,,对@Transactional的注解可以查看 对注解@Transactional的解读 一节。

这里为了适应更多的异常,我们提升了事务捕获异常的范围:@Transactional(rollbackFor = Exception.class)

四、手动回滚事务

有时我们需要捕获一些错误信息,又需要进行事务回滚,这时我们就需要用到Spring提供的事务切面支持类TransactionAspectSupport。

@Transactional(rollbackFor = Exception.class)
@Override
public void saveEntity() throws Exception{
    try {
        userDao.saveUser();
        studentDao.saveStudent();
    }catch (Exception e){
        System.out.println("异常了=====" + e);
        //手动强制回滚事务,这里一定要第一时间处理
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
}

手动回滚事务一定要加上@Transactional,不然会报以下错误:

org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope

想想也是,不开启事务,何来手动回滚,所以@Transactional必不可少。

 

上一篇:Spring事务失效的原因


下一篇:Linux运行级别研究(转)