What is the difference between org.hibernate.context.JTASessionContext
and 开发者_如何转开发org.hibernate.context.ThreadLocalSessionContex
?
With ThreadLocalSessionContext
, the current session (sessionFactory.getCurrentSession()
) is created and stored in a ThreadLocal
. This works in any environment, because ThreadLocal
is JavaSE.
JTASessionContext binds the current session to a JTA transaction. The JTA transaction provides a hook for cleanup, unlike the thread local. This is available in environments that have JTA, such as an application server.
This all is explained in the javadocs of the two classes. here and here
These strategies control behaviour of SessionFactory.getCurrentSession()
by defining the scope of the current session.
JTASessionContext
associates current session with the current JTA transaction and closes it at the end of JTA transaction. This strategy is used in JTA-capable environments, i.e. in application servers.ThreadLocalSessionContext
associates current session with the current thread and closes it at the end of transaction created in that session. It's for use in standalone environments.
By the way, javadoc and reference describe it pretty clear.
See also:
- 2.3. Contextual sessions
- JTASessionContext
- ThreadLocalSessionContext
精彩评论