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)
当然也可以写在类上,作用于所有方法,若改就直接在方法上改属性即可