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.dcache.chimera.DirectoryStreamImpl.java

DirectoryStreamImpl(FsInode dir, JdbcTemplate jdbc) {
    _jdbc = jdbc;/*from  w  w  w. j  a  v  a 2  s  .co  m*/

    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs;
    try {
        connection = DataSourceUtils.getConnection(_jdbc.getDataSource());
        ps = connection.prepareStatement(QUERY);
        ps.setFetchSize(50);
        ps.setLong(1, dir.ino());
        ps.setLong(2, dir.ino());
        ps.setLong(3, dir.ino());
        rs = ps.executeQuery();
    } catch (SQLException ex) {
        JdbcUtils.closeStatement(ps);
        DataSourceUtils.releaseConnection(connection, _jdbc.getDataSource());
        throw _jdbc.getExceptionTranslator().translate("StatementExecution", QUERY, ex);
    }
    _connection = connection;
    _resultSet = rs;
    _statement = ps;
}

From source file:au.com.breakpoint.hedron.core.context.JdbcConnectionCachingDataSource.java

public void closeConnection() {
    // Allow the Spring framework to close the connection. releaseConnection () calls
    // shouldClose ().
    m_shouldClose = true;/*from  w w w  .j ava 2 s  . co  m*/
    DataSourceUtils.releaseConnection(m_connection, this);
    m_connection = null;
}

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

@After
public void after() throws Exception {
    log.debug("After DbTestCase");
    if (connection != null)
        DataSourceUtils.releaseConnection(connection, ds);
}

From source file:com.bluexml.side.Integration.alfresco.sql.synchronization.common.JdbcTransactionListener.java

private void releaseConnection(Connection connection) {
    DataSourceUtils.releaseConnection(connection, dataSource);
    AlfrescoTransactionSupport.unbindResource(SYNCHRO_CONTEXT_KEY);
}

From source file:org.dcache.chimera.DirectoryStreamImpl.java

public void close() throws IOException {
    try {/*from  w w w  . j  a v  a 2  s.  com*/
        JdbcUtils.closeResultSet(_resultSet);
        JdbcUtils.closeStatement(_statement);
        DataSourceUtils.releaseConnection(_connection, _jdbc.getDataSource());
    } catch (DataAccessException e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:com.chillenious.common.db.jooq.SpringConnectionProvider.java

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

From source file:com.br.helpdesk.repository.MenuRepositoryTest.java

@Before
public void setupDB() throws Exception {
    FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
    builder.setColumnSensing(true);/*  w  w w.j  ava2 s.c  o  m*/
    Connection con = DataSourceUtils.getConnection(dataSource);
    IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
    IDataSet dataSet = builder.build(new File("src/main/resources/com/br/helpdesk/repository/MenuData.xml"));
    ReplacementDataSet replacementDataSet = new ReplacementDataSet(dataSet);
    replacementDataSet.addReplacementObject("[NULL]", null);
    DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, replacementDataSet);
    DataSourceUtils.releaseConnection(con, dataSource);
}

From source file:org.syncope.buildtools.H2StartStopListener.java

@Override
public void contextInitialized(final ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();

    File workDir = (File) sce.getServletContext().getAttribute("javax.servlet.context.tempdir");
    try {/*from  w  ww  . j av  a 2s.  co m*/
        Server h2TestDb = new Server();
        h2TestDb.runTool("-baseDir", workDir.getAbsolutePath(), "-tcp", "-tcpDaemon", "-web", "-webDaemon",
                "-webPort", sce.getServletContext().getInitParameter("testdb.webport"));

        context.setAttribute(H2_TESTDB, h2TestDb);
    } catch (SQLException e) {
        LOG.error("Could not start H2 test db", e);
    }

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
    DataSource datasource = ctx.getBean(DataSource.class);

    Connection conn = null;
    Statement stmt = null;
    try {
        conn = DataSourceUtils.getConnection(datasource);
        stmt = conn.createStatement();
        stmt.executeUpdate("RUNSCRIPT FROM 'classpath:/testdb.sql'");
    } catch (Exception e) {
        LOG.error("While loading data into testdb", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
        DataSourceUtils.releaseConnection(conn, datasource);
    }
}

From source file:com.healthcit.cacure.utils.DBSchemaUpdater.java

@Override
public void afterPropertiesSet() throws Exception {
    Connection connection = DataSourceUtils.getConnection(dataSource);
    connection.setAutoCommit(false);/*from   w w  w .  j a v a  2 s  .  c o m*/
    try {
        Statement statement = connection.createStatement();
        try {
            long version = 0;
            try {
                ResultSet rs = statement.executeQuery("select schema_version from sys_variables limit 1;");
                try {
                    if (!rs.next()) {
                        throw new RuntimeException("Seems there is no any row in sys_variables table.");
                    }
                    version = rs.getLong(1);
                } finally {
                    rs.close();
                }
            } catch (PSQLException e) {
                //               it's needed for executing more scripts successfully
                connection.rollback();
                log.info("Can't find sys_variables tables. Appling initial script.");
                String initialScriptStatements = getStatementsFor(0);
                if (initialScriptStatements == null) {
                    throw new RuntimeException("Can't find initial script.");
                }
                statement.executeUpdate(initialScriptStatements);
                //there is already schema_version at 0
                connection.commit();
                log.info("Initial script succesfully executed.");
            }
            for (long v = version + 1;; v++) {
                String statements = getStatementsFor(v);
                if (statements == null) {
                    break;
                }
                log.info("Updating schema to " + v + " version...");
                statement.execute(statements);
                statement.executeUpdate("update sys_variables set schema_version = " + v + ";");
                connection.commit();
                log.info("OK");
            }
        } catch (BatchUpdateException e) {
            if (e.getNextException() != null) {
                e.getNextException().printStackTrace();
            }
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            connection.rollback();
        } finally {
            statement.close();
        }
    } finally {
        DataSourceUtils.releaseConnection(connection, dataSource);
    }
}

From source file:org.dalesbred.integration.spring.SpringTransactionManager.java

private <T> T execute(@NotNull TransactionCallback<T> callback, @NotNull Dialect dialect,
        @NotNull DefaultTransactionDefinition df) {
    TransactionTemplate tt = new TransactionTemplate(platformTransactionManager, df);
    return tt.execute(status -> {
        try {/*from   w  ww.  j a v  a2 s  . c om*/
            Connection connection = DataSourceUtils.getConnection(dataSource);
            try {
                return callback.execute(new SpringTransactionContext(status, connection));
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        } catch (SQLException e) {
            throw dialect.convertException(e);
        }
    });
}