转自:http://blog.csdn.net/liusong0605/article/details/8440842
事务,就是保证一系列业务逻辑全部执行或者全部不执行,在开发中,事务是怎么控制的呢?
方案一、使用hibernate的OpenSession()。这种方式需要在业务边界创建session,并将session作为参数传递到Dao层,以此来保证多个业务逻辑之间使用的是同一个session。
添加用户的同时要完成addLog()和addUser()两个操作:
1. LogManagerImpl类中的添加日志
public void addLog(Log log,Session session){
session.save(log,session);
}
2. 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
3. 使用currentSession,需要在hibernate.cfg.xml配置文件中开启事务
<property name="hibernate.current_session_context_class">thread</property>
方案三、将hibernate和spring集成,使用spring框架的声明式事务。
使用spring的声明式事务,不再需要自动创建sessionFactory和Session,不再需要手动控制事务的开启和关闭。
使用spring声明式事务的几个步骤:
1. applicationContext.xml中进行配置
- <!-- 配置事务管理器 -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/> <!-- transactionManager的SetSessionFactory()方法的参数为SessionFactory --> </property>
- </bean>
- <!-- 那些类那些方法使用事务 -->
- <aop:config>
- <aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.manager.*.*(..))"/>
- <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
- </aop:config>
- <!-- 事务的传播特性 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED"/>
- <tx:method name="del*" propagation="REQUIRED"/>
- <tx:method name="modify*" propagation="REQUIRED"/>
- <tx:method name="*" propagation="REQUIRED" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- 2.UserManagerImpl类继承HibernateDaoSupport
- public class UserManagerImpl extends HibernateDaoSupport{
- public void addUser(User user)
- throws Exception {
- this.getHibernateTemplate().save(user);
- log.setType("操作日志");
- log.setTime(new Date());
- log.setDetail("XXX");
- logManager.addLog(log);
- throw new Exception();
- }