Spring事务的五种实现方式

前段时间对spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

总结如下:

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

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

具体如下图:

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

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

Xml代码  

  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共享一个代理基类

Xml代码  

  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>

第三种方式:使用拦截器

Xml代码  

  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标签配置的拦截器

Xml代码  

  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>

第五种方式:全注解

Xml代码  

  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>

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

Java代码  

  1. package com.bluesky.spring.Service;
  2. import java.util.List;
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  6. import org.springframework.stereotype.Component;
  7. import com.bluesky.spring.domain.User;
  8. @Transactional
  9. @Component("userService")
  10. public class UserServiceImpl extends HibernateDaoSupport implements UserService
    {
  11. public List<User> listUsers() {
  12. return   ………………………………;
  13. }
  14. }
时间: 2024-08-04 23:35:57

Spring事务的五种实现方式的相关文章

Spring事务Transaction配置的五种注入方式详解

Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ??? 总结如下: ??? Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. ???DataSou

实现spring事务的四种方式

用一个银行账号转钱的案例来说明spring事务的实现.在转钱过程中,一个账号钱增加,另一个减少,那么当有异常产生时,就会出现钱转丢了的现象一个减少了,而另一个没有增加,这个时候就需要把这两个行为绑定到一起,要么同时发生,要么都不发生这就用到了事务,事务就是指在逻辑上的一组操作,这组操作要么全部成功,要么全部失败 实现spring事务的四种方式分别为:(1)编程式事务管理:需要手动编写代码,在实际开发中很少使用(2)声明式事务管理:(2.1)基于TransactionProxyFactoryBea

CacheConcurrencyStrategy五种缓存方式

CacheConcurrencyStrategy有五种缓存方式:  CacheConcurrencyStrategy.NONE,不适用,默认  CacheConcurrencyStrategy.READ_ONLY ,只读模式,在此模式下,如果对数据进行更新操作,会有异常:  CacheConcurrencyStrategy.READ_WRITE ,读写模式在更新缓存的时候会把缓存里面的数据换成一个锁,其它事务如果去取相应的缓存数据,发现被锁了,直接就去数据库查询:  CacheConcurren

Spring-AOP的五种通知方式

AOP的五种通知方式: 前置通知:在我们执行目标方法之前运行(@Before) 后置通知:在我们目标方法运行结束之后,不管有没有异常(@After) 返回通知:在我们的目标方法正常返回值后运行(@AfterReturning) 异常通知:在我们的目标方法出现异常后运行(@AfterThrowing) 环绕通知:目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,joinPoint.procced()就是执行目标方法的代码 .环绕通知可以控制返回对象(@Around) 一.导jar包 co

Hibernate的检索及五种数据检索方式

检索数据时的 2 个问题 1). 不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象, 而程序实际上仅仅需要访问 Customer 对象, 那么这些关联的 Order 对象就白 白浪费了许多内存. 2). 更高的查询效率:发送尽可能少的 SQL 语句 类级别的检索策略 2. 类级别的检索策略:(主要掌握 load 方法和 get 方法的区别!) 1). 类级别可选的检索策略包括立即检索和延迟检索, 默认为延迟检索 ①. 立即检

Android五种布局方式——LinearLayout、RelativeLayout、TableLayout....(四)

Android五种布局方式--LinearLayout.RelativeLayout .TableLayout.... Android使用XML声明界面布局 将程序的表现层和控制层分离 修改用户界面时,无需更改程序的源代码 可视化工具设计用户界面 Android五种布局方式 LinearLayout线性布局 AbsoluteLayout坐标布局 RelativeLayout相对布局 FrameLayout帧布局 TableLayout表格布局 GridLayout 1.LinearLayout线

LFU五种实现方式,从简单到复杂

前言 最近刷力扣题,对于我这种 0 基础来说,真的是脑壳疼啊.这个月我估计都是中等和困难题,没有简单题了. 幸好,力扣上有各种大牛给写题解.看着他们行云流水的代码,真的是羡慕不已.让我印象最深刻的就是人称 "甜姨" 的知心姐姐,还有名叫威哥的大哥.几乎每天他们的题解我都是必看的. 甜姨的题解,虽然姿势很帅,但是对于我这种新手来说,感觉不是太友好,因为思路写的太少,不是很详细.所以,每次我看不明白的时候,都得反复看好几遍,才能想明白她代码中的思路. 上个周末的一道题是,让实现一个 LFU

(转)spring事务管理几种方式

转自:http://blog.csdn.net/jeamking/article/details/43982435 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是

[转] spring事务管理几种方式

前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分只是会根据数据访问方式有所变化,