抛出exception则事务回滚
注意是throws new Exception() / catch捕获到的exception是不会回滚的
@Transactional参数
例子:@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, value = “cargoMarketing”)
1.value
/**
* A qualifier value for the specified transaction.
* <p>May be used to determine the target transaction manager,
* matching the qualifier value (or the bean name) of a specific
* {@link org.springframework.transaction.PlatformTransactionManager}
* bean definition.
*/
String value() default "";
注意:当配置了多数据库源或多事务的时候,要标注value属性定义是回滚那个transactional manager
2.propagation
/**
* The transaction propagation type.
* Defaults to {@link Propagation#REQUIRED}.
* @see org.springframework.transaction.interceptor.TransactionAttribute#getPropagationBehavior()
*/
Propagation propagation() default Propagation.REQUIRED;
传播方式:网上有很多解释,这里就不多写了 默认是 Propagation.REQUIRED
其余的还有PROPAGATION_SUPPORTS,PROPAGATION_MANDATORY,PROPAGATION_REQUIRES_NEW
PROPAGATION_NOT_SUPPORTED,PROPAGATION_NEVER,PROPAGATION_NESTED
3.isolation
/**
* The transaction isolation level.
* Defaults to {@link Isolation#DEFAULT}.
* @see org.springframework.transaction.interceptor.TransactionAttribute#getIsolationLevel()
*/
Isolation isolation() default Isolation.DEFAULT;
这是sql隔离级别,默认是数据库的隔离级别。
其他的隔离级别就是普通的事务隔离级别,如ISOLATION_READ_UNCOMMITTED,ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ,ISOLATION_SERIALIZABLE
4.timeout
/**
* The timeout for this transaction.
* Defaults to the default timeout of the underlying transaction system.
* @see org.springframework.transaction.interceptor.TransactionAttribute#getTimeout()
*/
int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;
事务的超时时间:Transaction时间太长的话,将它停止掉。默认-1
5.readOnly
/**
* {@code true} if the transaction is read-only.
* Defaults to {@code false}.
* <p>This just serves as a hint for the actual transaction subsystem;
* it will <i>not necessarily</i> cause failure of write access attempts.
* A transaction manager which cannot interpret the read-only hint will
* <i>not</i> throw an exception when asked for a read-only transaction.
* @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly()
*/
boolean readOnly() default false;
默认是关闭的
readOnly=true表明所注解的方法或类只是读取数据。
readOnly=false表明所注解的方法或类是增加,删除,修改数据。
所有参数解读完毕。