1 22 package org.jboss.resource.adapter.jdbc; 23 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.SQLWarning ; 31 import java.sql.Savepoint ; 32 import java.sql.Statement ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.util.NestedSQLException; 39 40 47 public class WrappedConnection implements Connection 48 { 49 private static final Logger log = Logger.getLogger(WrappedConnection.class); 50 51 private BaseWrapperManagedConnection mc; 52 53 private WrapperDataSource dataSource; 54 55 private HashMap statements; 56 57 private boolean closed = false; 58 59 private int trackStatements; 60 61 public WrappedConnection(final BaseWrapperManagedConnection mc) 62 { 63 this.mc = mc; 64 if (mc != null) 65 trackStatements = mc.getTrackStatements(); 66 } 67 68 void setManagedConnection(final BaseWrapperManagedConnection mc) 69 { 70 this.mc = mc; 71 if (mc != null) 72 trackStatements = mc.getTrackStatements(); 73 } 74 75 public WrapperDataSource getDataSource() 76 { 77 return dataSource; 78 } 79 80 protected void setDataSource(WrapperDataSource dataSource) 81 { 82 this.dataSource = dataSource; 83 } 84 85 public void setReadOnly(boolean readOnly) throws SQLException 86 { 87 checkStatus(); 88 mc.setJdbcReadOnly(readOnly); 89 } 90 91 public boolean isReadOnly() throws SQLException 92 { 93 checkStatus(); 94 return mc.isJdbcReadOnly(); 95 } 96 97 public void close() throws SQLException 98 { 99 closed = true; 100 if (mc != null) 101 { 102 if (trackStatements != BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 103 { 104 synchronized (this) 105 { 106 if (statements != null) 107 { 108 for (Iterator i = statements.entrySet().iterator(); i.hasNext(); ) 109 { 110 Map.Entry entry = (Map.Entry ) i.next(); 111 WrappedStatement ws = (WrappedStatement) entry.getKey(); 112 if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT) 113 { 114 Throwable stackTrace = (Throwable ) entry.getValue(); 115 log.warn("Closing a statement you left open, please do your own housekeeping", stackTrace); 116 } 117 try 118 { 119 ws.internalClose(); 120 } 121 catch (Throwable t) 122 { 123 log.warn("Exception trying to close statement:", t); 124 } 125 } 126 } 127 } 128 } 129 mc.closeHandle(this); 130 } 131 mc = null; 132 dataSource = null; 133 } 134 135 public boolean isClosed() throws SQLException 136 { 137 return closed; 138 } 139 140 public Statement createStatement() throws SQLException 141 { 142 checkTransaction(); 143 try 144 { 145 return new WrappedStatement(this, mc.getConnection().createStatement()); 146 } 147 catch (Throwable t) 148 { 149 throw checkException(t); 150 } 151 } 152 153 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException 154 { 155 checkTransaction(); 156 try 157 { 158 return new WrappedStatement(this, mc.getConnection().createStatement(resultSetType, resultSetConcurrency)); 159 } 160 catch (Throwable t) 161 { 162 throw checkException(t); 163 } 164 } 165 166 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) 167 throws SQLException 168 { 169 170 checkTransaction(); 171 try 172 { 173 return new WrappedStatement(this, mc.getConnection() 174 .createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)); 175 } 176 catch (Throwable t) 177 { 178 throw checkException(t); 179 } 180 } 181 182 public PreparedStatement prepareStatement(String sql) throws SQLException 183 { 184 checkTransaction(); 185 try 186 { 187 return new WrappedPreparedStatement(this, mc.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); 188 } 189 catch (Throwable t) 190 { 191 throw checkException(t); 192 } 193 } 194 195 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 196 throws SQLException 197 { 198 checkTransaction(); 199 try 200 { 201 return new WrappedPreparedStatement(this, mc.prepareStatement(sql, resultSetType, resultSetConcurrency)); 202 } 203 catch (Throwable t) 204 { 205 throw checkException(t); 206 } 207 } 208 209 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, 210 int resultSetHoldability) throws SQLException 211 { 212 checkTransaction(); 213 try 214 { 215 return new WrappedPreparedStatement(this, mc.getConnection() 216 .prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); 217 } 218 catch (Throwable t) 219 { 220 throw checkException(t); 221 } 222 } 223 224 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException 225 { 226 checkTransaction(); 227 try 228 { 229 return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, autoGeneratedKeys)); 230 } 231 catch (Throwable t) 232 { 233 throw checkException(t); 234 } 235 } 236 237 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException 238 { 239 checkTransaction(); 240 try 241 { 242 return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, columnIndexes)); 243 } 244 catch (Throwable t) 245 { 246 throw checkException(t); 247 } 248 } 249 250 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException 251 { 252 253 checkTransaction(); 254 try 255 { 256 return new WrappedPreparedStatement(this, mc.getConnection().prepareStatement(sql, columnNames)); 257 } 258 catch (Throwable t) 259 { 260 throw checkException(t); 261 } 262 } 263 264 public CallableStatement prepareCall(String sql) throws SQLException 265 { 266 checkTransaction(); 267 try 268 { 269 return new WrappedCallableStatement(this, mc.prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); 270 } 271 catch (Throwable t) 272 { 273 throw checkException(t); 274 } 275 } 276 277 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException 278 { 279 checkTransaction(); 280 try 281 { 282 return new WrappedCallableStatement(this, mc.prepareCall(sql, resultSetType, resultSetConcurrency)); 283 } 284 catch (Throwable t) 285 { 286 throw checkException(t); 287 } 288 } 289 290 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, 291 int resultSetHoldability) throws SQLException 292 { 293 294 checkTransaction(); 295 try 296 { 297 return new WrappedCallableStatement(this, mc.getConnection() 298 .prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); 299 } 300 catch (Throwable t) 301 { 302 throw checkException(t); 303 } 304 } 305 306 public String nativeSQL(String sql) throws SQLException 307 { 308 checkTransaction(); 309 try 310 { 311 return mc.getConnection().nativeSQL(sql); 312 } 313 catch (Throwable t) 314 { 315 throw checkException(t); 316 } 317 } 318 319 public void setAutoCommit(boolean autocommit) throws SQLException 320 { 321 checkStatus(); 322 mc.setJdbcAutoCommit(autocommit); 323 } 324 325 public boolean getAutoCommit() throws SQLException 326 { 327 checkStatus(); 328 return mc.isJdbcAutoCommit(); 329 } 330 331 public void commit() throws SQLException 332 { 333 checkTransaction(); 334 mc.jdbcCommit(); 335 } 336 337 public void rollback() throws SQLException 338 { 339 checkTransaction(); 340 mc.jdbcRollback(); 341 } 342 343 public void rollback(Savepoint savepoint) throws SQLException 344 { 345 checkTransaction(); 346 mc.jdbcRollback(savepoint); 347 } 348 349 public DatabaseMetaData getMetaData() throws SQLException 350 { 351 checkTransaction(); 352 try 353 { 354 return mc.getConnection().getMetaData(); 355 } 356 catch (Throwable t) 357 { 358 throw checkException(t); 359 } 360 } 361 362 public void setCatalog(String catalog) throws SQLException 363 { 364 checkTransaction(); 365 try 366 { 367 mc.getConnection().setCatalog(catalog); 368 } 369 catch (Throwable t) 370 { 371 throw checkException(t); 372 } 373 } 374 public String getCatalog() throws SQLException 375 { 376 checkTransaction(); 377 try 378 { 379 return mc.getConnection().getCatalog(); 380 } 381 catch (Throwable t) 382 { 383 throw checkException(t); 384 } 385 } 386 387 public void setTransactionIsolation(int isolationLevel) throws SQLException 388 { 389 checkStatus(); 390 mc.setJdbcTransactionIsolation(isolationLevel); 391 } 392 393 public int getTransactionIsolation() throws SQLException 394 { 395 checkStatus(); 396 return mc.getJdbcTransactionIsolation(); 397 } 398 399 public SQLWarning getWarnings() throws SQLException 400 { 401 checkTransaction(); 402 try 403 { 404 return mc.getConnection().getWarnings(); 405 } 406 catch (Throwable t) 407 { 408 throw checkException(t); 409 } 410 } 411 412 public void clearWarnings() throws SQLException 413 { 414 checkTransaction(); 415 try 416 { 417 mc.getConnection().clearWarnings(); 418 } 419 catch (Throwable t) 420 { 421 throw checkException(t); 422 } 423 } 424 425 public Map getTypeMap() throws SQLException 426 { 427 checkTransaction(); 428 try 429 { 430 return mc.getConnection().getTypeMap(); 431 } 432 catch (Throwable t) 433 { 434 throw checkException(t); 435 } 436 } 437 438 public void setTypeMap(Map typeMap) throws SQLException 439 { 440 checkTransaction(); 441 try 442 { 443 mc.getConnection().setTypeMap(typeMap); 444 } 445 catch (Throwable t) 446 { 447 throw checkException(t); 448 } 449 } 450 451 public void setHoldability(int holdability) throws SQLException 452 { 453 checkTransaction(); 454 try 455 { 456 mc.getConnection().setHoldability(holdability); 457 } 458 catch (Throwable t) 459 { 460 throw checkException(t); 461 } 462 } 463 464 public int getHoldability() throws SQLException 465 { 466 checkTransaction(); 467 try 468 { 469 return mc.getConnection().getHoldability(); 470 } 471 catch (Throwable t) 472 { 473 throw checkException(t); 474 } 475 } 476 477 public Savepoint setSavepoint() throws SQLException 478 { 479 checkTransaction(); 480 try 481 { 482 return mc.getConnection().setSavepoint(); 483 } 484 catch (Throwable t) 485 { 486 throw checkException(t); 487 } 488 } 489 490 public Savepoint setSavepoint(String name) throws SQLException 491 { 492 checkTransaction(); 493 try 494 { 495 return mc.getConnection().setSavepoint(name); 496 } 497 catch (Throwable t) 498 { 499 throw checkException(t); 500 } 501 } 502 503 public void releaseSavepoint(Savepoint savepoint) throws SQLException 504 { 505 checkTransaction(); 506 try 507 { 508 mc.getConnection().releaseSavepoint(savepoint); 509 } 510 catch (Throwable t) 511 { 512 throw checkException(t); 513 } 514 } 515 516 public Connection getUnderlyingConnection() throws SQLException 517 { 518 checkTransaction(); 519 return mc.getConnection(); 520 } 521 522 void checkTransaction() throws SQLException 523 { 524 checkStatus(); 525 mc.checkTransaction(); 526 } 527 528 534 protected void checkStatus() throws SQLException 535 { 536 if (closed) 537 throw new SQLException ("Connection handle has been closed and is unusable"); 538 if (mc == null) 539 throw new SQLException ("Connection handle is not currently associated with a ManagedConnection"); 540 } 541 542 550 protected SQLException checkException(Throwable t) throws SQLException 551 { 552 Throwable result = null; 553 554 if (mc != null) 555 result = mc.connectionError(t); 556 557 if (result instanceof SQLException ) 558 { 559 throw (SQLException ) result; 560 } 561 else 562 { 563 throw new NestedSQLException("Error", result); 564 } 565 566 } 567 568 int getTrackStatements() 569 { 570 return trackStatements; 571 } 572 573 void registerStatement(WrappedStatement ws) 574 { 575 if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 576 return; 577 578 synchronized (this) 579 { 580 if (statements == null) 581 statements = new HashMap (); 582 583 if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT) 584 statements.put(ws, new Throwable ("STACKTRACE")); 585 else 586 statements.put(ws, null); 587 } 588 } 589 590 void unregisterStatement(WrappedStatement ws) 591 { 592 if (trackStatements == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 593 return; 594 synchronized (this) 595 { 596 if (statements != null) 597 statements.remove(ws); 598 } 599 } 600 601 void checkConfiguredQueryTimeout(WrappedStatement ws) throws SQLException 602 { 603 if (mc == null || dataSource == null) 604 return; 605 606 int timeout = 0; 607 608 if (mc.isTransactionQueryTimeout()) 610 timeout = dataSource.getTimeLeftBeforeTransactionTimeout(); 611 612 if (timeout <= 0) 614 timeout = mc.getQueryTimeout(); 615 616 if (timeout > 0) 617 ws.setQueryTimeout(timeout); 618 } 619 620 Logger getLogger() 621 { 622 return log; 623 } 624 } 625 | Popular Tags |