The easiest way to handle
and transactions is Hibernate's automatic "current"
management. For a discussion of contextual sessions see Section 2.5, “Contextual sessions”
. Using the
session context, if there is no Hibernate
associated with the current JTA transaction, one will be started and associated with that JTA transaction the first time you call
s retrieved via
context are set to automatically flush before the transaction completes, close after the transaction completes, and aggressively release JDBC connections after each statement. This allows the
s to be managed by the life cycle of the JTA transaction to which it is associated, keeping user code clean of such management concerns. Your code can either use JTA programmatically through
, or (recommended for portable code) use the Hibernate
API to set transaction boundaries. If you run in an EJB container, declarative transaction demarcation with CMT is preferred.