/** ************ private stuff ************** */ private void closeConnectionIfNecessary() { try { if (connection.autoClose) { connection.close(); } } catch (Exception ex) { throw new Sql2oException("Error while attempting to close connection", ex); } }
/** 关闭连接 */ public static final void closeConnection() { Connection conn = conns.get(); try { if (conn != null && !conn.isClosed()) { conn.setAutoCommit(true); conn.close(); connectionContext.remove(Thread.currentThread().getId()); } } catch (SQLException e) { log.error("Unabled to close connection!!! ", e); } conns.set(null); }
/** * Try to update the row with the given error. Flag a failure if a timeout occurs when not * expected, and vice versa. * * @param id The id of the row to be updated * @param timeoutExpected true if it is expected that the update times out * @throws java.sql.SQLException */ private void executeParallelUpdate(int id, boolean timeoutExpected) throws SQLException { Connection conn2 = openDefaultConnection(); Statement stmt2 = conn2.createStatement(); try { stmt2.executeUpdate( "update BLOBCLOB set BLOBDATA = " + "cast(X'FFFFFF' as blob) where ID=" + id); stmt2.close(); conn2.commit(); conn2.close(); if (timeoutExpected) { fail("FAIL - should have gotten lock timeout"); } } catch (SQLException se) { stmt2.close(); conn2.rollback(); conn2.close(); if (timeoutExpected) { assertSQLState(LOCK_TIMEOUT, se); } else { throw se; } } }
// TODO: factor out repetitive debugging code private synchronized void close(boolean known_invalid) throws SQLException { // System.err.println("Closing " + this); if (physicalConnection != null) { try { StringBuffer debugOnlyLog = null; if (Debug.DEBUG && known_invalid) { debugOnlyLog = new StringBuffer(); debugOnlyLog.append("[ exceptions: "); } Exception exc = cleanupUncachedActiveStatements(); if (Debug.DEBUG && exc != null) { if (known_invalid) debugOnlyLog.append(exc.toString() + ' '); else logger.log( MLevel.WARNING, "An exception occurred while cleaning up uncached active Statements.", exc); // exc.printStackTrace(); } try { // we've got to use silentClose() rather than close() here, // 'cuz if there's still an exposedProxy (say a user forgot to // close his Connection) before we close, and we use regular (loud) // close, we will try to check this dead or dying PooledConnection // back into the pool. We only want to do this when close is called // on user proxies, and the underlying PooledConnection might still // be good. The PooledConnection itself should only be closed by the // pool. if (exposedProxy != null) exposedProxy.silentClose(known_invalid); } catch (Exception e) { if (Debug.DEBUG) { if (known_invalid) debugOnlyLog.append(e.toString() + ' '); else logger.log(MLevel.WARNING, "An exception occurred.", exc); // e.printStackTrace(); } exc = e; } try { this.closeAll(); } catch (Exception e) { if (Debug.DEBUG) { if (known_invalid) debugOnlyLog.append(e.toString() + ' '); else logger.log(MLevel.WARNING, "An exception occurred.", exc); // e.printStackTrace(); } exc = e; } try { physicalConnection.close(); } catch (Exception e) { if (Debug.DEBUG) { if (known_invalid) debugOnlyLog.append(e.toString() + ' '); else logger.log(MLevel.WARNING, "An exception occurred.", exc); e.printStackTrace(); } exc = e; } if (exc != null) { if (known_invalid) { debugOnlyLog.append(" ]"); if (Debug.DEBUG) { // System.err.print("[DEBUG]" + this + ": while closing a PooledConnection known // to be invalid, "); // System.err.println(" some exceptions occurred. This is probably not a // problem:"); // System.err.println( debugOnlyLog.toString() ); logger.fine( this + ": while closing a PooledConnection known to be invalid, " + " some exceptions occurred. This is probably not a problem: " + debugOnlyLog.toString()); } } else throw new SQLException( "At least one error occurred while attempting " + "to close() the PooledConnection: " + exc); } if (Debug.TRACE == Debug.TRACE_MAX) logger.fine("C3P0PooledConnection closed. [" + this + ']'); // System.err.println("C3P0PooledConnection closed. [" + this + ']'); } finally { physicalConnection = null; } } }
void close() throws SQLException { m_originConnection.close(); }