Hibernate active transaction

lweller's answer is a more appropriate approach than my answer, but you can check the state of a transaction by calling session.getTransaction().isActive()

See the javadoc for Hibernate Transaction.


Basically you can call session.beginTransaction(); in any case as it is specified by JavaDoc of Hibernate:

Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction.

But I would seriously consider the usage of a framework for transation management, like spring


((SessionImpl)session).isTransactionInProgress()

This will return whether the transaction is active or not, without creating any new transaction.

Tags:

Hibernate