HHH-7959 gracefully handle non-transactional entity cache

This commit is contained in:
Brett Meyer 2013-08-07 11:44:44 -04:00
parent a745f93bfa
commit 90532087d5
1 changed files with 23 additions and 13 deletions

View File

@ -74,20 +74,30 @@ public class Caches {
public static <T> T withinTx(
TransactionManager tm,
Callable<T> c) throws Exception {
tm.begin();
try {
return c.call();
}
catch (Exception e) {
tm.setRollbackOnly();
throw e;
}
finally {
if ( tm.getStatus() == Status.STATUS_ACTIVE ) {
tm.commit();
if ( tm == null ) {
try {
return c.call();
}
else {
tm.rollback();
catch (Exception e) {
throw e;
}
}
else {
tm.begin();
try {
return c.call();
}
catch (Exception e) {
tm.setRollbackOnly();
throw e;
}
finally {
if ( tm.getStatus() == Status.STATUS_ACTIVE ) {
tm.commit();
}
else {
tm.rollback();
}
}
}
}