美文网首页
ssh中处理事务的几种方式

ssh中处理事务的几种方式

作者: 小沙鹰168 | 来源:发表于2017-02-22 11:12 被阅读0次

事务,就是保证一系列业务逻辑全部执行或者全部不执行,在开发中,事务是怎么控制的呢?
方法一、使用hibernate的OpenSession()。这种方式需要在业务边界创建session,并将session作为参数传递到Dao层,以此来保证多个业务逻辑之间使用的是同一个session。

         添加用户的同时要完成addLog()和addUser()两个操作:
           1. LogManagerImpl类中的添加日志

          public void addLog(Log log,Session session){
                  session.save(log,session);
           }
  1. UserManagerImpl类中完成所有业务逻辑
//openSession()创建session    
SessionFactory factory= new Configuration().configure();  
Session session=factory.openSession();            
//开启事务           
session.beginTransaction();           
//执行业务逻辑1. 保存user      
session.save(user);                
//Log和LogManagerImpl的创建由IoC控制                        
log.setTime(new Date());      
log.setType("操作日志");                
//执行业务逻辑2. 保存log,同时传递session           
logManager.addLog(log,session);                   
session.getTransaction().commit();           
//使用openSession,当最后一个业务逻辑完成后必须关闭session           
session.close();  
 方法二、 使用Hibernate的getCurrentSession(),currentSession和openSession的区别在于,使用currentSession使用完毕后不用手动关闭session。currentSession相当于将session放到一个ThreadLocal中。
  1. LogManagerImpl类

       pubic  void addLog(Log log){
    
             //可以通过getCurrentSession()创建Session,不必使用传递的session
    
            Session session= factory.getCurrentSession()
    
            session.save(log);
    
     }
    
  2. UserManagerImpl类中完成所有业务逻辑

 //openSession()创建session        
SessionFactory factory= new Configuration().configure();        
Session session=factory.getCurrentSession();                
//开启事务               
session.beginTransaction();               
//执行业务逻辑1. 保存user          
session.save(user);                    
//Log和LogManagerImpl的创建由IoC控制                            
log.setTime(new Date());          
log.setType("操作日志");                    
//执行业务逻辑2. 保存log              
logManager.addLog(log);                       
session.getTransaction().commit();               
//使用currentSession,当最后一个业务逻辑完成后不用关闭session
  1. 使用currentSession,需要在hibernate.cfg.xml配置文件中开启事务
<property name="hibernate.current_session_context_class">thread</property>

方案三、将hibernate和spring集成,使用spring框架的声明式事务。使用spring的声明式事务,不再需要自动创建sessionFactory和Session,不再需要手动控制事务的开启和关闭。
使用spring声明式事务的几个步骤:
1. applicationContext.xml中进行配置

<!-- 配置spring事务管理器-->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="qq"></property>
    </bean>
    <!-- 配置spring事务,基于xml开发-->
    <!-- 激活自动代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <!-- 配置aop -->
    <aop:config>
    <aop:pointcut expression="execution(* com.hw.service.impl.*.*(..))" id="pointcut"/>
    <aop:advisor advice-ref="trcut" pointcut-ref="pointcut"/><!-- 增强切 -->
    </aop:config>
    <!-- 定义事务通知 -->
    <tx:advice id="trcut" transaction-manager="transactionManager">
    <!-- 定义事务传播规则 -->
    <tx:attributes>
    <!-- REQUIRED的含义是支持当前已经存在的事务,如果还没有事务,就创建一个新事务 
    <tx:method name="*" propagation="REQUIRED"/> 
    表示所有方法都应用REQUIRED事务规则-->
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="*" propagation="REQUIRED" read-only="true"/>
    </tx:attributes> 

相关文章

网友评论

      本文标题:ssh中处理事务的几种方式

      本文链接:https://www.haomeiwen.com/subject/tggpwttx.html