⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c3p0pooledconnection.java

📁 c3p0数据库连接池实现源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				    return doRawConnectionOperation((Method) args[0], args[1], (Object[]) args[2]);    				}			    else if (mname.equals("setTransactionIsolation"))				{				    ensureOkay();				    //don't modify txn_known_resolved				    m.invoke( activeConnection, args );				    int lvl = ((Integer) args[0]).intValue();				    isolation_lvl_nondefault = (lvl != dflt_txn_isolation);				    //System.err.println("updated txn isolation to " + lvl + ", nondefault level? " + isolation_lvl_nondefault);				    return null;				}			    else if (mname.equals("setCatalog"))				{				    ensureOkay();				    //don't modify txn_known_resolved				    m.invoke( activeConnection, args );				    String catalog = (String) args[0];				    catalog_nondefault = ObjectUtils.eqOrBothNull(catalog, dflt_catalog);				    return null;				}			    else if (mname.equals("setHoldability"))				{				    ensureOkay();				    //don't modify txn_known_resolved				    m.invoke( activeConnection, args ); //will throw an exception if setHoldability() not supported...				    int holdability = ((Integer) args[0]).intValue();				    holdability_nondefault = (holdability != dflt_holdability);				    return null;				}			    else if (mname.equals("createStatement"))				{				    ensureOkay();				    txn_known_resolved = false;					    Object stmt = m.invoke( activeConnection, args );				    return createProxyStatement( (Statement) stmt );				}			    else if (mname.equals("prepareStatement"))				{				    ensureOkay();				    txn_known_resolved = false;					    Object pstmt;				    if (scache == null)					{					    pstmt = m.invoke( activeConnection, args );					    return createProxyStatement( (Statement) pstmt );					}				    else					{					    pstmt = scache.checkoutStatement( physicalConnection,									      m, 									      args );					    return createProxyStatement( true,									 (Statement) pstmt );					}				}			    else if (mname.equals("prepareCall"))				{				    ensureOkay();				    txn_known_resolved = false;					    Object cstmt;				    if (scache == null)					{					    cstmt = m.invoke( activeConnection, args );					    return createProxyStatement( (Statement) cstmt ); 					}				    else					{					    cstmt = scache.checkoutStatement( physicalConnection, m, args );					    return createProxyStatement( true,									 (Statement) cstmt );					}				}			    else if (mname.equals("getMetaData"))				{				    ensureOkay();				    txn_known_resolved = false; //views of tables etc. might be txn dependent					    DatabaseMetaData innerMd = activeConnection.getMetaData();				    if (metaData == null)					{					    // exposedProxy is protected by C3P0PooledConnection.this' lock					    synchronized (C3P0PooledConnection.this)						{ metaData = new SetManagedDatabaseMetaData(innerMd, activeMetaDataResultSets, exposedProxy); }					}				    return metaData;				}			    else if (mname.equals("silentClose"))				{				    //the PooledConnection doesn't have to be okay					    doSilentClose( proxy, ((Boolean) args[0]).booleanValue(), this.txn_known_resolved );				    return null;				}			    else if ( mname.equals("close") )				{				    //the PooledConnection doesn't have to be okay					    Exception e = doSilentClose( proxy, false, this.txn_known_resolved );				    if (! connection_error_signaled)					ces.fireConnectionClosed();				    //System.err.println("close() called on a ProxyConnection.");				    if (e != null)					{					    // 					    System.err.print("user close exception -- ");					    // 					    e.printStackTrace();					    throw e;					}				    else					return null;				}// 			    else if ( mname.equals("finalize") ) //REMOVE THIS CASE -- TMP DEBUG// 				{// 				    System.err.println("Connection apparently finalized!");// 				    return m.invoke( activeConnection, args );// 				}			    else				{				    ensureOkay();					    				    // we've disabled setting txn_known_resolved to true, ever, because				    // we failed to deal with the case that clients would work with previously				    // acquired Statements and ResultSets after a commit(), rollback(), or setAutoCommit().				    // the new non-reflective proxies have been modified to deal with this case.				    // here, with soon-to-be-deprecated in "traditional reflective proxies mode"				    // we are reverting to the conservative, always-presume-you-have-to-rollback				    // policy.				    //txn_known_resolved = ( mname.equals("commit") || mname.equals( "rollback" ) || mname.equals( "setAutoCommit" ) );				    txn_known_resolved = false; 				    return m.invoke( activeConnection, args );				}			}		    else			{			    if (mname.equals("close") || 				mname.equals("silentClose"))				return null;			    else if (mname.equals("isClosed"))				return new Boolean(true);			    else				{				    throw new SQLException("You can't operate on " +							   "a closed connection!!!");				}			}		}	    catch (InvocationTargetException e)		{		    Throwable convertMe = e.getTargetException();		    SQLException sqle = handleMaybeFatalToPooledConnection( convertMe, proxy, false );		    sqle.fillInStackTrace();		    throw sqle;		}	}		private Exception doSilentClose(Object proxyConnection, boolean pooled_connection_is_dead)	{ return doSilentClose( proxyConnection, pooled_connection_is_dead, false ); }	private Exception doSilentClose(Object proxyConnection, boolean pooled_connection_is_dead, boolean known_resolved_txn)	{	    if ( activeConnection != null )		{		    synchronized ( C3P0PooledConnection.this ) //uh oh... this is a nested lock acq... is there a deadlock hazard here?			{			    if ( C3P0PooledConnection.this.exposedProxy == proxyConnection )				{				    C3P0PooledConnection.this.exposedProxy = null;				    //System.err.println("Reset exposed proxy.");					    				    //DEBUG				    //origGet = null;				}			    else //else case -- DEBUG only				logger.warning("(c3p0 issue) doSilentClose( ... ) called on a proxyConnection " +					       "other than the current exposed proxy for its PooledConnection. [exposedProxy: " +					       exposedProxy + ", proxyConnection: " + proxyConnection);// 				System.err.println("[DEBUG] WARNING: doSilentClose( ... ) called on a proxyConnection " +// 						   "other than the current exposed proxy for its PooledConnection. [exposedProxy: " +// 						   exposedProxy + ", proxyConnection: " + proxyConnection);			}			    		    Exception out = null;			    		    Exception exc1 = null, exc2 = null, exc3 = null, exc4 = null;		    try 			{ 			    if (! pooled_connection_is_dead)				C3P0PooledConnection.this.reset(known_resolved_txn); 			}		    catch (Exception e)			{ 			    exc1 = e;			    // 		    if (Debug.DEBUG)			    // 			{			    // 			    System.err.print("exc1 -- ");			    // 			    exc1.printStackTrace();			    // 			}			}			    		    exc2 = cleanupUncachedActiveStatements();		    // 	    if (Debug.DEBUG && exc2 != null)		    // 		{		    // 		    System.err.print("exc2 -- ");		    // 		    exc2.printStackTrace();		    // 		}		    String errSource;		    if (doRawResultSets != null)			{			    activeMetaDataResultSets.addAll( doRawResultSets );			    errSource = "DataBaseMetaData or raw Connection operation";			}		    else			errSource = "DataBaseMetaData";		    if (!closeAndRemoveResultSets( activeMetaDataResultSets ))			exc3 = new SQLException("Failed to close some " + errSource + " Result Sets.");		    // 	    if (Debug.DEBUG && exc3 != null)		    // 		{		    // 		    System.err.print("exc3 -- ");		    // 		    exc3.printStackTrace();		    // 		}		    if (scache != null)			{			    try				{ scache.checkinAll( physicalConnection ); }			    catch ( Exception e )				{ exc4 = e; }			    // 		    if (Debug.DEBUG && exc4 != null)			    // 			{			    // 			    System.err.print("exc4 -- ");			    // 			    exc4.printStackTrace();			    // 			}			}			    		    if (exc1 != null)			{			    handleMaybeFatalToPooledConnection( exc1, proxyConnection, true );			    out = exc1;			}		    else if (exc2 != null)			{			    handleMaybeFatalToPooledConnection( exc2, proxyConnection, true );			    out = exc2;			}		    else if (exc3 != null)			{			    handleMaybeFatalToPooledConnection( exc3, proxyConnection, true );			    out = exc3;			}		    else if (exc4 != null)			{			    handleMaybeFatalToPooledConnection( exc4, proxyConnection, true );			    out = exc4;			}			    		    // 	    if (out != null)		    // 		{		    // 		    System.err.print("out -- ");		    // 		    out.printStackTrace();		    // 		}			    activeConnection = null;		    return out;		}	    else		return null;	}		private SQLException handleMaybeFatalToPooledConnection( Throwable t, Object proxyConnection, boolean already_closed )	{	    //System.err.println("handleMaybeFatalToPooledConnection()");		    SQLException sqle = SqlUtils.toSQLException( t );	    int status = connectionTester.statusOnException( physicalConnection, sqle );	    updateConnectionStatus( status ); 	    if (status != ConnectionTester.CONNECTION_IS_OKAY)		{		    if (Debug.DEBUG)			{// 			    System.err.print(C3P0PooledConnection.this + " will no longer be pooled because it has been " +// 					     "marked invalid by the following Exception: ");// 			    t.printStackTrace();			    logger.log(MLevel.INFO, 				       C3P0PooledConnection.this + " will no longer be pooled because it has been marked invalid by an Exception.",				       t );			}			    		    invalidatingException = sqle;		    /*		      ------		      A users have complained that SQLExceptions ought not close their Connections underneath		      them under any circumstance. Signalling the Connection error after updating the Connection		      status should be sufficient from the pool's perspective, because the PooledConnection		      will be marked broken by the pool and will be destroyed on checkin. I think actually		      close()ing the Connection when it appears to be broken rather than waiting for users		      to close() it themselves is overly aggressive, so I'm commenting the old behavior out.		      The only potential downside to this approach is that users who do not close() in a finally		      clause properly might see their close()es skipped by exceptions that previously would		      have led to automatic close(). But relying on the automatic close() was never reliable		      (since it only ever happened when c3p0 determined a Connection to be absolutely broken),		      and is generally speaking a client error that c3p0 ought not be responsible for dealing		      with. I think it's right to leave this out. -- swaldman 2004-12-09		      ------		      		      if (! already_closed )		          doSilentClose( proxyConnection, true );		    */		    if (! connection_error_signaled)			{			    ces.fireConnectionErrorOccurred( sqle );			    connection_error_signaled = true;			}		}	    return sqle;	}	    }    interface ProxyConnection extends C3P0ProxyConnection    { void silentClose( boolean known_invalid ) throws SQLException; }    public synchronized int getConnectionStatus()    { return this.connection_status; }    private synchronized void updateConnectionStatus(int status)    {	switch ( this.connection_status )	    {	    case ConnectionTester.DATABASE_IS_INVALID:		//can't get worse than this, do nothing.		break;	    case ConnectionTester.CONNECTION_IS_INVALID:		if (status == ConnectionTester.DATABASE_IS_INVALID)		    doBadUpdate(status);		break;	    case ConnectionTester.CONNECTION_IS_OKAY:		if (status != ConnectionTester.CONNECTION_IS_OKAY)		    doBadUpdate(status);		break;	    default:		throw new InternalError(this + " -- Illegal Connection Status: " + this.connection_status);	    }    }    //must be called from sync'ed method    private void doBadUpdate(int new_status)    {	this.connection_status = new_status;	try { this.close( true ); }	catch (SQLException e)	    {// 		System.err.print("Broken Connection Close Error: ");// 		e.printStackTrace(); 		logger.log(MLevel.WARNING, "Broken Connection Close Error. ", e);	    }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -