Spring的声明式事务
声明式事务是指利用AOP自动提交、回滚数据库事务
声明式事务规则:进入方法打开事务,执行成功提交,抛出运行时异常(RuntimeException)则回滚
@Transactional
是声明式事务的注解,当@Transactional
注解标于类前时, 表示类中所有方法都进行事务处理
@Transactional注解的参数
1、propagation 用于设置事务的传播行为
1 2 3 4 5 6 7 8 9 10 11 12
| @Transactional(propagation = Propagation.REQUIRED)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Transactional(propagation = Propagation.MANDATORY)
@Transactional(propagation = Propagation.NEVER)
@Transactional(propagation = Propagation.SUPPORTS)
|
2、rollbackFor 用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚
1 2 3 4 5 6
| @Transactional(rollbackFor = Exception.class)
@Transactional(rollbackFor = RuntimeException.class)
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
|