Spring事务机制总结
一.概述
1. 编程式事务管理
编程式事务管理是侵入性事务管理,使用TransactionTemplate或者直接使用PlatformTransactionManager,对于编程式事务管理,Spring推荐使用TransactionTemplate。
2. 声明式事务管理
声明式事务管理建立在AOP之上,其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,执行完目标方法之后根据执行的情况提交或者回滚。
本篇文章主要介绍声明式事务。
spring支持声明式事务,即使用注解选择需要加事务的方法(使用@Transactional注解在方法名或者类名上表明方法或该类所有public方法需要事务)
注意点: 1.只有public方法才能被aop代理
2. 同一个类中非事务方法调用事务方法是没有事务的。
2.的解决办法:
// 1.在实现类里面注入自己,在无事务的方法中self.有事务的方法,事务生效
@Resource
private TdDictionaryService self;
//使用PostConstruct注解第一次初始化获取bean
private MyService myService;
@PostConstruct
public void init() {
myService = (MyService) ApplicationContextUtils.getBean("myService");
}
3.Transactional 注解介绍
//ElementType.METHOD, ElementType.TYPE 分别标明方法、类上可用
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {
/**
* 事务管理器,不同的数据源对应不同的管理器
*/
@AliasFor("transactionManager")
String value() default "";
@AliasFor("value")
String transactionManager() default "";
/**
* The transaction propagation type.
* 事务的传播机制,默认REQUIRED即新建事务
*/
Propagation propagation() default Propagation.REQUIRED;
/**
* The transaction isolation level.
* 隔离级别。mysql默认RR(可重复读)
*/
Isolation isolation() default Isolation.DEFAULT;
/**
* The timeout for this transaction.
* 事务超时时间
*/
int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;
/**
* {@code true} if the transaction is read-only.
* 只读事务
*/
boolean readOnly() default false;
/**
* Defines zero (0) or more exception {@link Class cla
* 指定异常回滚
*/
Class<? extends Throwable>[] rollbackFor() default {};
/**
* Defines zero (0) or more exception names (for exceptions
*/
String[] rollbackForClassName() default {};
/**
* 指定异常不回滚
*/
Class<? extends Throwable>[] noRollbackFor() default {};
/**
*
*/
String[] noRollbackForClassName() default {};
二、传播机制
public enum Propagation {
/**
* Support a current transaction, create a new one if none exists.
* 有事务使用当前事务,无事务创建事务
*/
REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),
/**
* Support a current transaction, execute non-transactionally if none exists.
* 有事务使用当前事务,无事务也不开起事务
*/
SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),
/**
* Support a current transaction, throw an exception if none exists.
* 使用当前事务,无事务报异常
*/
MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),
/**
* Create a new transaction, and suspend the current transaction if one exists.
* 创建新的事务,挡圈事务挂起
*/
REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),
/**
* Execute non-transactionally, suspend the current transaction if one exists.
* 无事务执行,当前事务挂起
*/
NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),
/**
* Execute non-transactionally, throw an exception if a transaction exists.
* 无事务执行,当前有事务报错,跟
*/
NEVER(TransactionDefinition.PROPAGATION_NEVER),
/**
* Execute within a nested transaction if a current transaction exists,
* behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
*/
NESTED(TransactionDefinition.PROPAGATION_NESTED);
private final int value;
Propagation(int value) { this.value = value; }
public int value() { return this.value; }
}
示例: 两个方法上的事务传播