在Hibernate中,可以通过代码来操作管理事务,如通过
“Transaction tx=session.beginTransactiong();”
开启一个事务,持久化操作后,通过"tx.commit();" 提交事务;如果事务出现异常,又通过“tx.rollback();"操作来撤销事务(事务回滚)。
除了在代码中对事务开启,提交和回滚操作外,还可以在hibernate的配置文件中对事务进行配置。配置文件中,可以设置事务的隔离级别。其具体的配置方法是在hibernate.cfg.xml文件中的
标签元素中进行的。配置方法如下所示。
4
到这里我们已经设置了事务的隔离级别,那么我们在真正进行事务管理的时候,需要考虑事务的应用场景,也就是说我们的事务控制不应该是在DAO层实现的,应该在Service层实现,并且在Service中调用多个DAO实现一个业务逻辑的操作。具体操作如下显示:
![60f6fb78537fae1e16aaa8010892486c.png](https://i-blog.csdnimg.cn/blog_migrate/72327e88b36c06f6da8284e65205b33d.jpeg)
其实最主要的是如何保证在Service中开启的事务时使用的Session对象和DAO中多个操作使用的是同一个Session对象。
其实有两种办法可以实现:
1. 可以在业务层获取到Session,并将Session作为参数传递给DAO。
2. 可以使用ThreadLocal将业务层获取的Session绑定到当前线程中,然后再DAO中获取Session的时候,都从当前线程中获取。
其实使用第二种方式肯定是最优方案,那么具体的实现已经不用我们来完成了,hibernate的内部已经将这个事情做完了。我们只需要完成一段配置即可。
Hibernate5中自身提供了三种管理Session对象的方法
Session对象的生命周期与本地线程绑定
Session对象的生命周期与JTA事务绑定
Hibernate委托程序管理Session对象的生命周期
在Hibernate的配置文件中,hibernate.current_session_context_class属性用于指定Session管理方式,可选值包括:
1. thread:Session对象的生命周期与本地线程绑定(推荐)
2. jta:Session对象的生命周期与JTA事务绑定
3. managed:hibernate委托程序来管理Session对象的生命周期。
在hibernate.cfg.xml中进行如下配置:
thread
Hibernate提供sessionFactory.getCurrentSession()创建一个session和ThreadLocal绑定方法。
在HibernateUtils工具类中更改getCurrentSession方法:
public static Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
而且Hibernate中提供的这个与线程绑定的session可以不用关闭,当线程执行结束后,就会自动关闭了。
所以最终的配置文件如下:
![2314e58776746216efa33a98cd6238d7.gif](https://i-blog.csdnimg.cn/blog_migrate/74d2273635f04b02e24ae7ca50cfc19e.gif)
![b0b1df62f75a598f6713238aed41f400.gif](https://i-blog.csdnimg.cn/blog_migrate/8d4196eacacf324e7c4afeb0a8e5e2a8.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 4 5 com.mysql.jdbc.Driver 6 jdbc:mysql:///day24_db 7 root 8 toor 9 org.hibernate.dialect.MySQLDialect10 true11 true12 update13 24 25 26 427 28 thread29 30 31
1 package cn.eagle.utils; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 public class HibernateUtils { 8 9 private static final Configuration configuration;10 private static final SessionFactory sessionFactory;11 12 static {13 configuration = new Configuration().configure();14 sessionFactory = configuration.buildSessionFactory();15 }16 17 public static Session getCurrentSession() {18 return sessionFactory.getCurrentSession();19 }20 }