1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jdbc.xa; |
12 | |
|
13 | |
import java.lang.reflect.InvocationHandler; |
14 | |
import java.lang.reflect.InvocationTargetException; |
15 | |
import java.lang.reflect.Method; |
16 | |
import java.lang.reflect.Proxy; |
17 | |
import java.sql.CallableStatement; |
18 | |
import java.sql.Connection; |
19 | |
import java.sql.DatabaseMetaData; |
20 | |
import java.sql.PreparedStatement; |
21 | |
import java.sql.SQLException; |
22 | |
import java.sql.SQLWarning; |
23 | |
import java.sql.Savepoint; |
24 | |
import java.sql.Statement; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import javax.sql.XAConnection; |
28 | |
import javax.transaction.Transaction; |
29 | |
import javax.transaction.TransactionManager; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ConnectionWrapper implements Connection |
35 | |
{ |
36 | |
|
37 | |
private XAConnection xaCon; |
38 | |
private Connection con; |
39 | |
private TransactionManager tm; |
40 | |
private Transaction tx; |
41 | |
|
42 | |
public ConnectionWrapper(XAConnection xaCon, TransactionManager tm) throws SQLException |
43 | 0 | { |
44 | 0 | this.xaCon = xaCon; |
45 | 0 | this.con = xaCon.getConnection(); |
46 | 0 | this.tm = tm; |
47 | 0 | this.tx = null; |
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public int getHoldability() throws SQLException |
56 | |
{ |
57 | 0 | return con.getHoldability(); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public int getTransactionIsolation() throws SQLException |
66 | |
{ |
67 | 0 | return con.getTransactionIsolation(); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void clearWarnings() throws SQLException |
76 | |
{ |
77 | 0 | con.clearWarnings(); |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void close() throws SQLException |
86 | |
{ |
87 | 0 | con.close(); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void commit() throws SQLException |
96 | |
{ |
97 | 0 | con.commit(); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void rollback() throws SQLException |
106 | |
{ |
107 | 0 | con.rollback(); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public boolean getAutoCommit() throws SQLException |
116 | |
{ |
117 | 0 | return con.getAutoCommit(); |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public boolean isClosed() throws SQLException |
126 | |
{ |
127 | 0 | return con.isClosed(); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public boolean isReadOnly() throws SQLException |
136 | |
{ |
137 | 0 | return con.isReadOnly(); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public void setHoldability(int holdability) throws SQLException |
146 | |
{ |
147 | 0 | con.setHoldability(holdability); |
148 | 0 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public void setTransactionIsolation(int level) throws SQLException |
156 | |
{ |
157 | 0 | con.setTransactionIsolation(level); |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void setAutoCommit(boolean autoCommit) throws SQLException |
166 | |
{ |
167 | 0 | con.setAutoCommit(autoCommit); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public void setReadOnly(boolean readOnly) throws SQLException |
176 | |
{ |
177 | 0 | con.setReadOnly(readOnly); |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public String getCatalog() throws SQLException |
186 | |
{ |
187 | 0 | return con.getCatalog(); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public void setCatalog(String catalog) throws SQLException |
196 | |
{ |
197 | 0 | con.setCatalog(catalog); |
198 | 0 | } |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public DatabaseMetaData getMetaData() throws SQLException |
206 | |
{ |
207 | 0 | return con.getMetaData(); |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public SQLWarning getWarnings() throws SQLException |
216 | |
{ |
217 | 0 | return con.getWarnings(); |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public Savepoint setSavepoint() throws SQLException |
226 | |
{ |
227 | 0 | return con.setSavepoint(); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public void releaseSavepoint(Savepoint savepoint) throws SQLException |
236 | |
{ |
237 | 0 | con.releaseSavepoint(savepoint); |
238 | 0 | } |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public void rollback(Savepoint savepoint) throws SQLException |
246 | |
{ |
247 | 0 | con.rollback(); |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public Statement createStatement() throws SQLException |
256 | |
{ |
257 | 0 | Statement st = con.createStatement(); |
258 | 0 | return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(), |
259 | |
new Class[]{Statement.class}, new StatementInvocationHandler(st)); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException |
268 | |
{ |
269 | 0 | Statement st = con.createStatement(resultSetType, resultSetConcurrency); |
270 | 0 | return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(), |
271 | |
new Class[]{Statement.class}, new StatementInvocationHandler(st)); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) |
280 | |
throws SQLException |
281 | |
{ |
282 | 0 | Statement st = con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); |
283 | 0 | return (Statement)Proxy.newProxyInstance(Statement.class.getClassLoader(), |
284 | |
new Class[]{Statement.class}, new StatementInvocationHandler(st)); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public Map getTypeMap() throws SQLException |
293 | |
{ |
294 | 0 | return con.getTypeMap(); |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
public void setTypeMap(Map map) throws SQLException |
303 | |
{ |
304 | 0 | con.setTypeMap(map); |
305 | 0 | } |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public String nativeSQL(String sql) throws SQLException |
313 | |
{ |
314 | 0 | return con.nativeSQL(sql); |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public CallableStatement prepareCall(String sql) throws SQLException |
323 | |
{ |
324 | 0 | CallableStatement cs = con.prepareCall(sql); |
325 | 0 | return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(), |
326 | |
new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs)); |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) |
335 | |
throws SQLException |
336 | |
{ |
337 | 0 | CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency); |
338 | 0 | return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(), |
339 | |
new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs)); |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public CallableStatement prepareCall(String sql, |
348 | |
int resultSetType, |
349 | |
int resultSetConcurrency, |
350 | |
int resultSetHoldability) throws SQLException |
351 | |
{ |
352 | 0 | CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); |
353 | 0 | return (CallableStatement)Proxy.newProxyInstance(CallableStatement.class.getClassLoader(), |
354 | |
new Class[]{CallableStatement.class}, new StatementInvocationHandler(cs)); |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public PreparedStatement prepareStatement(String sql) throws SQLException |
363 | |
{ |
364 | 0 | PreparedStatement ps = con.prepareStatement(sql); |
365 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
366 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException |
375 | |
{ |
376 | 0 | PreparedStatement ps = con.prepareStatement(sql, autoGeneratedKeys); |
377 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
378 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
379 | |
} |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) |
387 | |
throws SQLException |
388 | |
{ |
389 | 0 | PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency); |
390 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
391 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
public PreparedStatement prepareStatement(String sql, |
400 | |
int resultSetType, |
401 | |
int resultSetConcurrency, |
402 | |
int resultSetHoldability) throws SQLException |
403 | |
{ |
404 | 0 | PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency, |
405 | |
resultSetHoldability); |
406 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
407 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException |
416 | |
{ |
417 | 0 | PreparedStatement ps = con.prepareStatement(sql, columnIndexes); |
418 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
419 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
420 | |
} |
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
public Savepoint setSavepoint(String name) throws SQLException |
428 | |
{ |
429 | 0 | return con.setSavepoint(name); |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException |
439 | |
{ |
440 | 0 | PreparedStatement ps = con.prepareStatement(sql, columnNames); |
441 | 0 | return (PreparedStatement)Proxy.newProxyInstance(PreparedStatement.class.getClassLoader(), |
442 | |
new Class[]{PreparedStatement.class}, new StatementInvocationHandler(ps)); |
443 | |
} |
444 | |
|
445 | |
protected void enlist() throws Exception |
446 | |
{ |
447 | 0 | if (tm != null && tx == null) |
448 | |
{ |
449 | 0 | tx = tm.getTransaction(); |
450 | 0 | if (tx != null) |
451 | |
{ |
452 | 0 | tx.enlistResource(xaCon.getXAResource()); |
453 | |
} |
454 | |
} |
455 | 0 | } |
456 | |
|
457 | |
protected class StatementInvocationHandler implements InvocationHandler |
458 | |
{ |
459 | |
|
460 | |
private Statement statement; |
461 | |
|
462 | |
public StatementInvocationHandler(Statement statement) |
463 | 0 | { |
464 | 0 | this.statement = statement; |
465 | 0 | } |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
474 | |
{ |
475 | 0 | if (method.getName().startsWith("execute")) |
476 | |
{ |
477 | 0 | enlist(); |
478 | |
} |
479 | |
try |
480 | |
{ |
481 | 0 | return method.invoke(statement, args); |
482 | |
} |
483 | 0 | catch (InvocationTargetException ex) |
484 | |
{ |
485 | 0 | throw ex.getTargetException(); |
486 | |
} |
487 | |
} |
488 | |
|
489 | |
} |
490 | |
|
491 | |
} |