23
23
24
24
package org .hibernate .cache .infinispan .util ;
25
25
26
- import org .infinispan .AdvancedCache ;
27
- import org .infinispan .context .Flag ;
28
- import org .infinispan .remoting .rpc .RpcManager ;
26
+ import java .util .concurrent .Callable ;
29
27
30
28
import javax .transaction .Status ;
31
29
import 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 ;
33
34
34
35
/**
35
36
* Helper for dealing with Infinispan cache instances.
@@ -49,19 +50,32 @@ public static <T> T withinTx(AdvancedCache cache,
49
50
return withinTx (cache .getTransactionManager (), c );
50
51
}
51
52
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
+ }
65
79
66
80
public static AdvancedCache localCache (AdvancedCache cache ) {
67
81
return cache .withFlags (Flag .CACHE_MODE_LOCAL );
0 commit comments