1 22 package org.jboss.resource.adapter.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.SQLWarning ; 28 import java.sql.Statement ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 42 public class WrappedStatement implements Statement , StatementAccess, 43 org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement 44 { 45 private final WrappedConnection lc; 46 private final Statement s; 47 48 49 private HashMap resultSets; 50 51 52 private boolean closed = false; 53 54 55 private Object lock = new Object (); 56 57 public WrappedStatement(final WrappedConnection lc, Statement s) 58 { 59 this.lc = lc; 60 this.s = s; 61 lc.registerStatement(this); 62 } 63 64 public void close() throws SQLException 65 { 66 synchronized (lock) 67 { 68 if (closed) 69 return; 70 71 closed = true; 72 } 73 lc.unregisterStatement(this); 74 internalClose(); 75 } 76 77 public boolean execute(String sql) throws SQLException 78 { 79 checkTransaction(); 80 try 81 { 82 checkConfiguredQueryTimeout(); 83 return s.execute(sql); 84 } 85 catch (Throwable t) 86 { 87 throw checkException(t); 88 } 89 } 90 91 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException 92 { 93 checkTransaction(); 94 try 95 { 96 checkConfiguredQueryTimeout(); 97 return s.execute(sql, autoGeneratedKeys); 98 } 99 catch (Throwable t) 100 { 101 throw checkException(t); 102 } 103 } 104 105 public boolean execute(String sql, int[] columnIndexes) throws SQLException 106 { 107 checkTransaction(); 108 try 109 { 110 checkConfiguredQueryTimeout(); 111 return s.execute(sql, columnIndexes); 112 } 113 catch (Throwable t) 114 { 115 throw checkException(t); 116 } 117 } 118 119 public boolean execute(String sql, String []columnNames ) throws SQLException 120 { 121 checkTransaction(); 122 try 123 { 124 checkConfiguredQueryTimeout(); 125 return s.execute(sql, columnNames); 126 } 127 catch (Throwable t) 128 { 129 throw checkException(t); 130 } 131 } 132 133 public Connection getConnection() throws SQLException 134 { 135 return lc; 136 } 137 138 public SQLWarning getWarnings() throws SQLException 139 { 140 checkState(); 141 try 142 { 143 return s.getWarnings(); 144 } 145 catch (Throwable t) 146 { 147 throw checkException(t); 148 } 149 } 150 151 public void clearWarnings() throws SQLException 152 { 153 checkState(); 154 try 155 { 156 s.clearWarnings(); 157 } 158 catch (Throwable t) 159 { 160 throw checkException(t); 161 } 162 } 163 164 public ResultSet executeQuery(String sql) throws SQLException 165 { 166 checkTransaction(); 167 try 168 { 169 checkConfiguredQueryTimeout(); 170 ResultSet result = s.executeQuery(sql); 171 return registerResultSet(result); 172 } 173 catch (Throwable t) 174 { 175 throw checkException(t); 176 } 177 } 178 179 public int executeUpdate(String sql) throws SQLException 180 { 181 checkTransaction(); 182 try 183 { 184 checkConfiguredQueryTimeout(); 185 return s.executeUpdate(sql); 186 } 187 catch (Throwable t) 188 { 189 throw checkException(t); 190 } 191 } 192 193 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException 194 { 195 checkTransaction(); 196 try 197 { 198 checkConfiguredQueryTimeout(); 199 return s.executeUpdate(sql, autoGeneratedKeys); 200 } 201 catch (Throwable t) 202 { 203 throw checkException(t); 204 } 205 } 206 207 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException 208 { 209 checkTransaction(); 210 try 211 { 212 checkConfiguredQueryTimeout(); 213 return s.executeUpdate(sql, columnIndexes); 214 } 215 catch (Throwable t) 216 { 217 throw checkException(t); 218 } 219 } 220 221 public int executeUpdate(String sql, String [] columnNames) throws SQLException 222 { 223 checkTransaction(); 224 try 225 { 226 checkConfiguredQueryTimeout(); 227 return s.executeUpdate(sql, columnNames); 228 } 229 catch (Throwable t) 230 { 231 throw checkException(t); 232 } 233 } 234 235 public int getMaxFieldSize() throws SQLException 236 { 237 checkState(); 238 try 239 { 240 return s.getMaxFieldSize(); 241 } 242 catch (Throwable t) 243 { 244 throw checkException(t); 245 } 246 } 247 248 public void setMaxFieldSize(int max) throws SQLException 249 { 250 checkState(); 251 try 252 { 253 s.setMaxFieldSize(max); 254 } 255 catch (Throwable t) 256 { 257 throw checkException(t); 258 } 259 } 260 261 public int getMaxRows() throws SQLException 262 { 263 checkState(); 264 try 265 { 266 return s.getMaxRows(); 267 } 268 catch (Throwable t) 269 { 270 throw checkException(t); 271 } 272 } 273 274 public void setMaxRows(int max) throws SQLException 275 { 276 checkState(); 277 try 278 { 279 s.setMaxRows(max); 280 } 281 catch (Throwable t) 282 { 283 throw checkException(t); 284 } 285 } 286 287 public void setEscapeProcessing(boolean enable) throws SQLException 288 { 289 checkState(); 290 try 291 { 292 s.setEscapeProcessing(enable); 293 } 294 catch (Throwable t) 295 { 296 throw checkException(t); 297 } 298 } 299 300 public int getQueryTimeout() throws SQLException 301 { 302 checkState(); 303 try 304 { 305 return s.getQueryTimeout(); 306 } 307 catch (Throwable t) 308 { 309 throw checkException(t); 310 } 311 } 312 313 public void setQueryTimeout(int timeout) throws SQLException 314 { 315 checkState(); 316 try 317 { 318 s.setQueryTimeout(timeout); 319 } 320 catch (Throwable t) 321 { 322 throw checkException(t); 323 } 324 } 325 326 public void cancel() throws SQLException 327 { 328 checkState(); 329 try 330 { 331 s.cancel(); 332 } 333 catch (Throwable t) 334 { 335 throw checkException(t); 336 } 337 } 338 339 public void setCursorName(String name) throws SQLException 340 { 341 checkState(); 342 try 343 { 344 s.setCursorName(name); 345 } 346 catch (Throwable t) 347 { 348 throw checkException(t); 349 } 350 } 351 352 public ResultSet getResultSet() throws SQLException 353 { 354 checkState(); 355 try 356 { 357 ResultSet result = s.getResultSet(); 358 if (result == null) 359 return null; 360 else 361 return registerResultSet(result); 362 } 363 catch (Throwable t) 364 { 365 throw checkException(t); 366 } 367 } 368 369 public int getUpdateCount() throws SQLException 370 { 371 checkState(); 372 try 373 { 374 return s.getUpdateCount(); 375 } 376 catch (Throwable t) 377 { 378 throw checkException(t); 379 } 380 } 381 382 public boolean getMoreResults() throws SQLException 383 { 384 checkState(); 385 try 386 { 387 return s.getMoreResults(); 388 } 389 catch (Throwable t) 390 { 391 throw checkException(t); 392 } 393 } 394 395 public boolean getMoreResults(int current) throws SQLException 396 { 397 checkState(); 398 try 399 { 400 return s.getMoreResults(current); 401 } 402 catch (Throwable t) 403 { 404 throw checkException(t); 405 } 406 } 407 408 public void setFetchDirection(int direction) throws SQLException 409 { 410 checkState(); 411 try 412 { 413 s.setFetchDirection(direction); 414 } 415 catch (Throwable t) 416 { 417 throw checkException(t); 418 } 419 } 420 421 public int getFetchDirection() throws SQLException 422 { 423 checkState(); 424 try 425 { 426 return s.getFetchDirection(); 427 } 428 catch (Throwable t) 429 { 430 throw checkException(t); 431 } 432 } 433 434 public void setFetchSize(int rows) throws SQLException 435 { 436 checkState(); 437 try 438 { 439 s.setFetchSize(rows); 440 } 441 catch (Throwable t) 442 { 443 throw checkException(t); 444 } 445 } 446 447 public int getFetchSize() throws SQLException 448 { 449 checkState(); 450 try 451 { 452 return s.getFetchSize(); 453 } 454 catch (Throwable t) 455 { 456 throw checkException(t); 457 } 458 } 459 460 public int getResultSetConcurrency() throws SQLException 461 { 462 checkState(); 463 try 464 { 465 return s.getResultSetConcurrency(); 466 } 467 catch (Throwable t) 468 { 469 throw checkException(t); 470 } 471 } 472 473 public int getResultSetType() throws SQLException 474 { 475 checkState(); 476 try 477 { 478 return s.getResultSetType(); 479 } 480 catch (Throwable t) 481 { 482 throw checkException(t); 483 } 484 } 485 486 public void addBatch(String sql) throws SQLException 487 { 488 checkState(); 489 try 490 { 491 s.addBatch(sql); 492 } 493 catch (Throwable t) 494 { 495 throw checkException(t); 496 } 497 } 498 499 public void clearBatch() throws SQLException 500 { 501 checkState(); 502 try 503 { 504 s.clearBatch(); 505 } 506 catch (Throwable t) 507 { 508 throw checkException(t); 509 } 510 } 511 512 public int[] executeBatch() throws SQLException 513 { 514 checkState(); 515 try 516 { 517 checkConfiguredQueryTimeout(); 518 return s.executeBatch(); 519 } 520 catch (Throwable t) 521 { 522 throw checkException(t); 523 } 524 } 525 526 public ResultSet getGeneratedKeys() throws SQLException 527 { 528 checkState(); 529 try 530 { 531 ResultSet resultSet = s.getGeneratedKeys(); 532 return registerResultSet(resultSet); 533 } 534 catch (Throwable t) 535 { 536 throw checkException(t); 537 } 538 } 539 540 public int getResultSetHoldability() throws SQLException 541 { 542 checkState(); 543 try 544 { 545 return s.getResultSetHoldability(); 546 } 547 catch (Throwable t) 548 { 549 throw checkException(t); 550 } 551 } 552 553 public Statement getUnderlyingStatement() throws SQLException 554 { 555 checkState(); 556 return s; 557 } 558 559 protected SQLException checkException(Throwable t) 560 throws SQLException 561 { 562 throw lc.checkException(t); 563 } 564 565 protected void checkTransaction() 566 throws SQLException 567 { 568 checkState(); 569 lc.checkTransaction(); 570 } 571 572 protected void checkConfiguredQueryTimeout() throws SQLException 573 { 574 lc.checkConfiguredQueryTimeout(this); 575 } 576 577 protected void internalClose() throws SQLException 578 { 579 synchronized (lock) 580 { 581 closed = true; 582 } 583 try 584 { 585 closeResultSets(); 586 } 587 finally 588 { 589 s.close(); 590 } 591 } 592 593 void checkState() throws SQLException 594 { 595 synchronized (lock) 596 { 597 if (closed) 598 throw new SQLException ("The statement is closed."); 599 } 600 } 601 602 protected ResultSet registerResultSet(ResultSet resultSet) 603 { 604 if (resultSet != null) 605 resultSet = new WrappedResultSet(this, resultSet); 606 607 if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 608 return resultSet; 609 610 synchronized (this) 611 { 612 if (resultSets == null) 613 resultSets = new HashMap (); 614 615 if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT) 616 resultSets.put(resultSet, new Throwable ("STACKTRACE")); 617 else 618 resultSets.put(resultSet, null); 619 } 620 return resultSet; 621 } 622 623 protected void unregisterResultSet(WrappedResultSet resultSet) 624 { 625 if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 626 return; 627 628 synchronized (this) 629 { 630 if (resultSets != null) 631 resultSets.remove(resultSet); 632 } 633 } 634 635 protected void closeResultSets() 636 { 637 if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_FALSE_INT) 638 return; 639 640 synchronized (this) 641 { 642 if (resultSets == null) 643 return; 644 for (Iterator i = resultSets.entrySet().iterator(); i.hasNext();) 645 { 646 Map.Entry entry = (Map.Entry ) i.next(); 647 WrappedResultSet resultSet = (WrappedResultSet) entry.getKey(); 648 if (lc.getTrackStatements() == BaseWrapperManagedConnectionFactory.TRACK_STATEMENTS_TRUE_INT) 649 { 650 Throwable stackTrace = (Throwable ) entry.getValue(); 651 lc.getLogger().warn("Closing a result set you left open! Please close it yourself.", stackTrace); 652 } 653 try 654 { 655 resultSet.internalClose(); 656 } 657 catch (Throwable t) 658 { 659 lc.getLogger().warn("Error closing a result set you left open! Please close it yourself.", t); 660 } 661 } 662 resultSets.clear(); 663 } 664 } 665 } 666 | Popular Tags |