1 23 24 package com.sun.gjc.spi; 25 26 import javax.resource.spi.*; 27 import javax.resource.*; 28 import javax.security.auth.Subject ; 29 import java.io.PrintWriter ; 30 import javax.transaction.xa.XAResource ; 31 import java.util.Set ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import javax.sql.PooledConnection ; 35 import javax.sql.XAConnection ; 36 import java.sql.SQLException ; 37 import javax.resource.spi.security.PasswordCredential ; 38 import com.sun.gjc.common.DataSourceObjectBuilder; 39 import com.sun.logging.*; 40 import java.util.logging.Logger ; 41 import java.util.logging.Level ; 42 import com.sun.enterprise.util.i18n.StringManager; 43 44 50 public class ManagedConnection implements javax.resource.spi.ManagedConnection , 51 javax.resource.spi.LazyEnlistableManagedConnection , 52 javax.resource.spi.DissociatableManagedConnection 53 54 { 55 56 public static final int ISNOTAPOOLEDCONNECTION = 0; 57 public static final int ISPOOLEDCONNECTION = 1; 58 public static final int ISXACONNECTION = 2; 59 60 protected boolean isDestroyed = false; 61 protected boolean isUsable = true; 62 protected int connectionCount = 0; 63 64 protected int connectionType = ISNOTAPOOLEDCONNECTION; 65 protected PooledConnection pc = null; 66 protected java.sql.Connection actualConnection = null; 67 protected Hashtable connectionHandles; 68 protected PrintWriter logWriter; 69 protected PasswordCredential passwdCredential; 70 protected javax.resource.spi.ManagedConnectionFactory mcf = null; 71 protected XAResource xar = null; 72 73 protected ConnectionHolder myLogicalConnection = null; 74 75 protected int isolationLevelWhenCleaned; 77 protected boolean isClean = false; 78 79 protected boolean transactionInProgress = false; 80 81 protected ConnectionEventListener listener = null; 82 83 protected ConnectionEvent ce = null; 84 85 private boolean defaultAutoCommitValue = true; 86 private boolean lastAutoCommitValue = defaultAutoCommitValue; 87 88 protected static Logger _logger; 89 static { 90 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 91 } 92 93 protected StringManager localStrings = 94 StringManager.getManager( DataSourceObjectBuilder.class ); 95 116 public ManagedConnection(PooledConnection pooledConn, java.sql.Connection sqlConn, 117 PasswordCredential passwdCred, javax.resource.spi.ManagedConnectionFactory mcf) 118 throws ResourceException 119 { 120 if(pooledConn == null && sqlConn == null) { 121 122 String i18nMsg = localStrings.getString( 123 "jdbc.conn_obj_null"); 124 throw new ResourceException( i18nMsg ); 125 } 126 127 if (connectionType == ISNOTAPOOLEDCONNECTION ) { 128 actualConnection = sqlConn; 129 } 130 131 pc = pooledConn; 132 connectionHandles = new Hashtable (); 133 passwdCredential = passwdCred; 134 this.mcf = mcf; 135 if(passwdCredential != null && 136 this.mcf.equals(passwdCredential.getManagedConnectionFactory()) == false) { 137 138 String i18nMsg = localStrings.getString( 139 "jdbc.mc_construct_err"); 140 throw new ResourceException( i18nMsg ); 141 } 142 logWriter = mcf.getLogWriter(); 143 ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED); 144 } 145 146 152 public void addConnectionEventListener(ConnectionEventListener listener) { 153 this.listener = listener; 154 } 155 156 165 public void associateConnection(Object connection) throws ResourceException { 166 if(logWriter != null) { 167 logWriter.println("In associateConnection"); 168 } 169 checkIfValid(); 170 if(connection == null) { 171 String i18nMsg = localStrings.getString( 172 "jdbc.conn_handle_null"); 173 throw new ResourceException(i18nMsg); 174 } 175 ConnectionHolder ch = (ConnectionHolder) connection; 176 177 com.sun.gjc.spi.ManagedConnection mc = (com.sun.gjc.spi.ManagedConnection)ch.getManagedConnection(); 178 isClean = false; 179 180 ch.associateConnection(actualConnection, this); 181 188 189 190 ch.setActive(true); 191 incrementCount(); 192 193 if ( mc != null ) { 195 mc.decrementCount(); 196 } 197 } 198 199 206 public void cleanup() throws ResourceException { 207 if(logWriter != null) { 208 logWriter.println("In cleanup"); 209 } 210 checkIfValid(); 211 212 215 if (connectionType == ISNOTAPOOLEDCONNECTION ) { 218 try { 219 isolationLevelWhenCleaned = actualConnection.getTransactionIsolation(); 220 } catch(SQLException sqle) { 221 String i18nMsg = localStrings.getString( 222 "jdbc.cannot_get_iso_lvl"); 223 ResourceException re = new ResourceException( i18nMsg ); 224 re.initCause( sqle ); 225 throw re; 226 } 227 } 228 isClean = true; 229 230 } 231 232 240 protected void invalidateAllConnectionHandles() throws ResourceException { 241 Set handles = connectionHandles.keySet(); 242 Iterator iter = handles.iterator(); 243 try { 244 while(iter.hasNext()) { 245 ConnectionHolder ch = (ConnectionHolder)iter.next(); 246 ch.invalidate(); 247 } 248 } catch(java.util.NoSuchElementException nsee) { 249 throw new ResourceException("Could not find the connection handle: "+ nsee.getMessage()); 250 } 251 connectionHandles.clear(); 252 } 253 254 259 public void destroy() throws ResourceException{ 260 if(logWriter != null) { 261 logWriter.println("In destroy"); 262 } 263 if(isDestroyed == true) { 265 return; 266 } 267 268 try { 269 if(connectionType == ISXACONNECTION || connectionType == ISPOOLEDCONNECTION) { 270 pc.close(); 271 pc = null; 272 actualConnection = null; 273 } else { 274 actualConnection.close(); 275 actualConnection = null; 276 } 277 } catch(SQLException sqle) { 278 isDestroyed = true; 279 passwdCredential = null; 280 connectionHandles = null; 281 String i18nMsg = localStrings.getString( 282 "jdbc.error_in_destroy"); 283 ResourceException re = new ResourceException( 284 i18nMsg + sqle.getMessage(), sqle ); 285 throw re; 286 } 287 isDestroyed = true; 288 passwdCredential = null; 289 connectionHandles = null; 290 } 291 292 305 public Object getConnection(Subject sub, javax.resource.spi.ConnectionRequestInfo cxReqInfo) 306 throws ResourceException { 307 if(logWriter != null) { 308 logWriter.println("In getConnection"); 309 } 310 checkIfValid(); 311 319 320 getActualConnection(); 322 323 329 if(isClean) { 330 ((com.sun.gjc.spi.ManagedConnectionFactory)mcf).resetIsolation(this, isolationLevelWhenCleaned); 331 } 332 333 resetAutoCommit(); 334 335 myLogicalConnection = new ConnectionHolder(actualConnection, this, 336 cxReqInfo); 337 incrementCount(); 338 isClean=false; 339 340 341 myLogicalConnection.setActive(true); 342 343 return myLogicalConnection; 344 345 } 346 347 353 private void resetAutoCommit() throws ResourceException { 354 if (defaultAutoCommitValue != getLastAutoCommitValue() && !(isTransactionInProgress())) { 355 try { 356 actualConnection.setAutoCommit(defaultAutoCommitValue); 357 } catch (SQLException sqle) { 358 String i18nMsg = localStrings.getString( 359 "jdbc.error_during_setAutoCommit"); 360 throw new ResourceException(i18nMsg + sqle.getMessage(), sqle); 361 } 362 setLastAutoCommitValue(defaultAutoCommitValue); 363 } 364 } 365 366 373 public javax.resource.spi.LocalTransaction getLocalTransaction() throws ResourceException { 374 if(logWriter != null) { 375 logWriter.println("In getLocalTransaction"); 376 } 377 checkIfValid(); 378 return new com.sun.gjc.spi.LocalTransaction(this); 379 } 380 381 389 public PrintWriter getLogWriter() throws ResourceException { 390 if(logWriter != null) { 391 logWriter.println("In getLogWriter"); 392 } 393 checkIfValid(); 394 395 return logWriter; 396 } 397 398 405 public javax.resource.spi.ManagedConnectionMetaData getMetaData() throws ResourceException { 406 if(logWriter != null) { 407 logWriter.println("In getMetaData"); 408 } 409 checkIfValid(); 410 411 return new com.sun.gjc.spi.ManagedConnectionMetaData(this); 412 } 413 414 424 public XAResource getXAResource() throws ResourceException { 425 if(logWriter != null) { 426 logWriter.println("In getXAResource"); 427 } 428 checkIfValid(); 429 430 if(connectionType == ISXACONNECTION) { 431 try { 432 if(xar == null) { 433 436 xar = new com.sun.gjc.spi.XAResourceImpl(((XAConnection )pc).getXAResource(), this); 437 } 438 return xar; 439 } catch(SQLException sqle) { 440 throw new ResourceException(sqle.getMessage(), sqle); 441 } 442 } else { 443 throw new NotSupportedException("Cannot get an XAResource from a non XA connection"); 444 } 445 } 446 447 454 public void removeConnectionEventListener(ConnectionEventListener listener) { 455 listener = null; 456 } 457 458 463 void transactionStarted() { 464 transactionInProgress = true; 465 } 466 467 472 void transactionCompleted() { 473 try { 474 transactionInProgress = false; 475 if (connectionType == ISPOOLEDCONNECTION || connectionType == ISXACONNECTION) { 476 try { 477 isolationLevelWhenCleaned = actualConnection.getTransactionIsolation(); 478 } catch (SQLException sqle) { 479 _logger.log(Level.WARNING, "jdbc.notgot_tx_isolvl"); 481 } 482 483 if (connectionCount <= 0) { 484 try { 485 actualConnection.close(); 486 actualConnection = null; 487 } catch (SQLException sqle) { 488 actualConnection = null; 489 } 490 } 491 } 492 } catch (java.lang.NullPointerException e) { 493 _logger.log(Level.FINE, "jdbc.duplicateTxCompleted"); 494 } 495 isClean = true; 496 } 497 498 502 public boolean isTransactionInProgress() { 503 return transactionInProgress; 504 } 505 506 514 public void setLogWriter(PrintWriter out) throws ResourceException { 515 checkIfValid(); 516 logWriter = out; 517 } 518 519 526 protected int getConnectionType(PooledConnection pooledConn) { 527 if(pooledConn == null) { 528 return ISNOTAPOOLEDCONNECTION; 529 } else if(pooledConn instanceof XAConnection ) { 530 return ISXACONNECTION; 531 } else { 532 return ISPOOLEDCONNECTION; 533 } 534 } 535 536 543 ManagedConnectionFactory getManagedConnectionFactory() { 544 return (com.sun.gjc.spi.ManagedConnectionFactory)mcf; 545 } 546 547 552 java.sql.Connection getActualConnection() throws ResourceException { 554 if(connectionType == ISXACONNECTION || connectionType == ISPOOLEDCONNECTION) { 556 try { 557 if(actualConnection == null) { 558 actualConnection = pc.getConnection(); 559 560 setLastAutoCommitValue(defaultAutoCommitValue); 563 } 564 565 } catch(SQLException sqle) { 566 throw new ResourceException(sqle.getMessage(), sqle); 567 } 568 } 569 return actualConnection; 570 } 571 572 578 PasswordCredential getPasswordCredential() { 579 return passwdCredential; 580 } 581 582 592 void checkIfValid() throws ResourceException { 594 if(isDestroyed == true || isUsable == false) { 595 596 String i18nMsg = localStrings.getString( 597 "jdbc.mc_not_usable"); 598 throw new ResourceException( i18nMsg ); 599 } 600 } 601 602 613 void connectionClosed(Exception e, ConnectionHolder connHolderObject) throws SQLException { 614 connHolderObject.invalidate(); 615 616 decrementCount(); 617 ce.setConnectionHandle(connHolderObject); 618 listener.connectionClosed(ce); 619 620 } 621 622 630 void connectionErrorOccurred(Exception e, 631 com.sun.gjc.spi.ConnectionHolder connHolderObject) { 632 633 ConnectionEventListener cel = this.listener; 634 ConnectionEvent ce = null; 635 ce = e == null ? new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED) 636 : new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, e); 637 if (connHolderObject != null) { 638 ce.setConnectionHandle(connHolderObject); 639 } 640 641 cel.connectionErrorOccurred(ce); 642 isUsable = false; 643 } 644 645 646 647 652 void XAStartOccurred() { 653 try { 654 actualConnection.setAutoCommit(false); 655 } catch(Exception e) { 656 e.printStackTrace(); 657 connectionErrorOccurred(e, null); 658 } 659 } 660 661 666 void XAEndOccurred() { 667 try { 668 actualConnection.setAutoCommit(true); 669 } catch(Exception e) { 670 e.printStackTrace(); 671 connectionErrorOccurred(e, null); 672 } 673 } 674 675 689 690 void checkIfActive(ConnectionHolder ch) throws SQLException { 691 if(isDestroyed == true || isUsable == false) { 692 693 String i18nMsg = localStrings.getString( 694 "jdbc.conn_not_usable"); 695 throw new SQLException ( i18nMsg ); 696 } 697 } 698 699 704 public void initializeConnectionType( int _connectionType ) { 705 connectionType = _connectionType; 706 } 707 708 public void incrementCount() { 709 connectionCount++; 710 } 711 712 public void decrementCount() { 713 connectionCount--; 714 } 715 716 public void dissociateConnections() { 717 myLogicalConnection.setManagedConnection( 718 (com.sun.gjc.spi.ManagedConnection)null ); 719 } 720 public boolean getLastAutoCommitValue() { 721 return lastAutoCommitValue; 722 } 723 724 729 public void setLastAutoCommitValue(boolean lastAutoCommitValue) { 730 this.lastAutoCommitValue = lastAutoCommitValue; 731 } 732 } 733 | Popular Tags |