@Transactional事务开发Ⅱ

2 篇文章 0 订阅

抛出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表明所注解的方法或类是增加,删除,修改数据。

所有参数解读完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值