手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Spring声明式事务管理

[日期:2015-09-12] 来源:Linux社区  作者:Linux [字体:大 中 小]

Spring的声明式事务管理,可以说是开发人员的福音,也是架构师们的法宝;通过这个神器我们可以有效的解决事务不一致、连接泄露等问题。

下面我们就介绍一下Spring声明式事务的配置:

1、创建事务管理器

 <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>

2、制定事务管理

<!-- 声明式事务配置 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
  <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="begin*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="end*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="do*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="modify*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
  <tx:method name="*" read-only="true" propagation="SUPPORTS" />
  </tx:attributes>
 </tx:advice>

3、创建切面

<aop:config>
  <aop:pointcut expression="execution(* com.test..service.*.*(..))" id="pointCut" />
  <aop:advisor  pointcut-ref="pointCut" advice-ref="txAdvice"/>
 </aop:config>

备注:

事务管理机制说明propagation 属性值

REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。
MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。
REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。
NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。
NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm

运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的详细介绍:请点这里
Spring 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-09/122940.htm

linux
相关资讯       Spring事务管理  Spring声明式事务 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款