springboot声明式事务

开启声明式事务

1.在启动类上增加注解来

@EnableTransactionManagement

2.在需要使用事务的方法上增加注解

@Transactional

注解的参数说明

value/transactionManager 传递事务管理器 接受string类型数据
事务管理器一般在在我们使用数据源并自定义配置的注入的时候,需要注入  数据源、数据源池对象、和事务管理器,事务管理器就是从这里注入(自动配置的时候就是spring帮忙注入的了)
propation  事务传播的机制 接受Propagation类型数据  默认传播机制是Propagation.REQUIRED
​
​
/**
 * Enumeration that represents transaction propagation behaviors for use
 * with the {@link Transactional} annotation, corresponding to the
 * {@link TransactionDefinition} interface.
 *  该枚举类作为向Transactional 注解传递的参数,主要决定事务的传播机制
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.2
 */
public enum Propagation {
​
    /**
     *支持现有的事务,如果没有事务就创建新的事务
     * Support a current transaction, create a new one if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>This is the default setting of a transaction annotation. 是默认的事务传递机制
     */
    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),
​
    /**
     *支持当前已经存在的事务,如果没有事务就以非事务的方式来执行
     * Support a current transaction, execute non-transactionally if none exists.
     * 和EJB的同名事务属性相似
     * Analogous to EJB transaction attribute of the same name.
     *注意supports的事务传播机制和完全没有事务的执行是不同的,因为它定义了需要同步的事务范围
     *因此,相同的资源将共享整个需要同步的事务范围。注意,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值