HHH-7959 gracefully handle non-transactional entity cache

Conflicts:
	hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java
This commit is contained in:
Brett Meyer 2013-08-07 11:50:13 -04:00
parent 9b4c401d04
commit 78f8efd36d
1 changed files with 31 additions and 17 deletions

View File

@ -23,13 +23,14 @@
package org.hibernate.cache.infinispan.util; package org.hibernate.cache.infinispan.util;
import org.infinispan.AdvancedCache; import java.util.concurrent.Callable;
import org.infinispan.context.Flag;
import org.infinispan.remoting.rpc.RpcManager;
import javax.transaction.Status; import javax.transaction.Status;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import java.util.concurrent.Callable;
import org.infinispan.AdvancedCache;
import org.infinispan.context.Flag;
import org.infinispan.remoting.rpc.RpcManager;
/** /**
* Helper for dealing with Infinispan cache instances. * Helper for dealing with Infinispan cache instances.
@ -49,19 +50,32 @@ public class Caches {
return withinTx(cache.getTransactionManager(), c); return withinTx(cache.getTransactionManager(), c);
} }
public static <T> T withinTx(TransactionManager tm, public static <T> T withinTx(TransactionManager tm, Callable<T> c) throws Exception {
Callable<T> c) throws Exception { if ( tm == null ) {
tm.begin(); try {
try { return c.call();
return c.call(); }
} catch (Exception e) { catch ( Exception e ) {
tm.setRollbackOnly(); throw e;
throw e; }
} finally { }
if (tm.getStatus() == Status.STATUS_ACTIVE) tm.commit(); else {
else tm.rollback(); 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();
}
}
}
public static AdvancedCache localCache(AdvancedCache cache) { public static AdvancedCache localCache(AdvancedCache cache) {
return cache.withFlags(Flag.CACHE_MODE_LOCAL); return cache.withFlags(Flag.CACHE_MODE_LOCAL);