1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.interceptors; |
9 |
| |
10 |
| import org.jboss.aop.joinpoint.Invocation; |
11 |
| import org.jboss.aop.joinpoint.MethodInvocation; |
12 |
| import org.jboss.cache.Fqn; |
13 |
| import org.jboss.cache.pojo.PojoCacheException; |
14 |
| import org.jboss.cache.transaction.BatchModeTransactionManager; |
15 |
| |
16 |
| import javax.transaction.RollbackException; |
17 |
| import javax.transaction.Status; |
18 |
| import javax.transaction.Transaction; |
19 |
| import javax.transaction.TransactionManager; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class PojoTxInterceptor extends AbstractInterceptor |
28 |
| { |
29 |
| private TransactionManager localTm_ = null; |
30 |
| private TransactionManager txManager_; |
31 |
| public static final String TAG = "PC"; |
32 |
| public static final String TX = "TX"; |
33 |
| |
34 |
| |
35 |
25119
| public Object invoke(Invocation in) throws Throwable
|
36 |
| { |
37 |
25119
| if (!(in instanceof MethodInvocation))
|
38 |
| { |
39 |
0
| throw new IllegalArgumentException("TxInterceptor.invoke(): invocation not MethodInvocation");
|
40 |
| } |
41 |
25119
| MethodInvocation invocation = (MethodInvocation) in;
|
42 |
| |
43 |
25119
| if (txManager_ == null)
|
44 |
| { |
45 |
351
| txManager_ = getCache(invocation).getTransactionManager();
|
46 |
| } |
47 |
| |
48 |
25119
| Transaction tx = null;
|
49 |
25119
| if (txManager_ != null)
|
50 |
| { |
51 |
| |
52 |
25117
| localTm_ = txManager_;
|
53 |
| } |
54 |
| else |
55 |
| { |
56 |
2
| localTm_ = BatchModeTransactionManager.getInstance();
|
57 |
| } |
58 |
| |
59 |
25119
| tx = localTm_.getTransaction();
|
60 |
| |
61 |
25119
| boolean needTx = false;
|
62 |
2941
| if (tx == null) needTx = true;
|
63 |
25119
| Fqn id = null;
|
64 |
25119
| try
|
65 |
| { |
66 |
25119
| if (needTx)
|
67 |
| { |
68 |
2941
| id = (Fqn) invocation.getArguments()[0];
|
69 |
2941
| log.debug("Initiating a local transaction for batch processing with id: " + id.toString());
|
70 |
| |
71 |
2941
| try
|
72 |
| { |
73 |
2941
| localTm_.begin();
|
74 |
2941
| tx = localTm_.getTransaction();
|
75 |
| |
76 |
2941
| invocation.getMetaData().addMetaData(TAG, TX, tx);
|
77 |
2941
| return invocation.invokeNext();
|
78 |
| } |
79 |
| catch (Exception e) |
80 |
| { |
81 |
2
| log.warn(invocation.getMethod().getName() + ": exception occurred: " + e);
|
82 |
2
| try
|
83 |
| { |
84 |
2
| localTm_.setRollbackOnly();
|
85 |
| } |
86 |
| catch (Exception e2) |
87 |
| { |
88 |
0
| log.error("setRollbackOnly", e2);
|
89 |
| } |
90 |
| |
91 |
2
| throw new PojoCacheException("PojoCache operation will be rollback. id: " + id
|
92 |
| + ". Operation: " + invocation.getMethod().getName(), e); |
93 |
| } |
94 |
| } |
95 |
| else |
96 |
| { |
97 |
22178
| invocation.getMetaData().addMetaData(TAG, TX, tx);
|
98 |
22178
| return invocation.invokeNext();
|
99 |
| } |
100 |
| } |
101 |
| finally |
102 |
| { |
103 |
25119
| if (needTx)
|
104 |
| { |
105 |
2941
| endTransaction(id);
|
106 |
| } |
107 |
| } |
108 |
| } |
109 |
| |
110 |
2941
| private void endTransaction(Fqn id)
|
111 |
| { |
112 |
2941
| if (localTm_ == null)
|
113 |
| { |
114 |
0
| log.warn("endTransaction(): tm is null for id: " + id);
|
115 |
0
| return;
|
116 |
| } |
117 |
| |
118 |
| |
119 |
2941
| try
|
120 |
| { |
121 |
2941
| if (localTm_.getTransaction().getStatus() != Status.STATUS_MARKED_ROLLBACK)
|
122 |
| { |
123 |
2928
| localTm_.commit();
|
124 |
| } |
125 |
13
| else if (localTm_.getTransaction().getStatus() == Status.STATUS_ROLLEDBACK)
|
126 |
| { |
127 |
0
| log.info("endTransaction(): has been rolled back for id: " + id);
|
128 |
| } |
129 |
| else |
130 |
| { |
131 |
13
| log.info("endTransaction(): rolling back tx for id: " + id);
|
132 |
13
| localTm_.rollback();
|
133 |
| } |
134 |
| } |
135 |
| catch (RollbackException re) |
136 |
| { |
137 |
| |
138 |
2
| log.warn("endTransaction(): rolling back transaction with exception: " + re);
|
139 |
| } |
140 |
| catch (Exception e) |
141 |
| { |
142 |
0
| log.warn("endTransaction(): Failed with exception: " + e);
|
143 |
| } |
144 |
| } |
145 |
| } |