spring–事务配置的两种方式 http://my.oschina.net/u/2312022/blog/743118
方式一:使用注解
1 2 3 4 5 6 |
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使事务注解生效 --> <tx:annotation-driven transaction-manager="transactionManager"/> |
备注:这种方式是,在需要加事务的方法上面使用@Transactional()来指定事务。
方法二:使用xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 定义查询方法只读 --> <tx:method name="query*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <!-- 主数据库操作 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <!-- 其它方法使用默认事务策略 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <!-- 定义切面,所有的service的所有的方法 --> <aop:pointcut expression="execution(* com.test.spring.service.*.*(..))" id="txPointcut"/> <!-- 添加事务到切面上 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> |
认真拜读中……
完全免费,十万站长,互刷排名
http://www.loveuv.com/l/NDGBXQ
注册即送5000流量。