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:com.qualogy.qafe.business.resource.rdb.RDBDatasource.java

public void destroy(ApplicationContext context) {
    if (dataSource != null) {
        try {/*from w  ww.  j  a  v a 2 s  .  c  o m*/
            DataSourceUtils.releaseConnection(dataSource.getConnection(), dataSource);
        } catch (SQLException e) {
            LOG.log(Level.WARNING, "Problem releasing db connection", e);
        }
        // if (!dataSource.getConnection().isClosed()){
        // // You call close() on the Connection object when you're done. Note that this doesn't really
        // close the connection - it just returns it to the pool.
        // dataSource.getConnection().close();
        // }
    }
}

From source file:xyz.vopen.passport.commons.jdbc.JdbcTemplate.java

/**
 * ?????(??)/*from   w  w  w.j  a  v  a2 s.  c  o m*/
 */
protected Object query(String sql, Object[] params, ResultSetHandler handler) {
    Connection conn = null;
    try {
        conn = getConnection();
        // logger.info("dbutils.query??");
        return queryRunner.query(conn, sql, handler, params);
        //            return queryRunner.query(conn, sql, params, handler);
    } catch (SQLException t) {
        System.out.println("??" + t.getMessage());
        throw new DataIntegrityViolationException(t.getMessage());
    } finally {
        DataSourceUtils.releaseConnection(conn, this.getDataSource());
        // logger.info("dbutils.query?");
    }

}

From source file:com.bluexml.side.Integration.alfresco.sql.synchronization.schemaManagement.SchemaCreation.java

private void createFromModel(QName modelName) {

    if (ready) {// ww w.j a v  a  2s .c  o m
        Connection connection = DataSourceUtils.getConnection(dataSource);

        List<CreateTableStatement> createStatements = doCreateStatement(modelName);

        CheckTableStatus checkTableStatus = doCheckStatus(createStatements, connection);
        statusByModel.put(modelName, checkTableStatus);

        if (checkTableStatus == CheckTableStatus.CREATE_TABLES) {

            boolean creationSuccess = doExecuteCreateStatements(createStatements, connection);
            if (!creationSuccess) {
                logger.error("Creation of tables failed");
                ready = false;
            }

            DataSourceUtils.releaseConnection(connection, dataSource);

        } else {
            if (logger.isDebugEnabled())
                logger.debug("Creation of model \"" + modelName
                        + "\" was not performed since the previous process marked the schema as not ready or creation has yet be done");
        }

    }

}

From source file:org.mybatis.spring.transaction.SpringManagedTransaction.java

/**
 * {@inheritDoc}/*from w  ww .j av a2 s  .  c  o  m*/
 */
@Override
public void close() throws SQLException {
    DataSourceUtils.releaseConnection(this.connection, this.dataSource);
}

From source file:io.kahu.hawaii.util.call.sql.AbortableQuery.java

@Override
protected void executeInternally(ResponseHandler responseHandler, Response response) throws ServerException {
    SqlParameterSource paramSource = new MapSqlParameterSource(params);
    PreparedStatementCreator psc = getPreparedStatementCreator(sql, paramSource);

    Connection connection = DataSourceUtils.getConnection(dataSource);
    try {/*from w  w w .  j  a  v a 2  s.c o  m*/
        preparedStatement = psc.createPreparedStatement(connection);

        switch (callType) {
        case INSERT:
            preparedStatement.executeUpdate();
            new UpdateIdResponseHandler().addToResponse(preparedStatement, response);
            break;

        case DELETE:
            // fall though
        case UPDATE:
            response.set(preparedStatement.executeUpdate());
            break;

        case SELECT:
            ResultSet resultSet = preparedStatement.executeQuery();
            responseHandler.addToResponse(resultSet, response);
            break;

        default:
            throw new ServerException(ServerError.ILLEGAL_ARGUMENT, "Unknown call type '" + callType + "'.");
        }
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (Throwable t) {
            //
        }
        if (!aborted) {
            response.setStatus(ResponseStatus.BACKEND_FAILURE, e);
        }
        throw new ServerException(ServerError.UNEXPECTED_EXCEPTION, e);
    } finally {
        DataSourceUtils.releaseConnection(connection, dataSource);
    }
}

From source file:xyz.vopen.passport.commons.jdbc.JdbcTemplate.java

public int[] batch(String sql, Object[][] params) {
    Connection conn = null;//from  w  ww . ja  v  a 2  s .c om
    try {
        conn = getConnection();
        // logger.info("dbutils.batch??");
        return queryRunner.batch(conn, sql, params);
    } catch (SQLException t) {
        System.out.println(t.getMessage());
        throw new DataIntegrityViolationException(t.getMessage());
    } finally {
        DataSourceUtils.releaseConnection(conn, this.getDataSource());
        // logger.info("dbutils.batch?");
    }
}

From source file:com.qualogy.qafe.business.integration.rdb.SQLQueryDAO.java

/**
 * @param ds/*  ww w  .  jav  a2  s .  c  o m*/
 * @param tableName
 * @throws SQLException
 */
private void populateTableColumnSet(DataSource ds, String tableName) throws SQLException {
    Connection conn = ds.getConnection();
    DatabaseMetaData dbmd = conn.getMetaData();
    ResultSet rsc = dbmd.getColumns(conn.getCatalog(), null, tableName, "%");
    Set<String> foundColumnSet = new HashSet<String>();
    while (rsc.next()) {
        String columnName = rsc.getString("COLUMN_NAME");
        foundColumnSet.add(columnName);
    }
    tableColumnSet.put(tableName, foundColumnSet);
    DataSourceUtils.releaseConnection(conn, ds);
}

From source file:org.mifos.test.framework.util.DatabaseTestUtils.java

private void cleanAndInsertDataSet(DriverManagerDataSource dataSource, IDataSet dataSet)
        throws DatabaseUnitException, SQLException {
    Connection jdbcConnection = null;
    ReplacementDataSet replacementDataSet = getDataSetWithNullsReplaced(dataSet);
    try {// w  w w .  j a va  2  s  .  c  om
        jdbcConnection = DataSourceUtils.getConnection(dataSource);
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, replacementDataSet);
    } finally {
        if (null != jdbcConnection) {
            jdbcConnection.close();
        }
        DataSourceUtils.releaseConnection(jdbcConnection, dataSource);
    }
}

From source file:com.bt.aloha.testing.JdbcHelper.java

protected int executeUpdate(DataSource ds, String createScript) throws SQLException {
    Connection conn = null;/*from   www.  j a v  a 2  s  .  c om*/
    Statement st = null;
    int rowCount = -1;
    try {
        conn = DataSourceUtils.getConnection(ds);
        st = conn.createStatement();
        rowCount = st.executeUpdate(createScript);
        assert rowCount > 0;
    } catch (SQLException e) {
        log.warn("Unable to execute create sctipt: " + e.getMessage());
    } finally {
        try {
            if (st != null)
                st.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null)
                DataSourceUtils.releaseConnection(conn, ds);
        }
    }
    return rowCount;
}