📄 c3p0pooledconnectionpool.java
字号:
{ try { if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable( MLevel.FINER )) logger.log( MLevel.FINER, "Preparing to destroy PooledConnection: " + resc); ((PooledConnection) resc).close(); connectionCounter.decrement(); if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable( MLevel.FINER )) logger.log( MLevel.FINER, "Successfully destroyed PooledConnection: " + resc + ". Currently open Connections: " + connectionCounter.getValue() + "; Failed close count: " + failedCloseCounter.getValue() + "; Total processed by this pool: " + totalOpenedCounter.getValue()); } catch (Exception e) { failedCloseCounter.increment(); if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable( MLevel.FINER )) logger.log( MLevel.FINER, "Failed to destroy PooledConnection: " + resc + ". Currently open Connections: " + connectionCounter.getValue() + "; Failed close count: " + failedCloseCounter.getValue() + "; Total processed by this pool: " + totalOpenedCounter.getValue()); throw e; } } } ResourcePool.Manager manager = new PooledConnectionResourcePoolManager(); synchronized (fact) { fact.setMin( min ); fact.setMax( max ); fact.setIncrement( inc ); fact.setIdleResourceTestPeriod( idleConnectionTestPeriod * 1000); fact.setResourceMaxAge( maxIdleTime * 1000 ); fact.setAcquisitionRetryAttempts( acq_retry_attempts ); fact.setAcquisitionRetryDelay( acq_retry_delay ); fact.setBreakOnAcquisitionFailure( break_after_acq_failure ); fact.setAgeIsAbsolute( false ); //we timeout Connections only when idle rp = fact.createPool( manager ); } } catch (ResourcePoolException e) { throw SqlUtils.toSQLException(e); } } public PooledConnection checkoutPooledConnection() throws SQLException { //System.err.println(this + " -- CHECKOUT"); try { return (PooledConnection) rp.checkoutResource( checkoutTimeout ); } catch (TimeoutException e) { throw SqlUtils.toSQLException("An attempt by a client to checkout a Connection has timed out.", e); } catch (CannotAcquireResourceException e) { throw SqlUtils.toSQLException("Connections could not be acquired from the underlying database!", "08001", e); } catch (Exception e) { throw SqlUtils.toSQLException(e); } } public void checkinPooledConnection(PooledConnection pcon) throws SQLException { //System.err.println(this + " -- CHECKIN"); try { rp.checkinResource( pcon ); } catch (ResourcePoolException e) { throw SqlUtils.toSQLException(e); } } public void close() throws SQLException { close( true ); } public void close( boolean close_outstanding_connections ) throws SQLException { // System.err.println(this + " closing."); Exception throwMe = null; try { if (scache != null) scache.close(); } catch (SQLException e) { throwMe = e; } try { rp.close( close_outstanding_connections ); } catch (ResourcePoolException e) { if ( throwMe != null && logger.isLoggable( MLevel.WARNING ) ) logger.log( MLevel.WARNING, "An Exception occurred while closing the StatementCache.", throwMe); throwMe = e; } if (throwMe != null) throw SqlUtils.toSQLException( throwMe ); } class ConnectionEventListenerImpl implements ConnectionEventListener { // // We might want to check Connections in asynchronously, // because this is called // (indirectly) from a sync'ed method of NewPooledConnection, but // NewPooledConnection may be closed synchronously from a sync'ed // method of the resource pool, leading to a deadlock. Checking // Connections in asynchronously breaks the cycle. // // But then we want checkins to happen quickly and reliably, // whereas pool shutdowns are rare, so perhaps it's best to // leave this synchronous, and let the closing of pooled // resources on pool closes happen asynchronously to break // the deadlock. // // For now we're leaving both versions around, but with faster // and more reliable synchronous checkin enabled, and async closing // of resources in BasicResourcePool.close(). // public void connectionClosed(final ConnectionEvent evt) { //System.err.println("Checking in: " + evt.getSource()); if (ASYNCHRONOUS_CONNECTION_EVENT_LISTENER) { Runnable r = new Runnable() { public void run() { doCheckinResource( evt ); } }; sharedTaskRunner.postRunnable( r ); } else doCheckinResource( evt ); } private void doCheckinResource(ConnectionEvent evt) { try { rp.checkinResource( evt.getSource() ); } catch (Exception e) { //e.printStackTrace(); logger.log( MLevel.WARNING, "An Exception occurred while trying to check a PooledConection into a ResourcePool.", e ); } } // // We might want to update the pool asynchronously, because this is called // (indirectly) from a sync'ed method of NewPooledConnection, but // NewPooledConnection may be closed synchronously from a sync'ed // method of the resource pool, leading to a deadlock. Updating // pool status asynchronously breaks the cycle. // // But then we want checkins to happen quickly and reliably, // whereas pool shutdowns are rare, so perhaps it's best to // leave all ConnectionEvent handling synchronous, and let the closing of pooled // resources on pool closes happen asynchronously to break // the deadlock. // // For now we're leaving both versions around, but with faster // and more reliable synchrounous ConnectionEventHandling enabled, and async closing // of resources in BasicResourcePool.close(). // public void connectionErrorOccurred(final ConnectionEvent evt) {// System.err.println("CONNECTION ERROR OCCURRED!");// System.err.println(); if ( logger.isLoggable( MLevel.FINE ) ) logger.fine("CONNECTION ERROR OCCURRED!"); final PooledConnection pc = (PooledConnection) evt.getSource(); int status; if (pc instanceof C3P0PooledConnection) status = ((C3P0PooledConnection) pc).getConnectionStatus(); else if (pc instanceof NewPooledConnection) status = ((NewPooledConnection) pc).getConnectionStatus(); else //default to invalid connection, but not invalid database status = ConnectionTester.CONNECTION_IS_INVALID; final int final_status = status; if (ASYNCHRONOUS_CONNECTION_EVENT_LISTENER) { Runnable r = new Runnable() { public void run() { doMarkPoolStatus( pc, final_status ); } }; sharedTaskRunner.postRunnable( r ); } else doMarkPoolStatus( pc, final_status ); } private void doMarkPoolStatus(PooledConnection pc, int status) { try { switch (status) { case ConnectionTester.CONNECTION_IS_OKAY: throw new RuntimeException("connectionErrorOcccurred() should only be " + "called for errors fatal to the Connection."); case ConnectionTester.CONNECTION_IS_INVALID: rp.markBroken( pc ); break; case ConnectionTester.DATABASE_IS_INVALID: rp.resetPool(); break; default: throw new RuntimeException("Bad Connection Tester (" + connectionTester + ") " + "returned invalid status (" + status + ")."); } } catch ( ResourcePoolException e ) { //System.err.println("Uh oh... our resource pool is probably broken!"); //e.printStackTrace(); logger.log(MLevel.WARNING, "Uh oh... our resource pool is probably broken!", e); } } } public int getNumConnections() throws SQLException { try { return rp.getPoolSize(); } catch ( Exception e ) { //e.printStackTrace(); logger.log( MLevel.WARNING, null, e ); throw SqlUtils.toSQLException( e ); } } public int getNumIdleConnections() throws SQLException { try { return rp.getAvailableCount(); } catch ( Exception e ) { //e.printStackTrace(); logger.log( MLevel.WARNING, null, e ); throw SqlUtils.toSQLException( e ); } } public int getNumBusyConnections() throws SQLException { try { synchronized ( rp ) { return (rp.getAwaitingCheckinCount() - rp.getExcludedCount()); } } catch ( Exception e ) { //e.printStackTrace(); logger.log( MLevel.WARNING, null, e ); throw SqlUtils.toSQLException( e ); } } public int getNumUnclosedOrphanedConnections() throws SQLException { try { return rp.getExcludedCount(); } catch ( Exception e ) { //e.printStackTrace(); logger.log( MLevel.WARNING, null, e ); throw SqlUtils.toSQLException( e ); } } /** * Discards all Connections managed by the pool * and reacquires new Connections to populate. * Current checked out Connections will still * be valid, and should still be checked into the * pool (so the pool can destroy them). */ public void reset() throws SQLException { try { rp.resetPool(); } catch ( Exception e ) { //e.printStackTrace(); logger.log( MLevel.WARNING, null, e ); throw SqlUtils.toSQLException( e ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -