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

首先我创建了两个类,一个接口一个实现:

  1. package com.dao;
  2. public interface UserDao {
  3. public void getUser();
  4. }

实现:

  1. package com.dao.impl;
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  3. import com.dao.UserDao;
  4. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  5. public void getUser(){
  6. }
  7. }

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

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <!-- 数据源 -->
  10. <bean id="dataSource"
  11. class="org.apache.commons.dbcp.BasicDataSource"
  12. destroy-method="close">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  14. <property name="url"
  15. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  16. <property name="username" value="root" />
  17. <property name="password" value="root" />
  18. <!-- 连接池启动时的初始值 -->
  19. <property name="initialSize" value="10" />
  20. <!-- 连接池的最大值 -->
  21. <property name="maxActive" value="10" />
  22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  23. <property name="maxIdle" value="20" />
  24. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  25. <property name="minIdle" value="10" />
  26. <property name="defaultAutoCommit" value="true" />
  27. </bean>
  28. <!-- 会话工厂 -->
  29. <bean id="sessionFactory"
  30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  31. <property name="dataSource" ref="dataSource" />
  32. <property name="mappingLocations">
  33. <list>
  34. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
  35. </list>
  36. </property>
  37. <property name="hibernateProperties">
  38. <props>
  39. <prop key="hibernate.dialect">
  40. org.hibernate.dialect.MySQL5Dialect
  41. </prop>
  42. <prop key="hibernate.show_sql">true</prop>
  43. <prop key="hibernate.format_sql">true</prop>
  44. </props>
  45. </property>
  46. </bean>
  47. <!-- 定义事务管理器 -->
  48. <bean id="transactionManager"
  49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  50. <property name="sessionFactory" ref="sessionFactory" />
  51. </bean>
  52. <!-- 配置服务层 -->
  53. <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
  54. <property name="sessionFactory" ref="sessionFactory" />
  55. </bean>
  56. <bean id="userDao"
  57. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  58. <!-- 配置事务管理器 -->
  59. <property name="transactionManager" ref="transactionManager" />
  60. <property name="target" ref="userDaoAgency" />
  61. <property name="proxyInterfaces" value="com.dao.UserDao" />
  62. <!-- 配置事务属性 -->
  63. <property name="transactionAttributes">
  64. <props>
  65. <prop key="*">PROPAGATION_REQUIRED</prop>
  66. </props>
  67. </property>
  68. </bean>
  69. </beans>

第二种:所有Bean共享一个代理:

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <!-- 数据源 -->
  10. <bean id="dataSource"
  11. class="org.apache.commons.dbcp.BasicDataSource"
  12. destroy-method="close">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  14. <property name="url"
  15. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  16. <property name="username" value="root" />
  17. <property name="password" value="root" />
  18. <!-- 连接池启动时的初始值 -->
  19. <property name="initialSize" value="10" />
  20. <!-- 连接池的最大值 -->
  21. <property name="maxActive" value="10" />
  22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  23. <property name="maxIdle" value="20" />
  24. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  25. <property name="minIdle" value="10" />
  26. <property name="defaultAutoCommit" value="true" />
  27. </bean>
  28. <!-- 会话工厂 -->
  29. <bean id="sessionFactory"
  30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  31. <property name="dataSource" ref="dataSource" />
  32. <property name="mappingLocations">
  33. <list>
  34. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
  35. </list>
  36. </property>
  37. <property name="hibernateProperties">
  38. <props>
  39. <prop key="hibernate.dialect">
  40. org.hibernate.dialect.MySQL5Dialect
  41. </prop>
  42. <prop key="hibernate.show_sql">true</prop>
  43. <prop key="hibernate.format_sql">true</prop>
  44. </props>
  45. </property>
  46. </bean>
  47. <!-- 定义事务管理器 -->
  48. <bean id="transactionManager"
  49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  50. <property name="sessionFactory" ref="sessionFactory" />
  51. </bean>
  52. <!-- 定义事务 -->
  53. <bean id="base"
  54. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  55. lazy-init="true" abstract="true">
  56. <!-- 配置事务管理器 -->
  57. <property name="transactionManager" ref="transactionManager" />
  58. <!-- 配置事务属性 -->
  59. <property name="transactionAttributes">
  60. <props>
  61. <prop key="*">PROPAGATION_REQUIRED</prop>
  62. </props>
  63. </property>
  64. </bean>
  65. <!-- 配置服务层 -->
  66. <bean id="userDao"
  67. class="com.dao.impl.UserDaoImpl">
  68. <property name="sessionFactory" ref="sessionFactory" />
  69. </bean>
  70. <!-- 代理对象 -->
  71. <bean id="userDaoAgency" parent="base">
  72. <property name="target" ref="userDao" />
  73. </bean>
  74. </beans>

第三种:拦截器:

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <!-- 数据源 -->
  10. <bean id="dataSource"
  11. class="org.apache.commons.dbcp.BasicDataSource"
  12. destroy-method="close">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  14. <property name="url"
  15. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  16. <property name="username" value="root" />
  17. <property name="password" value="root" />
  18. <!-- 连接池启动时的初始值 -->
  19. <property name="initialSize" value="10" />
  20. <!-- 连接池的最大值 -->
  21. <property name="maxActive" value="10" />
  22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  23. <property name="maxIdle" value="20" />
  24. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  25. <property name="minIdle" value="10" />
  26. <property name="defaultAutoCommit" value="true" />
  27. </bean>
  28. <!-- 会话工厂 -->
  29. <bean id="sessionFactory"
  30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  31. <property name="dataSource" ref="dataSource" />
  32. <property name="mappingLocations">
  33. <list>
  34. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
  35. </list>
  36. </property>
  37. <property name="hibernateProperties">
  38. <props>
  39. <prop key="hibernate.dialect">
  40. org.hibernate.dialect.MySQL5Dialect
  41. </prop>
  42. <prop key="hibernate.show_sql">true</prop>
  43. <prop key="hibernate.format_sql">true</prop>
  44. </props>
  45. </property>
  46. </bean>
  47. <!-- 定义事务管理器(声明式的事务) -->
  48. <bean id="transactionManager"
  49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  50. <property name="sessionFactory" ref="sessionFactory" />
  51. </bean>
  52. <!-- 定义事务 -->
  53. <bean id="transactionInterceptor"
  54. class="org.springframework.transaction.interceptor.TransactionInterceptor">
  55. <property name="transactionManager" ref="transactionManager" />
  56. <!-- 配置事务属性 -->
  57. <property name="transactionAttributes">
  58. <props>
  59. <prop key="*">PROPAGATION_REQUIRED</prop>
  60. </props>
  61. </property>
  62. </bean>
  63. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  64. <property name="beanNames">
  65. <list>
  66. <value>*DaoImpl</value>
  67. </list>
  68. </property>
  69. <property name="interceptorNames">
  70. <list>
  71. <value>transactionInterceptor</value>
  72. </list>
  73. </property>
  74. </bean>
  75. <!-- 配置服务层 -->
  76. <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
  77. <property name="sessionFactory" ref="sessionFactory" />
  78. </bean>
  79. </beans>

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

  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. <!-- 数据源 -->
  14. <bean id="dataSource"
  15. class="org.apache.commons.dbcp.BasicDataSource"
  16. destroy-method="close">
  17. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  18. <property name="url"
  19. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  20. <property name="username" value="root" />
  21. <property name="password" value="root" />
  22. <!-- 连接池启动时的初始值 -->
  23. <property name="initialSize" value="10" />
  24. <!-- 连接池的最大值 -->
  25. <property name="maxActive" value="10" />
  26. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  27. <property name="maxIdle" value="20" />
  28. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  29. <property name="minIdle" value="10" />
  30. <property name="defaultAutoCommit" value="true" />
  31. </bean>
  32. <!-- 会话工厂 -->
  33. <bean id="sessionFactory"
  34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  35. <property name="dataSource" ref="dataSource" />
  36. <property name="mappingLocations">
  37. <list>
  38. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
  39. </list>
  40. </property>
  41. <property name="hibernateProperties">
  42. <props>
  43. <prop key="hibernate.dialect">
  44. org.hibernate.dialect.MySQL5Dialect
  45. </prop>
  46. <prop key="hibernate.show_sql">true</prop>
  47. <prop key="hibernate.format_sql">true</prop>
  48. </props>
  49. </property>
  50. </bean>
  51. <context:annotation-config />
  52. <context:component-scan base-package="com.dao" />
  53. <!-- 定义事务管理器 -->
  54. <bean id="transactionManager"
  55. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  56. <property name="sessionFactory" ref="sessionFactory" />
  57. </bean>
  58. <!-- 定义事务 -->
  59. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  60. <tx:attributes>
  61. <tx:method name="*" propagation="REQUIRED" />
  62. </tx:attributes>
  63. </tx:advice>
  64. <!-- 定义切面 -->
  65. <aop:config>
  66. <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
  67. <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
  68. </aop:config>
  69. </beans>

第五种:注解:

  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. <!-- 数据源 -->
  14. <bean id="dataSource"
  15. class="org.apache.commons.dbcp.BasicDataSource"
  16. destroy-method="close">
  17. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  18. <property name="url"
  19. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
  20. <property name="username" value="root" />
  21. <property name="password" value="root" />
  22. <!-- 连接池启动时的初始值 -->
  23. <property name="initialSize" value="10" />
  24. <!-- 连接池的最大值 -->
  25. <property name="maxActive" value="10" />
  26. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  27. <property name="maxIdle" value="20" />
  28. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  29. <property name="minIdle" value="10" />
  30. <property name="defaultAutoCommit" value="true" />
  31. </bean>
  32. <!-- 会话工厂 -->
  33. <bean id="sessionFactory"
  34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  35. <property name="dataSource" ref="dataSource" />
  36. <property name="mappingLocations">
  37. <list>
  38. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
  39. </list>
  40. </property>
  41. <property name="hibernateProperties">
  42. <props>
  43. <prop key="hibernate.dialect">
  44. org.hibernate.dialect.MySQL5Dialect
  45. </prop>
  46. <prop key="hibernate.show_sql">true</prop>
  47. <prop key="hibernate.format_sql">true</prop>
  48. </props>
  49. </property>
  50. </bean>
  51. <context:annotation-config />
  52. <!-- 使用注解的包路径 -->
  53. <context:component-scan base-package="com.dao" />
  54. <!-- 支持  @Transactional 标记 -->
  55. <tx:annotation-driven transaction-manager="transactionManager"/>
  56. <!-- 定义事务管理器 -->
  57. <bean id="transactionManager"
  58. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  59. <property name="sessionFactory" ref="sessionFactory" />
  60. </bean>
  61. </beans>

如果使用了注解,那么实现类应该这样写:

  1. package com.dao.impl;
  2. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import com.dao.UserDao;
  6. @Transactional
  7. @Component("userDaoAgency")
  8. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  9. /**
  10. * 为方法增加事务处理特性
  11. */
  12. @Transactional(readOnly=true)
  13. public void getUser(){
  14. }
  15. }

这样每个方法都能自己定义自己的事务处理!

转自:http://www.iteye.com/topic/1123347

Spring管理 hibernate 事务配置的五种方式的更多相关文章

  1. Spring事务配置的五种方式(转发)

    Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...

  2. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

    转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...

  3. Spring事务配置的五种方式(转载)

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

  4. Spring事务配置的五种方式

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

  5. Spring事务配置的五种方式 -- 越往后需要Spring版本越高

    第五种 基本零配置  个人感觉第四种也可以 Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式, ...

  6. Spring事务配置的五种方式(转)

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

  7. Spring事务配置的五种方式 巨全!不看后悔,一看必懂!

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

  8. (转)Spring事务配置的五种方式

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

  9. SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式

    这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...

随机推荐

  1. LoadRunner场景参数文件部分参数说明(我在某银行的整理)

    由于场景中脚本繁多,同时设置60个脚本的“运行时设置”会提示个数限制信息,这时可以考虑通过场景的参数文件配置来批量解决这些事情,主要是提高工作效率. 选中自己保存的controller场景,鼠标右键点 ...

  2. C# RSA PEM解密字符

    1.第一步先用openssl将pem的key转换为der的key //bin>openssl.exe rsa -in rsakeydec.pem -outform der -out pri.de ...

  3. 自动化测试管理平台ATMS(V2.0.2_8.19)下载

    自动化测试管理平台ATMS(V2.0.2_8.19)下载 http://www.automationqa.com/forum.php?mod=viewthread&tid=2791

  4. 程序员之路:以Android证道

    大道三千,何以证道? 最近有私信.邮件给我咨询一些职业生涯规划的同学,我在这里以过来人的身份给大家一些建议. 任何行业,任何职位,无论高低,无论大小,都可以分为广博.精深两个方向. 精深自然指的是在某 ...

  5. MySql批量更新方法

    准备数据 表 user(用户).dept(部门) 1:更新符合单个条件的某个字段的一条数据 update user u set u.name = '测试' where u.id = "&qu ...

  6. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  7. Linux查看系统信息命令汇总

    # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算 ...

  8. SQL SERVER UNION和UNION ALL

    union与union allunion 缺省在合并结果集后消除重复项,union all 指定在合并结果集后保留重复项, 打个比喻吧 比如A表的数据是 A{ 1,4,5,9}       B{2,3 ...

  9. Using HiveServer2 - Authentication

    To configure Hive for use with HiveServer2, include the following configuration properties in the .. ...

  10. apple Swift教程大全,希望对你有帮助!

    1)apple Swift编程入门文档- http://gashero.iteye.com/blog/2075324 一位大神写的关于Swift的一些介绍和简单的使用,里面介绍了Swift和其他语言的 ...