Spring + Hibernate session management across multiple threads

耗尽温柔 提交于 2019-12-03 02:11:17

Yes. It is enough.

When setting hibernate.current_session_context_class to thread , the session returned from SessionFactory.getCurrentSession() is from the ThreadLocal instance .

Every thread will have their own, independently ThreadLocal instance , so different threads will not access to the same hibernate session .

The behaviour of SessionFactory.getCurrentSession() is that : if it is called for the first time in the current thread, a new Session is opened and returned . If it is called again in the same thread , the same session will be returned.

As a result , you can get the same session to use in different DAO methods in the same transaction code by simply calling SessionFactory.getCurrentSession() . It prevent you from passing the Hibernate session through the DAO method 's input parameters in the case that you have to call many different DAO methods in the same transaction code .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!