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 {
44 this.xaCon = xaCon;
45 this.con = xaCon.getConnection();
46 this.tm = tm;
47 this.tx = null;
48 }
49
50
51
52
53
54
55 public int getHoldability() throws SQLException
56 {
57 return con.getHoldability();
58 }
59
60
61
62
63
64
65 public int getTransactionIsolation() throws SQLException
66 {
67 return con.getTransactionIsolation();
68 }
69
70
71
72
73
74
75 public void clearWarnings() throws SQLException
76 {
77 con.clearWarnings();
78 }
79
80
81
82
83
84
85 public void close() throws SQLException
86 {
87 con.close();
88 }
89
90
91
92
93
94
95 public void commit() throws SQLException
96 {
97 con.commit();
98 }
99
100
101
102
103
104
105 public void rollback() throws SQLException
106 {
107 con.rollback();
108 }
109
110
111
112
113
114
115 public boolean getAutoCommit() throws SQLException
116 {
117 return con.getAutoCommit();
118 }
119
120
121
122
123
124
125 public boolean isClosed() throws SQLException
126 {
127 return con.isClosed();
128 }
129
130
131
132
133
134
135 public boolean isReadOnly() throws SQLException
136 {
137 return con.isReadOnly();
138 }
139
140
141
142
143
144
145 public void setHoldability(int holdability) throws SQLException
146 {
147 con.setHoldability(holdability);
148 }
149
150
151
152
153
154
155 public void setTransactionIsolation(int level) throws SQLException
156 {
157 con.setTransactionIsolation(level);
158 }
159
160
161
162
163
164
165 public void setAutoCommit(boolean autoCommit) throws SQLException
166 {
167 con.setAutoCommit(autoCommit);
168 }
169
170
171
172
173
174
175 public void setReadOnly(boolean readOnly) throws SQLException
176 {
177 con.setReadOnly(readOnly);
178 }
179
180
181
182
183
184
185 public String getCatalog() throws SQLException
186 {
187 return con.getCatalog();
188 }
189
190
191
192
193
194
195 public void setCatalog(String catalog) throws SQLException
196 {
197 con.setCatalog(catalog);
198 }
199
200
201
202
203
204
205 public DatabaseMetaData getMetaData() throws SQLException
206 {
207 return con.getMetaData();
208 }
209
210
211
212
213
214
215 public SQLWarning getWarnings() throws SQLException
216 {
217 return con.getWarnings();
218 }
219
220
221
222
223
224
225 public Savepoint setSavepoint() throws SQLException
226 {
227 return con.setSavepoint();
228 }
229
230
231
232
233
234
235 public void releaseSavepoint(Savepoint savepoint) throws SQLException
236 {
237 con.releaseSavepoint(savepoint);
238 }
239
240
241
242
243
244
245 public void rollback(Savepoint savepoint) throws SQLException
246 {
247 con.rollback();
248 }
249
250
251
252
253
254
255 public Statement createStatement() throws SQLException
256 {
257 Statement st = con.createStatement();
258 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 Statement st = con.createStatement(resultSetType, resultSetConcurrency);
270 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 Statement st = con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
283 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 return con.getTypeMap();
295 }
296
297
298
299
300
301
302 public void setTypeMap(Map map) throws SQLException
303 {
304 con.setTypeMap(map);
305 }
306
307
308
309
310
311
312 public String nativeSQL(String sql) throws SQLException
313 {
314 return con.nativeSQL(sql);
315 }
316
317
318
319
320
321
322 public CallableStatement prepareCall(String sql) throws SQLException
323 {
324 CallableStatement cs = con.prepareCall(sql);
325 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 CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency);
338 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 CallableStatement cs = con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
353 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 PreparedStatement ps = con.prepareStatement(sql);
365 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 PreparedStatement ps = con.prepareStatement(sql, autoGeneratedKeys);
377 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 PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency);
390 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 PreparedStatement ps = con.prepareStatement(sql, resultSetType, resultSetConcurrency,
405 resultSetHoldability);
406 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 PreparedStatement ps = con.prepareStatement(sql, columnIndexes);
418 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 return con.setSavepoint(name);
430 }
431
432
433
434
435
436
437
438 public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
439 {
440 PreparedStatement ps = con.prepareStatement(sql, columnNames);
441 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 if (tm != null && tx == null)
448 {
449 tx = tm.getTransaction();
450 if (tx != null)
451 {
452 tx.enlistResource(xaCon.getXAResource());
453 }
454 }
455 }
456
457 protected class StatementInvocationHandler implements InvocationHandler
458 {
459
460 private Statement statement;
461
462 public StatementInvocationHandler(Statement statement)
463 {
464 this.statement = statement;
465 }
466
467
468
469
470
471
472
473 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
474 {
475 if (method.getName().startsWith("execute"))
476 {
477 enlist();
478 }
479 try
480 {
481 return method.invoke(statement, args);
482 }
483 catch (InvocationTargetException ex)
484 {
485 throw ex.getTargetException();
486 }
487 }
488
489 }
490
491 }