Hi Devs,
While working on a client project I came across an issue where an exception
is thrown with a message that reads something like:
"Caused by: org.hibernate.HibernateException: Failed to commit or rollback
transaction. Root cause []"
Upon further digging I found the following code in XWikiHibernateBaseStore:
<code>
/**
* Ends a transaction
*
* @param context
* @param commit should we commit or not
*/
public void endTransaction(XWikiContext context, boolean commit)
{
endTransaction(context, commit, false);
}
/**
* Ends a transaction
*
* @param context
* @param commit should we commit or not
* @param withTransaction
* @throws HibernateException
*/
public void endTransaction(XWikiContext context, boolean commit, boolean
withTransaction) throws HibernateException
{
Session session = null;
try {
session = getSession(context);
Transaction transaction = getTransaction(context);
setSession(null, context);
setTransaction(null, context);
if (transaction != null) {
// We need to clean up our connection map first because the
connection will
// be aggressively closed by hibernate 3.1 and more
preCloseSession(session);
if (log.isDebugEnabled()) {
log.debug("Releasing hibernate transaction " + transaction);
}
if (commit) {
transaction.commit();
} else {
transaction.rollback();
}
}
} catch (HibernateException e) {
// Ensure the original cause will get printed.
throw new HibernateException("Failed to commit or rollback transaction.
Root cause ["
+ getExceptionMessage(e) + "]", e);
} finally {
closeSession(session);
}
}
</code>
The problem i see in this code is that if preCloseSession(session) or
transaction.commit() throws an exception, there is no attempt made to
rollback the transaction. Normally a transaction rollback attempt should be
made within the catch block.
If I understand the basics of transaction handling, this code is wrong. I
want to discuss this with you (other developers) because I'm not an expert
in this subject :)
Thanks.
- Asiri