Konstantin Kudryavtsev
Konstantin Kudryavtsev

Reputation: 592

hibernate JTASessionContext vs ThreadLocalSessionContext

What is the difference between org.hibernate.context.JTASessionContext and org.hibernate.context.ThreadLocalSessionContex?

Upvotes: 3

Views: 4423

Answers (2)

Bozho
Bozho

Reputation: 597224

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

Upvotes: 5

axtavt
axtavt

Reputation: 242716

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:

Upvotes: 2

Related Questions