1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.aopalliance.intercept.MethodInvocation;
24
25 import javax.inject.Inject;
26
27
28
29
30 class TxnInterceptor
31 implements MethodInterceptor
32 {
33
34
35
36
37 private UnitOfWork unitOfWork;
38
39
40
41
42 private TransactionFacadeFactory tfProvider;
43
44
45
46
47 private TransactionalAnnotationHelper txnAnnotationHelper;
48
49 @Inject
50 @VisibleForTesting
51 void init( UnitOfWork unitOfWork, TransactionFacadeFactory tfProvider,
52 TransactionalAnnotationHelper txnAnnotationHelper )
53 {
54 this.unitOfWork = unitOfWork;
55 this.tfProvider = tfProvider;
56 this.txnAnnotationHelper = txnAnnotationHelper;
57 }
58
59
60
61
62
63 public final Object invoke( MethodInvocation methodInvocation )
64 throws Throwable
65 {
66 if ( persistenceUnitParticipatesInTransactionFor( methodInvocation ) )
67 {
68 return invokeInTransactionAndUnitOfWork( methodInvocation );
69 }
70 else
71 {
72 return methodInvocation.proceed();
73 }
74
75 }
76
77
78
79
80
81
82
83
84
85 private boolean persistenceUnitParticipatesInTransactionFor( MethodInvocation methodInvocation )
86 {
87 return txnAnnotationHelper.persistenceUnitParticipatesInTransactionFor( methodInvocation );
88 }
89
90
91
92
93
94
95
96
97 private Object invokeInTransactionAndUnitOfWork( MethodInvocation methodInvocation )
98 throws Throwable
99 {
100 final boolean weStartedTheUnitOfWork = !unitOfWork.isActive();
101 if ( weStartedTheUnitOfWork )
102 {
103 unitOfWork.begin();
104 }
105
106 Throwable originalException = null;
107 try
108 {
109 return invokeInTransaction( methodInvocation );
110 }
111 catch ( Throwable exc )
112 {
113 originalException = exc;
114 throw exc;
115 }
116 finally
117 {
118 if ( weStartedTheUnitOfWork )
119 {
120 endUnitOfWork( originalException );
121 }
122 }
123 }
124
125
126
127
128
129
130
131
132
133 private void endUnitOfWork( Throwable originalException )
134 throws Throwable
135 {
136 try
137 {
138 unitOfWork.end();
139 }
140 catch ( Throwable exc )
141 {
142 if ( originalException != null )
143 {
144 throw originalException;
145 }
146 else
147 {
148 throw exc;
149 }
150 }
151 }
152
153
154
155
156
157
158
159
160 private Object invokeInTransaction( MethodInvocation methodInvocation )
161 throws Throwable
162 {
163 final TransactionFacade transactionFacade = tfProvider.createTransactionFacade();
164 transactionFacade.begin();
165 final Object result = invokeAndHandleException( methodInvocation, transactionFacade );
166 transactionFacade.commit();
167
168 return result;
169 }
170
171
172
173
174
175
176
177
178
179
180 private Object invokeAndHandleException( MethodInvocation methodInvocation, TransactionFacade transactionFacade )
181 throws Throwable
182 {
183 try
184 {
185 return methodInvocation.proceed();
186 }
187 catch ( Throwable exc )
188 {
189 handleException( methodInvocation, transactionFacade, exc );
190 throw exc;
191 }
192 }
193
194
195
196
197
198
199
200
201 private void handleException( MethodInvocation methodInvocation, TransactionFacade transactionFacade,
202 Throwable exc )
203 throws Throwable
204 {
205 try
206 {
207 if ( isRollbackNecessaryFor( methodInvocation, exc ) )
208 {
209 transactionFacade.rollback();
210 }
211 else
212 {
213 transactionFacade.commit();
214 }
215 }
216 catch ( Exception swallowedException )
217 {
218
219 throw exc;
220 }
221 }
222
223
224
225
226
227
228
229
230 private boolean isRollbackNecessaryFor( MethodInvocation methodInvocation, Throwable exc )
231 {
232 return txnAnnotationHelper.isRollbackNecessaryFor( methodInvocation, exc );
233 }
234
235 }