sessionFactory中的openSession和getCurrentSession的一些注意事项

今天进行Hibernate测试时遇到了一个问题

我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常:

Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

应该是说在主线程中不能包含事务同步会话

参考链接:

https://blog.csdn.net/wang1472jian1110/article/details/42679217

http://www.360doc.com/content/11/0502/19/987036_113784420.shtml

优先推荐:https://blog.csdn.net/lmdcszh/article/details/13775933

相同点

    openSession与getCurrentSession都可以创建session

不同点

    * openSession没有绑定当前线程,所以,使用完后必须关闭,

    * currentSession和当前线程绑定,在事务结束后会自动关闭。

猜你喜欢

转载自www.cnblogs.com/Sword007/p/10624650.html
今日推荐