spring中管理事务方式

spring中管理3种事务方式:

1、编码式:在代码中管理事务;将核心管理事务配置到spring中

            1)事务核心管理器

           <bean   name=“transactionManager” class=“xxx.DataSourceTransationManager”>

            <property  name="dataSource" ref = "dataSource"></property>

          </bean>   该对象依赖与DataSource连接池

             2)事务模版对象

            <bean  name = transationTemplate   class="xxx.TransationTemplate">

           <property  name="transactionManager" ref = "transactionManager"></property>

           </bean>

           3)写代码

           service方法中调用: TransationTemplate    tt

           tt.excute(new TransationCallBackWithoutResult(){

           //重写方法,方法中加入逻辑处理,excute方法在逻辑处理前和后已经打开/关闭事务了,不用自己加

             })

       

2、aop的xml配置式

1)配置事务通知

<tx:advice transation-manager="transactionManager"  id=“txAdvice”>

<tx:attributes>

<tx:method name="使用事务的方法" isolation=“隔离级别(default)”  propagation=“传播行为”  read-only=“是否只读”/>

</tx:attributes>

</tx:advice>

2)配置织入

<aop:config>

<aop:pointcut expression="excution(*  xxxx.*(..))" id = "pc"/>

<aop:advisor advice-ref=“txAdvice”  pointcut-ref="pc"/>

</aop:config>

2、注解配置

1)开启使用注解管理事务

<tx:annotation-driver />

2)使用注解

在方法上直接使用

@Transational(isolation=Isolation.REPEATABLE_READ ,propagation=Propagation.REQUIRED,readOnly=false)

当然也可以写在类上,作用于所有方法,若改就直接在方法上改属性即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值