1 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 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 55 public int getHoldability() throws SQLException 56 { 57 return con.getHoldability(); 58 } 59 60 65 public int getTransactionIsolation() throws SQLException 66 { 67 return con.getTransactionIsolation(); 68 } 69 70 75 public void clearWarnings() throws SQLException 76 { 77 con.clearWarnings(); 78 } 79 80 85 public void close() throws SQLException 86 { 87 con.close(); 88 } 89 90 95 public void commit() throws SQLException 96 { 97 con.commit(); 98 } 99 100 105 public void rollback() throws SQLException 106 { 107 con.rollback(); 108 } 109 110 115 public boolean getAutoCommit() throws SQLException 116 { 117 return con.getAutoCommit(); 118 } 119 120 125 public boolean isClosed() throws SQLException 126 { 127 return con.isClosed(); 128 } 129 130 135 public boolean isReadOnly() throws SQLException 136 { 137 return con.isReadOnly(); 138 } 139 140 145 public void setHoldability(int holdability) throws SQLException 146 { 147 con.setHoldability(holdability); 148 } 149 150 155 public void setTransactionIsolation(int level) throws SQLException 156 { 157 con.setTransactionIsolation(level); 158 } 159 160 165 public void setAutoCommit(boolean autoCommit) throws SQLException 166 { 167 con.setAutoCommit(autoCommit); 168 } 169 170 175 public void setReadOnly(boolean readOnly) throws SQLException 176 { 177 con.setReadOnly(readOnly); 178 } 179 180 185 public String getCatalog() throws SQLException 186 { 187 return con.getCatalog(); 188 } 189 190 195 public void setCatalog(String catalog) throws SQLException 196 { 197 con.setCatalog(catalog); 198 } 199 200 205 public DatabaseMetaData getMetaData() throws SQLException 206 { 207 return con.getMetaData(); 208 } 209 210 215 public SQLWarning getWarnings() throws SQLException 216 { 217 return con.getWarnings(); 218 } 219 220 225 public Savepoint setSavepoint() throws SQLException 226 { 227 return con.setSavepoint(); 228 } 229 230 235 public void releaseSavepoint(Savepoint savepoint) throws SQLException 236 { 237 con.releaseSavepoint(savepoint); 238 } 239 240 245 public void rollback(Savepoint savepoint) throws SQLException 246 { 247 con.rollback(); 248 } 249 250 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 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 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 292 public Map getTypeMap() throws SQLException 293 { 294 return con.getTypeMap(); 295 } 296 297 302 public void setTypeMap(Map map) throws SQLException 303 { 304 con.setTypeMap(map); 305 } 306 307 312 public String nativeSQL(String sql) throws SQLException 313 { 314 return con.nativeSQL(sql); 315 } 316 317 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 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 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 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 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 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 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 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 427 public Savepoint setSavepoint(String name) throws SQLException 428 { 429 return con.setSavepoint(name); 430 } 431 432 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 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 } 492 | Popular Tags |