首页 > 编程语言 > 详细

Spring中如何配置Hibernate事务

时间:2017-02-08 10:32:51      阅读:244      评论:0      收藏:0      [点我收藏+]

  为了保证数据的一致性,在编程的时候往往需要引入事务这个概念。事务有4个特性:原子性、一致性、隔离性、持久性。

         事务的种类有两种:编程式事务和声明式事务。编程式事务就是将事务处理放在程序中,而声明式事务则是通过配置文件或者注解进行操作。

         在Spring中有声明式事务的概念,通过和Hibernate类似框架的集成,可以很好的完成声明式事务。

         其实,不论在Spring中有几种配置Hibernate事务的方法,都逃不出一下几条:

         1.配置SessionFactory

         2.配置事务容器

         3.配置事务规则

         4.配置事务入口

         后面一共为大家提供4种配置Hibernate事务的方法。

         首先说下配置SessionFactory,配置SessionFactory有两种方式,一种是通过配置hibernate.cfg.xml文件的位置来配置SessionFactory,另一种就是在Spring配置文件中,手动配置数据源。

         下面是两种配置SessionFactory的方式(第二种配置需要额外引入两个包:commons-dbcp、commons-pool)

[html] view plain copy

  1. <!-- 1、第一种配置SessionFactory的方式 -->  
  2. <bean id="sessionFactory"  
  3.     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  4.     <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
  5. </bean>  
  6.   
  7. <!-- 2、第二种配置SessionFactory的方式 -->  
  8. <!-- 2.1配置数据源 -->  
  9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  10. 10.     destroy-method="close">  
  11. 11.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  12. 12.     <property name="url" value="jdbc:mysql://localhost:3306/hibernate_cache"></property>  
  13. 13.     <property name="username" value="root"></property>  
  14. 14.     <property name="password" value="admin"></property>  

15. </bean>  

16. <!-- 2.2、配置SessionFactory -->  

17. <bean id="sessionFactory"  

  1. 18.     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2. 19.     <property name="dataSource" ref="dataSource"></property>  
  3. 20.     <property name="hibernateProperties">  
  4. 21.         <props>  
  5. 22.             <prop key="hibernate.hbm2ddl.auto">update</prop>  
  6. 23.         </props>  
  7. 24.     </property>  
  8. 25.     <property name="mappingLocations">  
  9. 26.         <list>  
  10. 27.             <value>classpath:实体对应xml的路径</value>  
  11. 28.         </list>  
  12. 29.     </property>  

30. </bean>  


         至此Hibernate就成功的将SessionFactory交给了Spring来管理。现在再来看Spring是怎样管理Hibernate事务的吧。

         第一种方式,利用tx标签配置事务。

[html] view plain copy

  1. <!-- 配置事务容器 -->  
  2. <bean id="transactionManager"  
  3.     class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  4.     <property name="sessionFactory" ref="sessionFactory" />  
  5. </bean>  
  6. <!-- 定义事务规则 -->  
  7. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  8.     <tx:attributes>  
  9.         <tx:method name="add*" propagation="REQUIRED" />  
  10. 10.         <tx:method name="modify*" propagation="REQUIRED" />  
  11. 11.         <tx:method name="del*" propagation="REQUIRED" />  
  12. 12.         <tx:method name="*" propagation="REQUIRED" read-only="true" />  
  13. 13.     </tx:attributes>  

14. </tx:advice>  

15. <!-- 定义事务入口 -->  

16. <aop:config>  

  1. 17.     <aop:pointcut id="allDaoMethod" expression="execution(* com.jianxin.dao.*.*(..))" />  
  2. 18.     <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod" />  

19. </aop:config>  


         第二种,用代理进行配置

[html] view plain copy

  1. <!-- 配置事务容器 -->  
  2. <bean id="transactionManager"  
  3.     class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  4.     <property name="sessionFactory" ref="sessionFactory" />  
  5. </bean>  
  6. <!-- 定义事务规则 -->  
  7. <bean id="transactionProxy"  
  8.     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  9.     abstract="true">  
  10. 10.     <property name="transactionManager" ref="transactionManager" />  
  11. 11.     <property name="transactionAttributes">  
  12. 12.         <props>  
  13. 13.             <!-- ,回滚为-,不回滚为+ -->  
  14. 14.             <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
  15. 15.             <prop key="modify*">PROPAGATION_REQUIRED,+MyException</prop>  
  16. 16.             <prop key="del*">PROPAGATION_REQUIRED</prop>  
  17. 17.             <prop key="*">READONLY</prop>  
  18. 18.         </props>  
  19. 19.     </property>  

20. </bean>  

21. <!-- 定义事务入口 -->  

22. <bean id="userDaoProxy" parent="transactionProxy">  

  1. 23.     <property name="target" ref="userDao"></property>  

24. </bean>  


         第三种,利用拦截器

[html] view plain copy

  1. <!-- 配置事务容器 -->  
  2. <bean id="transactionManager"  
  3.     class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  4.     <property name="sessionFactory" ref="sessionFactory" />  
  5. </bean>  
  6. <!-- 定义事务规则 -->  
  7. <bean id="transactionInterceptor"  
  8.     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  9.     <property name="transactionManager" ref="transactionManager" />  
  10. 10.     <property name="transactionAttributes">  
  11. 11.         <props>  
  12. 12.             <!-- 回滚为-,不回滚为+ -->  
  13. 13.             <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
  14. 14.             <prop key="modify*">PROPAGATION_REQUIRED,+MyException</prop>  
  15. 15.             <prop key="del*">PROPAGATION_REQUIRED</prop>  
  16. 16.             <prop key="*">READONLY</prop>  
  17. 17.         </props>  
  18. 18.     </property>  

19. </bean>  

20. <!-- 定义事务入口 -->  

21. <bean id="proxyFactory"  

  1. 22.     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  2. 23.     <property name="interceptorNames">  
  3. 24.         <list>  
  4. 25.             <value>transactionInterceptor</value>  
  5. 26.         </list>  
  6. 27.     </property>  
  7. 28.     <property name="beanNames">  
  8. 29.         <list>  
  9. 30.             <value>*Dao</value>  
  10. 31.         </list>  
  11. 32.     </property>  

33. </bean>  


         第四种,利用注解。

         首先,在配置文件中写入下面语句,打开注解功能

[html] view plain copy

  1. <!-- 开户事务注解功能 -->  
  2. <tx:annotation-driven transaction-manager="transactionManager" />  


         然后用@Transactional对类或者方法进行标记,如果标记到类上,那么次类中所有方法都进行事务回滚处理,在类中定义Transactional的时候,它有propagation、rollbackFor、noRollbackFor等属性,此属性是用来定义事务规则,而定义到哪这个就是事务入口。

         纵观以上四种在Spring中配置Hibernate事务的方法,其核心都是一样的,不同的只是实现的方式而已。所以看到这,这篇博文中你只需要记住4句话,就可以轻松理解在Spring中配置Hibernate事务的核心:

         1.配置SessionFactory

         2.配置事务容器

         3.配置事务规则

         4.配置事务入口

Spring中如何配置Hibernate事务

原文:http://www.cnblogs.com/lihongfeng0121/p/6376967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!