1 22 23 package org.cofax.connectionpool; 24 25 import java.sql.*; 26 import java.util.*; 27 28 36 public class ConnectionWrapper implements Connection { 37 private static long counter = 0; 38 39 Connection realConn; 42 43 private ConnectionPool pool; 44 45 private boolean isClosed = false; 46 47 private long id; 49 50 private int timesUsed; 52 53 private long timeCreated; 54 55 private long lastTimeUsed; 57 58 private String currentActivity; 60 61 63 71 public ConnectionWrapper(Connection realConn, ConnectionPool pool) { 72 73 this.realConn = realConn; 74 this.pool = pool; 75 timesUsed = 0; 76 counter++; 77 id = counter; 78 java.util.Date currDate = new java.util.Date (); 79 timeCreated = currDate.getTime(); 80 lastTimeUsed = currDate.getTime(); 81 82 } 83 84 87 public void setLastTimeUsed() { 88 89 java.util.Date currDate = new java.util.Date (); 90 lastTimeUsed = currDate.getTime(); 91 92 } 93 94 104 public void setCurrentActivity(String activity) { 105 106 int junkStart = activity.indexOf(" "); 107 if (junkStart != -1) { 108 this.currentActivity = activity.substring(0, junkStart); 109 } else if (activity.length() > 250) { 110 this.currentActivity = activity.substring(0, 250); 111 } else { 112 this.currentActivity = activity; 113 } 114 115 } 116 117 125 public void setHoldability(int holdability) throws SQLException { 126 if (isClosed) { 127 throw new SQLException("Pooled connection is closed"); 128 } 129 realConn.setHoldability(holdability); 130 } 131 132 136 public int getHoldability() throws SQLException { 137 if (isClosed) { 138 throw new SQLException("Pooled connection is closed"); 139 } 140 return realConn.getHoldability(); 141 } 142 143 149 public Savepoint setSavepoint() throws SQLException { 150 if (isClosed) { 151 throw new SQLException("Pooled connection is closed"); 152 } 153 return realConn.setSavepoint(); 154 } 155 156 164 public Savepoint setSavepoint(String savepoint) throws SQLException { 165 if (isClosed) { 166 throw new SQLException("Pooled connection is closed"); 167 } 168 return realConn.setSavepoint(savepoint); 169 } 170 171 179 public void rollback(Savepoint savepoint) throws SQLException { 180 if (isClosed) { 181 throw new SQLException("Pooled connection is closed"); 182 } 183 realConn.rollback(savepoint); 184 } 185 186 208 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 209 if (isClosed) { 210 throw new SQLException("Pooled connection is closed"); 211 } 212 return realConn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); 213 } 214 215 240 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 241 if (isClosed) { 242 throw new SQLException("Pooled connection is closed"); 243 } 244 return realConn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 245 } 246 247 263 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 264 if (isClosed) { 265 throw new SQLException("Pooled connection is closed"); 266 } 267 return realConn.prepareStatement(sql, autoGeneratedKeys); 268 } 269 270 286 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 287 if (isClosed) { 288 throw new SQLException("Pooled connection is closed"); 289 } 290 return realConn.prepareStatement(sql, columnIndexes); 291 } 292 293 309 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 310 if (isClosed) { 311 throw new SQLException("Pooled connection is closed"); 312 } 313 return realConn.prepareStatement(sql, columnNames); 314 } 315 316 341 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { 342 if (isClosed) { 343 throw new SQLException("Pooled connection is closed"); 344 } 345 return realConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); 346 } 347 348 356 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 357 if (isClosed) { 358 throw new SQLException("Pooled connection is closed"); 359 } 360 realConn.releaseSavepoint(savepoint); 361 } 362 363 371 public void setTypeMap(Map map) throws SQLException { 372 if (isClosed) { 373 throw new SQLException("Pooled connection is closed"); 374 } 375 realConn.setTypeMap(map); 376 } 377 378 386 public void setAutoCommit(boolean autoCommit) throws SQLException { 387 if (isClosed) { 388 throw new SQLException("Pooled connection is closed"); 389 } 390 realConn.setAutoCommit(autoCommit); 391 } 392 393 401 public void setCatalog(String catalog) throws SQLException { 402 if (isClosed) { 403 throw new SQLException("Pooled connection is closed"); 404 } 405 realConn.setCatalog(catalog); 406 } 407 408 416 public void setReadOnly(boolean readOnly) throws SQLException { 417 if (isClosed) { 418 throw new SQLException("Pooled connection is closed"); 419 } 420 realConn.setReadOnly(readOnly); 421 } 422 423 431 public void setTransactionIsolation(int level) throws SQLException { 432 if (isClosed) { 433 throw new SQLException("Pooled connection is closed"); 434 } 435 realConn.setTransactionIsolation(level); 436 } 437 438 443 public int getTimesUsed() { 444 445 return timesUsed; 446 } 447 448 453 public long getTimeCreated() { 454 455 return timeCreated; 456 } 457 458 463 public long getLastTimeUsed() { 464 465 return lastTimeUsed; 466 } 467 468 473 public long getConnectionId() { 474 475 return id; 476 } 477 478 483 public String getCurrentActivity() { 484 485 return this.currentActivity; 486 } 487 488 495 public boolean isClosed() throws SQLException { 496 497 return isClosed; 498 } 499 500 507 public boolean getAutoCommit() throws SQLException { 508 if (isClosed) { 509 throw new SQLException("Pooled connection is closed"); 510 } 511 return realConn.getAutoCommit(); 512 } 513 514 521 public String getCatalog() throws SQLException { 522 if (isClosed) { 523 throw new SQLException("Pooled connection is closed"); 524 } 525 return realConn.getCatalog(); 526 } 527 528 535 public DatabaseMetaData getMetaData() throws SQLException { 536 if (isClosed) { 537 throw new SQLException("Pooled connection is closed"); 538 } 539 return realConn.getMetaData(); 540 } 541 542 549 public Map getTypeMap() throws SQLException { 550 if (isClosed) { 551 throw new SQLException("Pooled connection is closed"); 552 } 553 return realConn.getTypeMap(); 554 } 555 556 563 public int getTransactionIsolation() throws SQLException { 564 if (isClosed) { 565 throw new SQLException("Pooled connection is closed"); 566 } 567 return realConn.getTransactionIsolation(); 568 } 569 570 577 public SQLWarning getWarnings() throws SQLException { 578 if (isClosed) { 579 throw new SQLException("Pooled connection is closed"); 580 } 581 return realConn.getWarnings(); 582 } 583 584 591 public boolean isReadOnly() throws SQLException { 592 if (isClosed) { 593 throw new SQLException("Pooled connection is closed"); 594 } 595 return realConn.isReadOnly(); 596 } 597 598 601 public void incrementTimesUsed() { 602 603 timesUsed++; 604 setLastTimeUsed(); 605 606 } 607 608 614 public void close() throws SQLException { 615 616 pool.freeConnection(this); 617 618 } 619 620 626 public void closeConnection() throws SQLException { 627 628 try { 629 realConn.close(); 630 } catch (Exception e) { 631 } finally { 632 realConn = null; 633 isClosed = true; 634 } 635 636 } 637 638 641 647 public void clearWarnings() throws SQLException { 648 if (isClosed) { 649 throw new SQLException("Pooled connection is closed"); 650 } 651 realConn.clearWarnings(); 652 } 653 654 660 public void commit() throws SQLException { 661 if (isClosed) { 662 throw new SQLException("Pooled connection is closed"); 663 } 664 realConn.commit(); 665 } 666 667 674 public Statement createStatement() throws SQLException { 675 if (isClosed) { 676 throw new SQLException("Pooled connection is closed"); 677 } 678 return realConn.createStatement(); 679 } 680 681 692 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 693 if (isClosed) { 694 throw new SQLException("Pooled connection is closed"); 695 } 696 return realConn.createStatement(resultSetType, resultSetConcurrency); 697 } 698 699 708 public String nativeSQL(String sql) throws SQLException { 709 if (isClosed) { 710 throw new SQLException("Pooled connection is closed"); 711 } 712 return realConn.nativeSQL(sql); 713 } 714 715 724 public CallableStatement prepareCall(String sql) throws SQLException { 725 if (isClosed) { 726 throw new SQLException("Pooled connection is closed"); 727 } 728 return realConn.prepareCall(sql); 729 } 730 731 744 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 745 if (isClosed) { 746 throw new SQLException("Pooled connection is closed"); 747 } 748 return realConn.prepareCall(sql, resultSetType, resultSetConcurrency); 749 } 750 751 760 public PreparedStatement prepareStatement(String sql) throws SQLException { 761 if (isClosed) { 762 throw new SQLException("Pooled connection is closed"); 763 } 764 return realConn.prepareStatement(sql); 765 } 766 767 780 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 781 if (isClosed) { 782 throw new SQLException("Pooled connection is closed"); 783 } 784 return realConn.prepareStatement(sql, resultSetType, resultSetConcurrency); 785 } 786 787 793 public void rollback() throws SQLException { 794 if (isClosed) { 795 throw new SQLException("Pooled connection is closed"); 796 } 797 realConn.rollback(); 798 } 799 } 800 | Popular Tags |