Refine search
private void closeConnections(final ArrayList<ConnectionData> connections) { if (connections == null) { return; } try { for (ConnectionData connectionData : connections) { Connection connection = connectionData.connection; if (!connection.isClosed()) { connection.close(); } } } catch (SQLException ignore) { // Ignore errors; garbage collect anyhow } }
/** * Closes the appender, flushing the buffer first then closing the default * connection if it is open. */ public void close() { flushBuffer(); try { if (connection != null && !connection.isClosed()) connection.close(); } catch (SQLException e) { errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE); } this.closed = true; }
public static void closeResources(Connection con, Statement statement) { try { if (statement != null && !statement.isClosed()) { statement.close(); } } catch (Exception e) { logger.error("", e); } try { if (con != null && !con.isClosed()) { con.close(); } } catch (Exception e) { logger.error("", e); } }
/** * Closes the physical database connection represented by this PooledConnection. If any client has * a connection based on this PooledConnection, it is forcibly closed as well. */ @Override public void close() throws SQLException { if (last != null) { last.close(); if (!con.isClosed()) { if (!con.getAutoCommit()) { try { con.rollback(); } catch (SQLException ignored) { } } } } try { con.close(); } finally { con = null; } }
/** * 关闭事务,清理现场 */ @Override public void close() { ComboException ce = new ComboException(); for (ConnInfo cInfo : list) { try { // 试图恢复旧的事务级别 if (!cInfo.conn.isClosed()) { if (cInfo.restoreIsoLevel) cInfo.conn.setTransactionIsolation(cInfo.oldLevel); if (cInfo.restoreAutoCommit) cInfo.conn.setAutoCommit(true); } } catch (Throwable e) {} finally { try { cInfo.conn.close(); } catch (Exception e) { ce.add(e); } } } // 清除数据源记录 list.clear(); }
/** * @throws Exception If failed. */ @Test public void testClose() throws Exception { String url = URL_PREFIX + HOST; final Connection conn = DriverManager.getConnection(url); assert conn != null; assert !conn.isClosed(); conn.close(); assert conn.isClosed(); GridTestUtils.assertThrows( log, new Callable<Object>() { @Override public Object call() throws Exception { conn.isValid(2); return null; } }, SQLException.class, "Connection is closed." ); } }
@Test public void testLeaveConnectionOpenOnRequest() throws Exception { String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(false); given(this.connection.isClosed()).willReturn(false); given(this.connection.createStatement()).willReturn(this.preparedStatement); // if close is called entire test will fail willThrow(new RuntimeException()).given(this.connection).close(); SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false); this.template = new JdbcTemplate(scf, false); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query(sql, rcch); verify(this.resultSet).close(); verify(this.preparedStatement).close(); }