Example usage for org.springframework.jdbc.datasource DataSourceUtils releaseConnection

List of usage examples for org.springframework.jdbc.datasource DataSourceUtils releaseConnection

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource DataSourceUtils releaseConnection.

Prototype

public static void releaseConnection(@Nullable Connection con, @Nullable DataSource dataSource) 

Source Link

Document

Close the given Connection, obtained from the given DataSource, if it is not managed externally (that is, not bound to the thread).

Usage

From source file:org.libreplan.business.common.test.dbunit.DBUnitTestExecutionListener.java

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {

    super.beforeTestMethod(testContext);

    DataSource dataSource = getDataSource(testContext);
    Connection conn = DataSourceUtils.getConnection(dataSource);
    IDatabaseConnection dbUnitConn = getDBUnitConnection(conn);

    try {/*from w ww. ja v  a  2 s.c o m*/
        IDataSet dataSets[] = getDataSets(testContext);
        for (IDataSet dataSet : dataSets) {
            DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet);
            logger.debug("Performed CLEAN_INSERT of IDataSet.");
        }
    } finally {
        DataSourceUtils.releaseConnection(conn, dataSource);
    }
}

From source file:org.mifos.framework.util.DbUnitUtilities.java

public void loadDataFromFile(String filename, DriverManagerDataSource dataSource)
        throws DatabaseUnitException, SQLException, IOException {
    Connection jdbcConnection = null;
    IDataSet dataSet = getDataSetFromDataSetDirectoryFile(filename);
    try {/*w  ww .java2 s .  com*/
        jdbcConnection = DataSourceUtils.getConnection(dataSource);
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
        DataSourceUtils.releaseConnection(jdbcConnection, dataSource);
    }
}

From source file:org.mifos.framework.util.DbUnitUtilities.java

/**
 * Returns a DbUnit DataSet for several tables.
 * @param driverManagerDataSource TODO// ww  w. j  a  v  a  2 s  .  c  o  m
 * @param tableNames
 * @return IDataSet
 * @throws Exception
 */
@SuppressWarnings("PMD.SignatureDeclareThrowsException") // one of the dependent methods throws Exception
public IDataSet getDataSetForTables(DriverManagerDataSource driverManagerDataSource, String[] tableNames)
        throws Exception {
    Connection jdbcConnection = null;
    IDataSet databaseDataSet = null;
    try {
        jdbcConnection = DataSourceUtils.getConnection(driverManagerDataSource);
        IDatabaseTester databaseTester = new DataSourceDatabaseTester(driverManagerDataSource);
        IDatabaseConnection databaseConnection = databaseTester.getConnection();
        databaseDataSet = databaseConnection.createDataSet(tableNames);
    } finally {
        jdbcConnection.close();
        DataSourceUtils.releaseConnection(jdbcConnection, driverManagerDataSource);
    }
    return databaseDataSet;
}

From source file:org.springframework.batch.item.database.AbstractCursorItemReader.java

/**
 * Close the cursor and database connection. Make call to cleanupOnClose so sub classes can cleanup
 * any resources they have allocated./*from w  w w  .j  a  v  a2 s .  com*/
 */
@Override
protected void doClose() throws Exception {
    initialized = false;
    JdbcUtils.closeResultSet(this.rs);
    rs = null;
    cleanupOnClose();
    if (useSharedExtendedConnection && dataSource instanceof ExtendedConnectionDataSourceProxy) {
        ((ExtendedConnectionDataSourceProxy) dataSource).stopCloseSuppression(this.con);
        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
    } else {
        JdbcUtils.closeConnection(this.con);
    }
}

From source file:org.springframework.jdbc.support.JdbcUtils.java

/**
 * Extract database meta data via the given DatabaseMetaDataCallback.
 * <p>This method will open a connection to the database and retrieve the database metadata.
 * Since this method is called before the exception translation feature is configured for
 * a datasource, this method can not rely on the SQLException translation functionality.
 * <p>Any exceptions will be wrapped in a MetaDataAccessException. This is a checked exception
 * and any calling code should catch and handle this exception. You can just log the
 * error and hope for the best, but there is probably a more serious error that will
 * reappear when you try to access the database again.
 * @param dataSource the DataSource to extract metadata for
 * @param action callback that will do the actual work
 * @return object containing the extracted information, as returned by
 * the DatabaseMetaDataCallback's {@code processMetaData} method
 * @throws MetaDataAccessException if meta data access failed
 *///from   w  w  w.  j a  va  2 s.co m
public static Object extractDatabaseMetaData(DataSource dataSource, DatabaseMetaDataCallback action)
        throws MetaDataAccessException {

    Connection con = null;
    try {
        con = DataSourceUtils.getConnection(dataSource);
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData == null) {
            // should only happen in test environments
            throw new MetaDataAccessException("DatabaseMetaData returned by Connection [" + con + "] was null");
        }
        return action.processMetaData(metaData);
    } catch (CannotGetJdbcConnectionException ex) {
        throw new MetaDataAccessException("Could not get Connection for extracting meta data", ex);
    } catch (SQLException ex) {
        throw new MetaDataAccessException("Error while extracting DatabaseMetaData", ex);
    } catch (AbstractMethodError err) {
        throw new MetaDataAccessException(
                "JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver", err);
    } finally {
        DataSourceUtils.releaseConnection(con, dataSource);
    }
}

From source file:org.unitils.database.core.DataSourceWrapper.java

public void releaseConnection(Connection connection) {
    DataSourceUtils.releaseConnection(connection, wrappedDataSource);
}