1 23 24 package com.sun.gjc.spi; 25 26 import java.sql.*; 27 import java.util.Hashtable ; 28 import java.util.Map ; 29 import java.util.Enumeration ; 30 import com.sun.enterprise.util.i18n.StringManager; 31 import com.sun.gjc.common.DataSourceObjectBuilder; 32 import javax.resource.ResourceException ; 33 34 41 public class ConnectionHolder implements Connection{ 42 43 protected Connection con; 44 45 protected ManagedConnection mc; 46 47 protected boolean wrappedAlready = false; 48 49 protected boolean isClosed = false; 50 51 protected boolean valid = true; 52 53 protected boolean active = false; 54 55 private javax.resource.spi.LazyAssociatableConnectionManager lazyAssocCm_; 56 private javax.resource.spi.LazyEnlistableConnectionManager lazyEnlistCm_; 57 58 private javax.resource.spi.ConnectionRequestInfo cxReqInfo_; 59 60 private javax.resource.spi.ManagedConnectionFactory mcf_; 61 62 public static enum ConnectionType { LAZY_ENLISTABLE, LAZY_ASSOCIATABLE, STANDARD }; 63 64 private ConnectionType myType_ = ConnectionType.STANDARD; 65 66 76 77 static protected StringManager sm = StringManager.getManager( 78 DataSourceObjectBuilder.class); 79 80 81 86 public ConnectionHolder(Connection con, ManagedConnection mc, 87 javax.resource.spi.ConnectionRequestInfo cxRequestInfo ) { 88 this.con = con; 89 this.mc = mc; 90 mcf_ = mc.mcf; 91 cxReqInfo_ = cxRequestInfo; 92 } 93 94 99 Connection getConnection() { 100 return con; 101 } 102 103 108 void wrapped(boolean wrapFlag){ 109 this.wrappedAlready = wrapFlag; 110 } 111 112 117 boolean isWrapped(){ 118 return wrappedAlready; 119 } 120 121 127 ManagedConnection getManagedConnection() { 128 return mc; 129 } 130 131 138 void associateConnection(Connection con, ManagedConnection mc) { 139 this.mc = mc; 140 this.con = con; 141 } 142 143 148 public void clearWarnings() throws SQLException{ 149 checkValidity(); 150 con.clearWarnings(); 151 } 152 153 158 public void close() throws SQLException{ 159 isClosed = true; 160 if ( mc != null ) { 161 mc.connectionClosed(null, this); 164 } 165 } 166 167 170 public void invalidate() { 171 valid = false; 172 } 173 174 179 void actualClose() throws SQLException{ 180 con.close(); 181 } 182 183 188 public void commit() throws SQLException { 189 checkValidity(); 190 con.commit(); 191 } 192 193 199 public Statement createStatement() throws SQLException { 200 checkValidity(); 201 jdbcPreInvoke(); 202 return con.createStatement(); 203 } 204 205 213 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 214 checkValidity(); 215 jdbcPreInvoke(); 216 return con.createStatement(resultSetType, resultSetConcurrency); 217 } 218 219 228 public Statement createStatement(int resultSetType, int resultSetConcurrency, 229 int resultSetHoldabilty) throws SQLException { 230 checkValidity(); 231 jdbcPreInvoke(); 232 return con.createStatement(resultSetType, resultSetConcurrency, 233 resultSetHoldabilty); 234 } 235 236 242 public boolean getAutoCommit() throws SQLException { 243 checkValidity(); 244 return con.getAutoCommit(); 245 } 246 247 253 public String getCatalog() throws SQLException { 254 checkValidity(); 255 return con.getCatalog(); 256 } 257 258 265 public int getHoldability() throws SQLException { 266 checkValidity(); 267 return con.getHoldability(); 268 } 269 270 277 public DatabaseMetaData getMetaData() throws SQLException { 278 checkValidity(); 279 return con.getMetaData(); 280 } 281 282 288 public int getTransactionIsolation() throws SQLException { 289 checkValidity(); 290 return con.getTransactionIsolation(); 291 } 292 293 300 public Map getTypeMap() throws SQLException { 301 checkValidity(); 302 return con.getTypeMap(); 303 } 304 305 312 public SQLWarning getWarnings() throws SQLException { 313 checkValidity(); 314 return con.getWarnings(); 315 } 316 317 324 public boolean isClosed() throws SQLException { 325 return isClosed; 326 } 327 328 334 public boolean isReadOnly() throws SQLException { 335 checkValidity(); 336 return con.isReadOnly(); 337 } 338 339 346 public String nativeSQL(String sql) throws SQLException { 347 checkValidity(); 348 return con.nativeSQL(sql); 349 } 350 351 359 public CallableStatement prepareCall(String sql) throws SQLException { 360 checkValidity(); 361 jdbcPreInvoke(); 362 return con.prepareCall(sql); 363 } 364 365 375 public CallableStatement prepareCall(String sql,int resultSetType, 376 int resultSetConcurrency) throws SQLException{ 377 checkValidity(); 378 jdbcPreInvoke(); 379 return con.prepareCall(sql, resultSetType, resultSetConcurrency); 380 } 381 382 393 public CallableStatement prepareCall(String sql, int resultSetType, 394 int resultSetConcurrency, 395 int resultSetHoldabilty) throws SQLException{ 396 checkValidity(); 397 jdbcPreInvoke(); 398 return con.prepareCall(sql, resultSetType, resultSetConcurrency, 399 resultSetHoldabilty); 400 } 401 402 410 public PreparedStatement prepareStatement(String sql) throws SQLException { 411 checkValidity(); 412 jdbcPreInvoke(); 413 return con.prepareStatement(sql); 414 } 415 416 425 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 426 checkValidity(); 427 jdbcPreInvoke(); 428 return con.prepareStatement(sql,autoGeneratedKeys); 429 } 430 431 441 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 442 checkValidity(); 443 jdbcPreInvoke(); 444 return con.prepareStatement(sql,columnIndexes); 445 } 446 447 457 public PreparedStatement prepareStatement(String sql,int resultSetType, 458 int resultSetConcurrency) throws SQLException{ 459 checkValidity(); 460 jdbcPreInvoke(); 461 return con.prepareStatement(sql, resultSetType, resultSetConcurrency); 462 } 463 464 475 public PreparedStatement prepareStatement(String sql, int resultSetType, 476 int resultSetConcurrency, 477 int resultSetHoldabilty) throws SQLException { 478 checkValidity(); 479 jdbcPreInvoke(); 480 return con.prepareStatement(sql, resultSetType, resultSetConcurrency, 481 resultSetHoldabilty); 482 } 483 484 493 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 494 checkValidity(); 495 jdbcPreInvoke(); 496 return con.prepareStatement(sql,columnNames); 497 } 498 499 505 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 506 checkValidity(); 507 con.releaseSavepoint(savepoint); 508 } 509 510 515 public void rollback() throws SQLException { 516 checkValidity(); 517 con.rollback(); 518 } 519 520 525 public void rollback(Savepoint savepoint) throws SQLException { 526 checkValidity(); 527 con.rollback(savepoint); 528 } 529 530 536 public void setAutoCommit(boolean autoCommit) throws SQLException { 537 checkValidity(); 538 con.setAutoCommit(autoCommit); 539 mc.setLastAutoCommitValue(autoCommit); 540 } 541 542 548 public void setCatalog(String catalog) throws SQLException { 549 checkValidity(); 550 con.setCatalog(catalog); 551 } 552 553 560 public void setHoldability(int holdability) throws SQLException { 561 checkValidity(); 562 con.setHoldability(holdability); 563 } 564 565 572 public void setReadOnly(boolean readOnly) throws SQLException { 573 checkValidity(); 574 con.setReadOnly(readOnly); 575 } 576 577 583 public Savepoint setSavepoint() throws SQLException { 584 checkValidity(); 585 return con.setSavepoint(); 586 } 587 588 595 public Savepoint setSavepoint(String name) throws SQLException { 596 checkValidity(); 597 return con.setSavepoint(name); 598 } 599 600 606 public void setTransactionIsolation(int level) throws SQLException { 607 checkValidity(); 608 con.setTransactionIsolation(level); 609 } 610 611 618 public void setTypeMap(Map map) throws SQLException { 619 checkValidity(); 620 con.setTypeMap(map); 621 } 622 623 626 protected void checkValidity() throws SQLException { 627 if (isClosed) throw new SQLException ("Connection closed"); 628 if (!valid) throw new SQLException ("Invalid Connection"); 629 if(active == false) { 630 mc.checkIfActive(this); 631 } 632 } 633 634 639 void setActive(boolean actv) { 640 active = actv; 641 } 642 643 647 protected void jdbcPreInvoke() throws SQLException { 648 if ( myType_ == ConnectionType.LAZY_ASSOCIATABLE ) { 649 performLazyAssociation(); 650 } else if ( myType_ == ConnectionType.LAZY_ENLISTABLE ) { 651 performLazyEnlistment(); 652 } 653 654 } 655 656 protected void performLazyEnlistment() throws SQLException { 657 try { 658 this.lazyEnlistCm_.lazyEnlist(mc); 659 } catch( ResourceException re ) { 660 String msg = sm.getString( 661 "jdbc.cannot_enlist" , re.getMessage() + 662 " Cannnot Enlist ManagedConnection"); 663 664 SQLException sqle = new SQLException( msg ); 665 sqle.initCause( re ); 666 throw sqle; 667 } 668 669 } 670 671 protected void performLazyAssociation() throws SQLException { 672 try { 673 this.lazyAssocCm_.associateConnection( this, mcf_, cxReqInfo_); 674 } catch( ResourceException re ) { 675 String msg = sm.getString( 676 "jdbc.cannot_enlist", re.getMessage() + 677 " Cannnot Enlist ManagedConnection"); 678 679 SQLException sqle = new SQLException( msg); 680 sqle.initCause( re ); 681 throw sqle; 682 } 683 684 } 685 686 public void setConnectionType( ConnectionType type ) { 687 myType_ = type; 688 } 689 690 public ConnectionType getConnectionType() { 691 return myType_; 692 } 693 694 public void setLazyAssociatableConnectionManager( 695 javax.resource.spi.LazyAssociatableConnectionManager cm ) { 696 697 lazyAssocCm_ = cm; 698 } 699 700 public void setLazyEnlistableConnectionManager( 701 javax.resource.spi.LazyEnlistableConnectionManager cm ) { 702 703 lazyEnlistCm_ = cm; 704 } 705 706 public void setManagedConnection( ManagedConnection con ) { 707 this.mc = con; 708 } 709 710 } 711 | Popular Tags |