2323
2424package org .hibernate .cache .infinispan .util ;
2525
26- import org .infinispan .AdvancedCache ;
27- import org .infinispan .context .Flag ;
28- import org .infinispan .remoting .rpc .RpcManager ;
26+ import java .util .concurrent .Callable ;
2927
3028import javax .transaction .Status ;
3129import javax .transaction .TransactionManager ;
32- import java .util .concurrent .Callable ;
30+
31+ import org .infinispan .AdvancedCache ;
32+ import org .infinispan .context .Flag ;
33+ import org .infinispan .remoting .rpc .RpcManager ;
3334
3435/**
3536 * Helper for dealing with Infinispan cache instances.
@@ -49,19 +50,32 @@ public static <T> T withinTx(AdvancedCache cache,
4950 return withinTx (cache .getTransactionManager (), c );
5051 }
5152
52- public static <T > T withinTx (TransactionManager tm ,
53- Callable <T > c ) throws Exception {
54- tm .begin ();
55- try {
56- return c .call ();
57- } catch (Exception e ) {
58- tm .setRollbackOnly ();
59- throw e ;
60- } finally {
61- if (tm .getStatus () == Status .STATUS_ACTIVE ) tm .commit ();
62- else tm .rollback ();
63- }
64- }
53+ public static <T > T withinTx (TransactionManager tm , Callable <T > c ) throws Exception {
54+ if ( tm == null ) {
55+ try {
56+ return c .call ();
57+ }
58+ catch ( Exception e ) {
59+ throw e ;
60+ }
61+ }
62+ else {
63+ tm .begin ();
64+ try {
65+ return c .call ();
66+ }
67+ catch ( Exception e ) {
68+ tm .setRollbackOnly ();
69+ throw e ;
70+ }
71+ finally {
72+ if ( tm .getStatus () == Status .STATUS_ACTIVE )
73+ tm .commit ();
74+ else
75+ tm .rollback ();
76+ }
77+ }
78+ }
6579
6680 public static AdvancedCache localCache (AdvancedCache cache ) {
6781 return cache .withFlags (Flag .CACHE_MODE_LOCAL );
0 commit comments