if ( tx == null || !tx.isActive() ) {
throw new IllegalStateException( "Transaction not active" );
}
if ( rollbackOnly ) {
tx.rollback();
throw new RollbackException( "Transaction marked as rollbackOnly" );
}
try {
tx.commit();
}
catch (Exception e) {
Exception wrappedException;
if (e instanceof StaleStateException) {
wrappedException = entityManager.wrapStaleStateException( (StaleStateException) e );
}
else if (e instanceof HibernateException) {
throw entityManager.convert( (HibernateException)e );
}
else {
wrappedException = e;
}
try {
//as per the spec we should rollback if commit fails
tx.rollback();
}
catch (Exception re) {
//swallow
}
throw new RollbackException( "Error while commiting the transaction", wrappedException );
}
finally {
rollbackOnly = false;
}
//if closed and we commit, the mode should have been adjusted already