@Override protected void closeConnection(Connection con) { // Will work for transactional and non-transactional connections. DataSourceUtils.releaseConnection(con, this.dataSource); }
@Override public void close() throws SQLException { DataSourceUtils.releaseConnection(connection, dataSource); }
/** * Close the given JDBC Connection, created via this DAO's DataSource, * if it isn't bound to the thread. * @param con the Connection to close * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ protected final void releaseConnection(Connection con) { DataSourceUtils.releaseConnection(con, getDataSource()); }
/** * Execute the given {@link DatabasePopulator} against the given {@link DataSource}. * @param populator the {@code DatabasePopulator} to execute * @param dataSource the {@code DataSource} to execute against * @throws DataAccessException if an error occurs, specifically a {@link ScriptException} */ public static void execute(DatabasePopulator populator, DataSource dataSource) throws DataAccessException { Assert.notNull(populator, "DatabasePopulator must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); try { Connection connection = DataSourceUtils.getConnection(dataSource); try { populator.populate(connection); } finally { DataSourceUtils.releaseConnection(connection, dataSource); } } catch (Throwable ex) { if (ex instanceof ScriptException) { throw (ScriptException) ex; } throw new UncategorizedScriptException("Failed to execute database script", ex); } }
@Override @Nullable public <T> T execute(ConnectionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(obtainDataSource()); try { // Create close-suppressing Connection proxy, also preparing returned Statements. Connection conToUse = createConnectionProxy(con); return action.doInConnection(conToUse); } catch (SQLException ex) { // Release Connection early, to avoid potential connection pool deadlock // in the case when the exception translator hasn't been initialized yet. String sql = getSql(action); DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw translateException("ConnectionCallback", sql, ex); } finally { DataSourceUtils.releaseConnection(con, getDataSource()); } }
@Override public void beforeCompletion() { // Release Connection early if the holder is not open anymore // (that is, not used by another resource like a Hibernate Session // that has its own cleanup via transaction synchronization), // to avoid issues with strict JTA implementations that expect // the close call before transaction completion. if (!this.connectionHolder.isOpen()) { TransactionSynchronizationManager.unbindResource(this.dataSource); this.holderActive = false; if (this.connectionHolder.hasConnection()) { releaseConnection(this.connectionHolder.getConnection(), this.dataSource); } } }
@Override protected void doAfterCompletion(int status) { super.doAfterCompletion(status); Connection con = DataSourceUtils.getConnection(ds2); DataSourceUtils.releaseConnection(con, ds2); } };
@Override public void suspend() { if (this.holderActive) { TransactionSynchronizationManager.unbindResource(this.dataSource); if (this.connectionHolder.hasConnection() && !this.connectionHolder.isOpen()) { // Release Connection on suspend if the application doesn't keep // a handle to it anymore. We will fetch a fresh Connection if the // application accesses the ConnectionHolder again after resume, // assuming that it will participate in the same transaction. releaseConnection(this.connectionHolder.getConnection(), this.dataSource); this.connectionHolder.setConnection(null); } } }
@Override @Nullable public <T> T execute(StatementCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(obtainDataSource()); Statement stmt = null; try { stmt = con.createStatement(); applyStatementSettings(stmt); T result = action.doInStatement(stmt); handleWarnings(stmt); return result; } catch (SQLException ex) { // Release Connection early, to avoid potential connection pool deadlock // in the case when the exception translator hasn't been initialized yet. String sql = getSql(action); JdbcUtils.closeStatement(stmt); stmt = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw translateException("StatementCallback", sql, ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
/** * Executes the SQL as specified by {@link #getSequenceQuery()}. */ @Override protected long getNextKey() throws DataAccessException { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); rs = stmt.executeQuery(getSequenceQuery()); if (rs.next()) { return rs.getLong(1); } else { throw new DataAccessResourceFailureException("Sequence query did not return a result"); } } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not obtain sequence value", ex); } finally { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
@Override protected void doCleanupAfterCompletion(Object transaction) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction; // Remove the connection holder from the thread, if exposed. if (txObject.isNewConnectionHolder()) { TransactionSynchronizationManager.unbindResource(obtainDataSource()); } // Reset connection. Connection con = txObject.getConnectionHolder().getConnection(); try { if (txObject.isMustRestoreAutoCommit()) { con.setAutoCommit(true); } DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel()); } catch (Throwable ex) { logger.debug("Could not reset JDBC Connection after transaction", ex); } if (txObject.isNewConnectionHolder()) { if (logger.isDebugEnabled()) { logger.debug("Releasing JDBC Connection [" + con + "] after transaction"); } DataSourceUtils.releaseConnection(con, this.dataSource); } txObject.getConnectionHolder().clear(); }
@Override public void afterCompletion(int status) { // If we haven't closed the Connection in beforeCompletion, // close it now. The holder might have been used for other // cleanup in the meantime, for example by a Hibernate Session. if (this.holderActive) { // The thread-bound ConnectionHolder might not be available anymore, // since afterCompletion might get called from a different thread. TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource); this.holderActive = false; if (this.connectionHolder.hasConnection()) { releaseConnection(this.connectionHolder.getConnection(), this.dataSource); // Reset the ConnectionHolder: It might remain bound to the thread. this.connectionHolder.setConnection(null); } } this.connectionHolder.reset(); } }
JdbcUtils.closeStatement(cs); cs = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw translateException("CallableStatementCallback", sql, ex); DataSourceUtils.releaseConnection(con, getDataSource());
JdbcUtils.closeStatement(ps); ps = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw translateException("PreparedStatementCallback", sql, ex); DataSourceUtils.releaseConnection(con, getDataSource());
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Connection c = DataSourceUtils.getConnection(dsToUse); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(connection, c); DataSourceUtils.releaseConnection(c, dsToUse); } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Connection c = DataSourceUtils.getConnection(dsToUse); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(connection1, c); DataSourceUtils.releaseConnection(c, dsToUse); } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Connection c = DataSourceUtils.getConnection(dsToUse); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(connection, c); DataSourceUtils.releaseConnection(c, dsToUse); } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Connection c = DataSourceUtils.getConnection(dsToUse); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(connection2, c); DataSourceUtils.releaseConnection(c, dsToUse); } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is existing transaction", !status.isNewTransaction()); Connection c = DataSourceUtils.getConnection(dataSource); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource)); DataSourceUtils.releaseConnection(c, dataSource); c = DataSourceUtils.getConnection(dataSource); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource)); if (releaseCon) { DataSourceUtils.releaseConnection(c, dataSource); } } });
@Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dataSource)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Connection c = DataSourceUtils.getConnection(dataSource); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource)); DataSourceUtils.releaseConnection(c, dataSource); c = DataSourceUtils.getConnection(dataSource); assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dataSource)); DataSourceUtils.releaseConnection(c, dataSource); if (rollback) { status.setRollbackOnly(); } } });