2015-05-17 11:42 606人阅读 评论(0) 收藏 举报
 分类:
Spring(12) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Spring事务管理的抽象,核心的三个接口:PlatformTransactionManager、TransactionDefinition和TransactionStatus。关系如下图所示:

TransactionDefinition:定义了Spring兼容的事务属性,包含:事务隔离级别、事务传播行为、超时时长、只读状态;

TransactionStatus:代表一个事务的具体运行状态。事务管理器通过该接口得到事务运行的状态信息,也可以通过它回滚事务等;

PlatformTransactionManager:事务管理器的顶级接口,它的常用实现类如下图所示:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSourceTransactionManager代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个Bean都有一个代理

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  11. <bean id="sessionFactory"
  12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  13. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  14. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  15. </bean>
  16. <!-- 定义事务管理器(声明式的事务) -->
  17. <bean id="transactionManager"
  18. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  19. <property name="sessionFactory" ref="sessionFactory" />
  20. </bean>
  21. <!-- 配置DAO -->
  22. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
  23. <property name="sessionFactory" ref="sessionFactory" />
  24. </bean>
  25. <bean id="userDao"
  26. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  27. <!-- 配置事务管理器 -->
  28. <property name="transactionManager" ref="transactionManager" />
  29. <property name="target" ref="userDaoTarget" />
  30. <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
  31. <!-- 配置事务属性 -->
  32. <property name="transactionAttributes">
  33. <props>
  34. <prop key="*">PROPAGATION_REQUIRED</prop>
  35. </props>
  36. </property>
  37. </bean>
  38. </beans>

第二种方式:所有Bean共享一个代理基类

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  11. <bean id="sessionFactory"
  12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  13. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  14. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  15. </bean>
  16. <!-- 定义事务管理器(声明式的事务) -->
  17. <bean id="transactionManager"
  18. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  19. <property name="sessionFactory" ref="sessionFactory" />
  20. </bean>
  21. <bean id="transactionBase"
  22. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  23. lazy-init="true" abstract="true">
  24. <!-- 配置事务管理器 -->
  25. <property name="transactionManager" ref="transactionManager" />
  26. <!-- 配置事务属性 -->
  27. <property name="transactionAttributes">
  28. <props>
  29. <prop key="*">PROPAGATION_REQUIRED</prop>
  30. </props>
  31. </property>
  32. </bean>
  33. <!-- 配置DAO -->
  34. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
  35. <property name="sessionFactory" ref="sessionFactory" />
  36. </bean>
  37. <bean id="userDao" parent="transactionBase" >
  38. <property name="target" ref="userDaoTarget" />
  39. </bean>
  40. </beans>

第三种方式:使用拦截器

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  11. <bean id="sessionFactory"
  12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  13. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  14. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  15. </bean>
  16. <!-- 定义事务管理器(声明式的事务) -->
  17. <bean id="transactionManager"
  18. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  19. <property name="sessionFactory" ref="sessionFactory" />
  20. </bean>
  21. <bean id="transactionInterceptor"
  22. class="org.springframework.transaction.interceptor.TransactionInterceptor">
  23. <property name="transactionManager" ref="transactionManager" />
  24. <!-- 配置事务属性 -->
  25. <property name="transactionAttributes">
  26. <props>
  27. <prop key="*">PROPAGATION_REQUIRED</prop>
  28. </props>
  29. </property>
  30. </bean>
  31. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  32. <property name="beanNames">
  33. <list>
  34. <value>*Dao</value>
  35. </list>
  36. </property>
  37. <property name="interceptorNames">
  38. <list>
  39. <value>transactionInterceptor</value>
  40. </list>
  41. </property>
  42. </bean>
  43. <!-- 配置DAO -->
  44. <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
  45. <property name="sessionFactory" ref="sessionFactory" />
  46. </bean>
  47. </beans>

第四种方式:使用tx标签配置的拦截器

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  13. <context:annotation-config />
  14. <context:component-scan base-package="com.bluesky" />
  15. <bean id="sessionFactory"
  16. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  17. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  18. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  19. </bean>
  20. <!-- 定义事务管理器(声明式的事务) -->
  21. <bean id="transactionManager"
  22. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  23. <property name="sessionFactory" ref="sessionFactory" />
  24. </bean>
  25. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  26. <tx:attributes>
  27. <tx:method name="*" propagation="REQUIRED" />
  28. </tx:attributes>
  29. </tx:advice>
  30. <aop:config>
  31. <aop:pointcut id="interceptorPointCuts"
  32. expression="execution(* com.bluesky.spring.dao.*.*(..))" />
  33. <aop:advisor advice-ref="txAdvice"
  34. pointcut-ref="interceptorPointCuts" />
  35. </aop:config>
  36. </beans>

第五种方式:全注解

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  13. <context:annotation-config />
  14. <context:component-scan base-package="com.bluesky" />
  15. <tx:annotation-driven transaction-manager="transactionManager"/>
  16. <bean id="sessionFactory"
  17. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  18. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  19. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  20. </bean>
  21. <!-- 定义事务管理器(声明式的事务) -->
  22. <bean id="transactionManager"
  23. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  24. <property name="sessionFactory" ref="sessionFactory" />
  25. </bean>
  26. </beans>

此时在DAO上需加上@Transactional注解,如下:

[java] view plain copy

  1. @Transactional
  2. @Component("userDao")
  3. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  4. public List<User> listUsers() {
  5. return this.getSession().createQuery("from User").list();
  6. }
  7. }

配置参考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

Spring事务SPI及配置介绍的更多相关文章

  1. Spring声明式事务管理与配置介绍

    转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...

  2. Spring事务解析1-使用介绍

    spring的事务控制让我们从复杂的事务处理中得到解脱,是我们再也不需要去处理获得连接,关闭连接,事务提交和回滚等操作,再也不需要在事务相关的方法中处理大量的try..catch...finally代 ...

  3. spring 事务隔离级别配置

    声明式的事务处理中,要配置一个切面,即一组方法,如 其中就用到了propagation,表示打算对这些方法怎么使用事务,是用还是不用,其中propagation有七种配置,REQUIRED.SUPPO ...

  4. Spring事务管理的配置

    spring-datasource-config.xml配置事务 <bean id="txManager" class="org.springframework.j ...

  5. Spring事务传播及数据库事务操作

    从Spring 事务配置说起 先看看Spring 事务的基础配置 <aop:aspectj-autoproxy proxy-target-class="true"/> ...

  6. Spring 事务管理原理探究

    此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...

  7. 【面试】Spring事务面试考点吐血整理(建议珍藏)

    Spring和事务的关系 关系型数据库.某些消息队列等产品或中间件称为事务性资源,因为它们本身支持事务,也能够处理事务. Spring很显然不是事务性资源,但是它可以管理事务性资源,所以Spring和 ...

  8. Spring事务回滚和异常类

    1.异常的一些基本知识 异常的架构 异常的继承结构:Throwable为基类,Error和Exception继承Throwable.Error和RuntimeException及其子类成为未检查异常( ...

  9. Spring 事务相关点整理

    Spring和事务的关系 关系型数据库.某些消息队列等产品或中间件称为事务性资源,因为它们本身支持事务,也能够处理事务. Spring很显然不是事务性资源,但是它可以管理事务性资源,所以Spring和 ...

随机推荐

  1. js享元模式

    享元模式(Flyweight),运用共享技术有效地支持大量细粒度的对象. 内部状态与外部状态 内部状态存储于对象内部. 内部状态可以被一些对象共享 内部状态独立于具体的场景,通常不会改变 外部状态取决 ...

  2. Mac下git安装(使用Xcode)

    (不使用Xcode的出门右转百度其他方法:比如直接安装git软件包.) 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode ...

  3. uva 10453 dp/LCS变形

    https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...

  4. lr中检查点的使用web_find()和web_reg_find()的区别

    web_find()和web_reg_find()的区别:1. 这两个函数函数类型不同,web_find()是普通函数,web_reg_find()是注册函数;2. VU run time设置中的 “ ...

  5. BEC translation exercise 1

    U.S. oil drillers have made major efficiency improvements with a speed that has repeatedly surprised ...

  6. _Meta 部分用法

    model.UserInfo._meta.app_label #获取该类所在app的app名称 model.UserInfo._meta.model_name #获取该类对应表名(字符串类型) mod ...

  7. vector map迭代器失效解决方案

    vector : iter = container.erase(iter);    //erase的返回值是删除元素下一个元素的迭代器 vector<int>::iterator it = ...

  8. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)

    最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...

  9. storm 学习教程

    转自:http://blog.csdn.net/hrn1216/article/details/51538962 翻译太累了,再也不想去翻译了,真的太累了: 在这个教程中, 你将学到如何创建一个Sto ...

  10. OpenJudge9278:旅行

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  131072kB 描述 转眼毕业了,曾经朝夕相处的同学们不得不都各奔东西,大家都去了不同的城市开始新的生活.在各自城 ...