事务配置(采用配置文件的形式)
(1). 配置Spring.xml。
文件添加一下配置信息:
<!-- 事务配置 --> <import resource="spring-service-tx.xml"/>
(2).添加配置spring-service-tx.xml文件。
文件配置信息:
<!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- tx配置 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 指定事务作用的方法及声明事务的传播属性 并且指定对应方法抛出指定异常时事务回滚 保证数据一致性 --> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="select*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.lcl.*.service.*.*(..))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config>