|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| package org.objectweb.easybeans.transaction.interceptors; |
|
27 |
| |
|
28 |
| import static javax.transaction.Status.STATUS_ACTIVE; |
|
29 |
| import static javax.transaction.Status.STATUS_MARKED_ROLLBACK; |
|
30 |
| |
|
31 |
| import javax.ejb.EJBException; |
|
32 |
| import javax.ejb.TransactionRolledbackLocalException; |
|
33 |
| import javax.transaction.NotSupportedException; |
|
34 |
| import javax.transaction.RollbackException; |
|
35 |
| import javax.transaction.SystemException; |
|
36 |
| import javax.transaction.Transaction; |
|
37 |
| |
|
38 |
| import org.objectweb.easybeans.api.EasyBeansInvocationContext; |
|
39 |
| import org.objectweb.easybeans.log.JLog; |
|
40 |
| import org.objectweb.easybeans.log.JLogFactory; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| public class CMTRequiredTransactionInterceptor extends AbsTransactionInterceptor { |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| private JLog logger = JLogFactory.getLog(CMTRequiredTransactionInterceptor.class); |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
0
| public CMTRequiredTransactionInterceptor() {
|
|
59 |
0
| super();
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
0
| @Override
|
|
72 |
| public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { |
|
73 |
0
| logger.debug("Calling Required TX interceptor");
|
|
74 |
| |
|
75 |
| |
|
76 |
0
| Transaction transaction;
|
|
77 |
0
| try {
|
|
78 |
0
| transaction = getTransactionManager().getTransaction();
|
|
79 |
| } catch (SystemException se) { |
|
80 |
0
| throw new EJBException("Cannot get the current transaction on transaction manager.", se);
|
|
81 |
| } |
|
82 |
| |
|
83 |
0
| logger.debug("Transaction found = {0}", transaction);
|
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
0
| boolean startedTransaction = false;
|
|
93 |
0
| if (transaction == null) {
|
|
94 |
0
| try {
|
|
95 |
0
| getTransactionManager().begin();
|
|
96 |
0
| startedTransaction = true;
|
|
97 |
| } catch (NotSupportedException nse) { |
|
98 |
0
| throw new EJBException("Transaction Manager implementation does not support nested transactions.", nse);
|
|
99 |
| } catch (SystemException se) { |
|
100 |
0
| throw new EJBException("Cannot call begin() on the transaction manager.", se);
|
|
101 |
| } |
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
0
| try {
|
|
111 |
0
| return invocationContext.proceed();
|
|
112 |
| } finally { |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
0
| if (startedTransaction) {
|
|
117 |
| |
|
118 |
| |
|
119 |
0
| Transaction transactionAfter = null;
|
|
120 |
0
| try {
|
|
121 |
0
| transactionAfter = getTransactionManager().getTransaction();
|
|
122 |
| } catch (SystemException se) { |
|
123 |
0
| throw new EJBException("Cannot get the current transaction on transaction manager.", se);
|
|
124 |
| } |
|
125 |
| |
|
126 |
0
| if (transactionAfter == null) {
|
|
127 |
0
| throw new RuntimeException("Transaction disappeared.");
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
0
| try {
|
|
137 |
0
| switch (getTransactionManager().getStatus()) {
|
|
138 |
0
| case STATUS_ACTIVE:
|
|
139 |
0
| getTransactionManager().commit();
|
|
140 |
0
| break;
|
|
141 |
0
| case STATUS_MARKED_ROLLBACK:
|
|
142 |
0
| getTransactionManager().rollback();
|
|
143 |
0
| break;
|
|
144 |
0
| default:
|
|
145 |
0
| throw new RuntimeException("Unexpected transaction status" + getTransactionManager().getStatus());
|
|
146 |
| } |
|
147 |
| } catch (RollbackException e) { |
|
148 |
0
| throw new TransactionRolledbackLocalException("Could not commit transaction", e);
|
|
149 |
| } catch (Exception e) { |
|
150 |
0
| throw new EJBException("Container exception", e);
|
|
151 |
| } |
|
152 |
| } |
|
153 |
| } |
|
154 |
| } |
|
155 |
| |
|
156 |
| } |