spring源码分析---事务篇

时间:2023-03-08 16:02:02

  上一篇我介绍了spring事务的传播特性和隔离级别,以及事务定义的先关接口和类的关系。我们知晓了用TransactionTemplate(或者直接用底层P的latformTransactionManage---不推荐)进行事务管理方式,也就是Spring的编程式事务,这种方式使得业务代码和事务耦合度还是过高,spring推荐非入侵方式开发,所以它还有另外一种基于AOP的管理事务方式-------声明式事务,今天我们就来研究下这种方式

  声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。

  基于配置文件的如下

  

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ========================================分隔线========================================= --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 修改 保存 删除 -->
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <!-- 查询方法只读 -->
<tx:method name="get*" propagation="REQUIRED" read-only="false"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true"/>
<tx:method name="select*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.meiren.message.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>

未完待续。。。