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:net.tirasa.ilgrosso.resetdb.Main.java

public static void main(final String[] args) {
    ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

    final DBMS dbms = ctx.getBean(DBMS.class);
    if (dbms == null) {
        throw new IllegalArgumentException("Could not find a valid DBMS bean");
    }//from www .  java2  s .  co  m

    final DataSource dataSource = ctx.getBean(DataSource.class);
    final Connection conn = DataSourceUtils.getConnection(dataSource);
    try {
        switch (dbms) {

        case POSTGRESQL:
            resetPostgreSQL(conn);
            break;

        case MYSQL:
            resetMySQL(conn);
            break;

        case ORACLE:
            resetOracle(conn);
            break;

        case SQLSERVER:
            resetSQLServer(conn);
            break;

        default:
            LOG.warn("Unsupported DBMS: {}", dbms);
        }
    } catch (Throwable t) {
        LOG.error("During execution", t);
    } finally {
        DataSourceUtils.releaseConnection(conn, dataSource);
    }

    LOG.info("Reset successfully done.");
}

From source file:org.cloudfoundry.identity.uaa.scim.test.TestUtils.java

public static void runScript(DataSource dataSource, String stem) throws Exception {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    String packageName = ClassUtils.getPackageName(TestUtils.class).replace(".", "/");
    populator.addScript(new ClassPathResource(
            packageName.substring(0, packageName.lastIndexOf("/")) + "/" + stem + "-" + platform + ".sql"));
    Connection connection = dataSource.getConnection();
    try {//from w w w. j av  a  2 s.  co  m
        populator.populate(connection);
    } catch (ScriptStatementFailedException e) {
        // ignore
    } finally {
        DataSourceUtils.releaseConnection(connection, dataSource);
    }
}

From source file:org.copperengine.spring.SpringTransaction.java

public void run(PlatformTransactionManager transactionManager, DataSource dataSource, TransactionDefinition def)
        throws Exception {
    TransactionStatus txnStatus = transactionManager.getTransaction(def);
    try {//from ww w .jav a2  s .  com
        Connection con = DataSourceUtils.getConnection(dataSource);
        try {
            execute(con);
        } finally {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
    } catch (Exception e) {
        transactionManager.rollback(txnStatus);
        throw e;
    }
    transactionManager.commit(txnStatus);
}

From source file:org.hsweb.web.datasource.dynamic.DynamicDataSourceSqlExecutorService.java

@Override
public void releaseConnection(Connection connection) throws SQLException {
    DataSourceUtils.releaseConnection(connection, dynamicDataSource.getActiveDataSource());
}

From source file:org.jasig.cas.adaptors.jdbc.BindModeSearchDatabaseAuthenticationHandler.java

protected final boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials)
        throws AuthenticationException {
    final String username = credentials.getUsername();
    final String password = credentials.getPassword();

    try {/*from  ww w. ja v a 2  s.c o m*/
        final Connection c = this.getDataSource().getConnection(username, password);
        DataSourceUtils.releaseConnection(c, this.getDataSource());
        return true;
    } catch (final SQLException e) {
        return false;
    }
}

From source file:com.oreilly.springdata.jpa.AbstractIntegrationTest.java

/**
 * Populates the configured {@link DataSource} with data from {@code data.sql}.
 * /*from ww w .j ava2 s  .co m*/
 * @throws SQLException
 */
@Before
public void populateDatabase() throws SQLException {

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("data.sql"));

    Connection connection = null;

    try {
        connection = DataSourceUtils.getConnection(dataSource);
        populator.populate(connection);
    } finally {
        if (connection != null) {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }
}

From source file:com.backend.test.repository.AbstractIntegrationTest.java

/**
 * Populates the configured {@link DataSource} with data from {@code data.sql}.
 * //from   w ww.  j ava 2  s  .  c  o m
 * @throws SQLException
 */
@Before
public void populateDatabase() throws SQLException {

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("foodies.sql"));

    Connection connection = null;

    try {
        connection = DataSourceUtils.getConnection(dataSource);
        populator.populate(connection);
    } finally {
        if (connection != null) {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }
}

From source file:net.mlw.vlh.adapter.jdbc.spring.util.SpringConnectionCreator.java

public void close(ResultSet result, PreparedStatement statement, Connection connection) {
    JdbcUtils.closeResultSet(result);/*from   w w w .  j a va2s.  co m*/
    JdbcUtils.closeStatement(statement);
    // deprecated since spring 1.2
    DataSourceUtils.releaseConnection(connection, getDataSource());
}

From source file:org.hsweb.web.mybatis.SpringApplication.java

@Bean
public SqlExecutor sqlExecutor(DataSource dataSource) throws SQLException {
    Connection connection = dataSource.getConnection();
    try {/*from w w  w .j  a  v a  2  s. c  o m*/
        DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
    } finally {
        connection.close();
    }
    return new AbstractJdbcSqlExecutor() {
        @Override
        public Connection getConnection() {
            return DataSourceUtils.getConnection(dataSource);
        }

        @Override
        public void releaseConnection(Connection connection) throws SQLException {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    };
}

From source file:com.winit.vms.base.db.mybatis.support.SQLHelp.java

/**
 * /*  www  .  ja v  a2s  . c  om*/
 *
 * @param sql             SQL?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @param dialect         database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(final String sql, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountString(sql);
    logger.debug("Total count SQL [{}] ", count_sql);
    logger.debug("Total count Parameters: {} ", parameterObject);

    DataSource dataSource = mappedStatement.getConfiguration().getEnvironment().getDataSource();
    Connection connection = DataSourceUtils.getConnection(dataSource);
    PreparedStatement countStmt = null;
    ResultSet rs = null;
    try {
        countStmt = connection.prepareStatement(count_sql);
        //Page SQLCount SQL???boundSql
        DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject,
                boundSql);
        handler.setParameters(countStmt);

        rs = countStmt.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        logger.debug("Total count: {}", count);
        return count;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } finally {
            try {
                if (countStmt != null) {
                    countStmt.close();
                }
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        }
    }
}