原文:http://www.iteye.com/topic/1123347

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. }

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

以上内容,是从网络找到的资料总结而来,仅供参考!

Spring笔记——配置Hibernate框架事务的更多相关文章

  1. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  2. Spring中配置Hibernate事务管理

    <!-- transationManager --> <bean id="transactionManager" class="org.springfr ...

  3. Spring笔记⑤--整合hibernate代码测试

    String整合hibernate代码测试 在上节生成的表中插入数据:   注意:使用myeclipse2014生成的整合项目可能存在问题需要我们自己导入.   第一步 我们写dao接口 packag ...

  4. Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析

    一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...

  5. 笔记:Hibernate 框架配置说明

    下载 Hibernate ,打开地址 www.hibernate.org ,点击 Hibernate ORM -> Downloads 下载 4.3.11 版本,要使用Hibernate 需要把 ...

  6. MyEclipse配置Hibernate框架(基础篇)

    一.创建java project项目 二.项目右键Configure Facets -- Install Hibernate Facet 三.项目添加对应数据库的jar包 四.编写实体类 packag ...

  7. 深入浅出Struts2+Spring+Hibernate框架

    一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...

  8. 整合spring和hibernate框架

    一)整合spring和hibernate框架整合要点:(1)数据源配置在Spring的配置文件中,供Spring和Hibernate框架共同使用:(2)不再需要hibernate.hbm.xml配置文 ...

  9. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

随机推荐

  1. 【每日scrum】NO.2

    1.今天找到了铁大电子地图. 2.需求分析未完成,进度有点慢.

  2. 搭建SpringMVC+MyBatis开发框架五

    建立web结构 1.在webapp目录下新建css.img和js文件夹,删除默认的index.jsp文件:  2.在WEB-INF文件夹下建立一个page文件夹,然后在page下新建一个index. ...

  3. JIRA安装过程中链接mysql的问题!

    测试下我使用的是mysql7.5的版本,JIRA是6.3.6!这是版本引起的问题! 服务器上原生的mysql驱动jar包:Mysql-connector-java-5.1.18-bin 可参考:htt ...

  4. 委托、匿名委托和lambda表达式

    1.委托 在.NET中,委托有点类似于C/C++中的函数指针,但与指针不同的是,委托是一种安全的类型,那么我们就以实现两个数的差为例,先声明一个成员方法: public int CompareTwoV ...

  5. Grails 1.2参考文档速读(10):Controller

    转载:http://keyvalue.blog.51cto.com/1475446/303260       从本篇起,我们将开始进入Grails的Web层,首先让我们从Controller说起. G ...

  6. chm文件右边部分查看不了

    右键属性-> 下面好像有句话是说该文件来自网络为保护您的计算机什么什么的,, 然后你解除锁定就能用了 版权声明:本文为博主原创文章,未经博主允许不得转载.

  7. jquery的ajax向后台提交时,乱码的解决方案

    1. 可以给每个参数加上encodeURIComponent(),然后在后台获得参数后用URLDecoder.decode(string, 'utf-8')解码. 2. 后台不用解码. $.ajax( ...

  8. Confluence Wiki Markup & Markdown

    Markup : 默认有支持 Markdown : 需先安装插件,插件下载地址: Confluence markdown : https://marketplace.atlassian.com/plu ...

  9. jQuery打印插件PrintArea实现

    实现javascript打印功能,打印整个页面就很简单,但如果指定打印某一个区域就有点难点,这里有一个jQuery插件PrintArea可实现打印页面某区域功能. 使用说明需要使用jQuery库文件和 ...

  10. 机器学习(一):梯度下降、神经网络、BP神经网络

    这几天围绕论文A Neural Probability Language Model 看了一些周边资料,如神经网络.梯度下降算法,然后顺便又延伸温习了一下线性代数.概率论以及求导.总的来说,学到不少知 ...