您的位置:首页 > 编程语言 > Java开发

Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)

2014-08-20 11:52 861 查看
原文链接:http://blog.csdn.net/dyllove98/article/details/8586283

采用声明式事务 

1、声明式事务配置 

* 配置SessionFactory 

* 配置事务管理器 

* 事务的传播特性 

* 那些类那些方法使用事务

思考:要把事物加到那些层,把事物加在DAO这一层呢,还是加在业务逻辑处理service这一层?为什么?

在service层中配置事物管理。

假设在DAO层使用事物: UserDAO中有一个save()方法,用来添加新用户,LogDAO类中也有一个Sava方法,用来在新用户保存之后,添加日子记录。如果你把事务分别加在DAO层的UserDAOImpl和LogDAOImpl的save方法上,LogDAO中的save方法在一个事务里面,UserDAO的save()方法也在一个事务里面。LogDAOImpl中的save方法出现了异常,事务发生了回滚,但是UserDAOImpl中的save方法正常,事务没有发生rollback。即在新用户保存的时候,就没有日志的记录了,这样逻辑就不对了,因为我们希望在新用户保存的同时,存储日志的表里面就会有一条记录。

所以事务应该加到业务逻辑层,即service层。应该把事务加到UserService中的add方法开始的前后,add方法一旦有异常,事务就会回滚,就会使得保存用户和添加日志达到同步。如果UserDAOImpl中的save方法抛出异常,则新用户就不能被保存,则日志记录中的save方法也不会执行,也就是说当事务添加到service层的话,事务回滚是同步的。在没学Spring之前,要使用事务,需要通过手动添加代码的方式,即在方法开始写beginTransaction,方法结束写rollback,并且需要每次写try{}catch(){};学了Spring之后,Spring有专门管理事务的类HibernateTransactionManager(是一个切面类,在方法的前后进行操作,其实已经应用了AOP),你要连接数据库,HibernateTransactionManager会通过数据库的连接来管理事务,你要是跨数据库,通过JTA(Java
Transaction API)来管理事务。现在只有一个数据库,通过数据库的连接管理事物,通过注入sessionFactory来实现,sessionFactory里面注入了dataSource,就拿到了连接。还有一点是HibernateTransactionManager管理时需要hibernate的一些配置,这也会通过sessionFactory,因为sessionFactory里面有这些配置。
2、编写业务逻辑方法 

* 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate是Hibernate session的封装    * 默认的回滚是RuntimeException(包括继承RuntimeException的子类),普通异常不回滚  

  

* 在编写业务逻辑方法时,最好将异常一直往上抛出,在呈现层处理(struts)    

* spring的事务需要设置到业务方法上(事务边界定义到Facade类上),不要添加到Dao上 

3、了解事务的几种传播特性 

1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启 

2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行 

3. PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。 

4. PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。 

5. PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。 

6. PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常 

7. PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 

     则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行 

4、Spring事务的隔离级别 

1. ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别. 

     另外四个与JDBC的隔离级别相对应 

2. ISOLATION_READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。 

     这种隔离级别会产生脏读,不可重复读和幻像读。 

3. ISOLATION_READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据 

4. ISOLATION_REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 

     它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。 

5. ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 

     除了防止脏读,不可重复读外,还避免了幻像读。 
加入xml的头信息文件:spring-tx-2.5.xsd文件:。然后再bean.xml配置:

1..xmlns:tx="http://www.springframework.org/schema/tx"  

2. http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
<?xml version="1.0" encoding="UTF-8"?>   

  

<beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    
<!-- 配置SessionFactory -->   

<bean

                   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

                   <property name="locations">

                            <value>classpath:jdbc.properties</value><!—资源文件名字为jdbc.properties-->

                   </property>

         </bean>

<!-- 配置dataSource ->   

 

         <bean id="dataSource" destroy-method="close"

                   class="org.apache.commons.dbcp.BasicDataSource">

                   <property name=   "

                            value="${jdbc.driverClassName}" />

                   <property name="url" value="${jdbc.url}" />

                   <property name="username" value="${jdbc.username}" />

                   <property name="password" value="${jdbc.password}" />

         </bean>

把dataSource注入给sessionFactory:

<!-- 配置sessionFactory ->   

         <bean id="sessionFactory"

         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

                   <property name="dataSource" ref="dataSource" />

                   <property name="annotatedClasses">

                            <list>

                                     <value>com.lp.model.User</value>

                                     <value>com.lp.model.Log</value>

                            </list>

                   </property>

                   <property name="hibernateProperties">

                            <props>

                                     <prop key="hibernate.dialect">

                                               org.hibernate.dialect.MySQLDialect

                                     </prop>

                                     <prop key="hibernate.show_sql">true</prop>

                            </props>

                   </property>

         </bean>    

 

又把sessionFactory注给txManager:

(为什么把sessionFactory (bean)注给TransactionManager呢?原因是txManager需要用到sessionFactory中所保留的hibernate的那些配置,同时需要数据源,接下来对于事物的管理就交给了txManager了,它会在你需要事务的时候,配置事物管理。最后必须加入这句话在配置文件bean.xml中。<tx:annotation-driven transaction-manager="txManager"/>,比如说在UserService下面,你需要在add下面有事务,你就可以在add方法前面上(@Transactional),它会自动的帮你在前后加上关于事务的业务逻辑。开头的是beginTransaction,结束的时候是Transaction.commit,如果要是catch到任何的异常,就会rollback,如果是RuntimeException,就会自动回滚。有了spring对事务的管理,就方便的多了,不用在方法中重复的写try{}catch{},

beginTransaction,Transaction.commit,rollback等等。

<!-- 配置事务管理器 TransactionManager-->   

         <bean id="txManager"

                   class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                   <property name="sessionFactory" ref="sessionFactory" />

         </bean>    

  <!--启动spring Annotation注解功能管理事物-->

         <tx:annotation-driven transaction-manager="txManager"/>

 

<!-- 配置事务传播特性 -->   

    <tx:advice id="txAdvice" transaction-manager=" txManager ">   

         <tx:attributes>   

             <tx:method name="add*" propagation="REQUIRED"/>    

              <tx:method name="del*" propagation="REQUIRED"/>   

              <tx:method name="update*" propagation="REQUIRED"/>   

              <tx:method name="*" read-only="true"/>   

         </tx:attributes>   

</tx:advice>   

 

    <!—哪些类使用事务 -->   

    <aop:config>   

      <aop:pointcut id="point-cut" expression="execution(* com.wlh.spring.manager.*.*(..))"/>   

      <aop:advisor advice-ref="txAdvice" pointcut-ref="point-cut"/>   

    </aop:config>   

       

</beans>  

部分代码:参考Spring19_Spring_hibernate_Transaction

总结:Spring与hibernate整合

1 首先要加入整合需要的jar包

2. 在beans.xml文件中配置:加入各种.xsd文件

<beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    
3.在做其它的配置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐