Hibernate mark as rollback even for no transaction requests

Description

When used a Hibernate EntityManager in a CMT Bean and a GenericJDBCException is launched, Hibernate will try a rollback even if there is not an active transaction, generating the exception:

<org.hibernate.ejb.AbstractEntityManagerImpl> Unable to mark for rollback on PersistenceException:
java.lang.IllegalStateException: [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] No such transaction!
at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.setRollbackOnly(BaseTransaction.java:188)
at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.setRollbackOnly(BaseTransactionManagerDelegate.java:135)
at org.hibernate.ejb.AbstractEntityManagerImpl.markAsRollback(AbstractEntityManagerImpl.java:440)
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:595)
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:74)

... hiding the original "Caused By" from the other Exception (GenericJDBCException).

As example, a case where some error in runtime happens in a database procedure execution and the bean method is annotated as "NOT_SUPPORTED" transaction, this behaviour will happen and the original exception will be lost, the root exception is overridden by "No such transaction" exception and so the cause is not reported in log files. Hibernate should not try a rollback if there is not a transaction.

Fixing it is necessary update the method:
"protected void markAsRollback()"

.. from org.hibernate.ejb.AbstractEntityManagerImpl.java
That method just check no CMT JTA transaction:

"if ( tx.isActive() ) {
tx.setRollbackOnly();
}"

The same check is necessary for CMT JTA using transactionManager.getStatus and looking if it is different than Status.NoTransaction.
This check should be done above the code that call transactionManager.setRollbackOnly();

Environment

None

Activity

Show:
Strong Liu
August 30, 2011, 6:49 AM

Alessandro,

which branch is this patch for?
and does it affect 3.6 or 4.0?

Alessandro Lazarotti
August 30, 2011, 7:14 AM

It is based on Hibernate 4 (master branch).
However, previous versions have the same problem and similar fix can be used.

Strong Liu
August 30, 2011, 4:14 PM
Brett Meyer
March 7, 2014, 10:09 PM

Bulk closing tickets resolved in released versions

Andreas Benneke
March 8, 2017, 9:10 PM

This one seems to be back for Hibernate 5.2.

See AbstractSharedSessionContract.markForRollbackOnly and invoked methods...

Reopen? Or should we file a new one?

Thank you!

Assignee

Strong Liu

Reporter

Alessandro Lazarotti

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure