List of usage examples for org.springframework.jdbc.datasource DataSourceUtils releaseConnection
public static void releaseConnection(@Nullable Connection con, @Nullable DataSource dataSource)
From source file:org.cfr.capsicum.test.AbstractCayenneJUnit4DbUnitSpringContextTests.java
protected void loadDataSet(URL url) throws SQLException, IOException, DatabaseUnitException { // initialize your database connection here IDatabaseConnection connection = new DatabaseConnection(DataSourceUtils.getConnection(getDataSource())); // DatabaseConfig config = connection.getConfig(); // config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, // true);/* w ww.j a v a 2 s .c o m*/ // initialize your dataset here IDataSet dataSet = getSrcDataSet(url, producerType, false); try { DatabaseOperation.INSERT.execute(connection, dataSet); } finally { DataSourceUtils.releaseConnection(connection.getConnection(), getDataSource()); } }
From source file:org.syncope.core.util.ImportExport.java
public void export(final OutputStream os) throws SAXException, TransformerConfigurationException, CycleInMultiParentTreeException { StreamResult streamResult = new StreamResult(os); SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = transformerFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(streamResult);/*from ww w . ja va 2s .c o m*/ handler.startDocument(); handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl()); Connection conn = DataSourceUtils.getConnection(dataSource); ResultSet rs = null; try { // first read all tables... rs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); Set<String> tableNames = new HashSet<String>(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); // these tables must be ignored if (!tableName.toUpperCase().startsWith("QRTZ_") && !tableName.toUpperCase().equals("ACT_GE_PROPERTY")) { tableNames.add(tableName); } } // then sort tables based on foreign keys and dump for (String tableName : sortByForeignKeys(conn, tableNames)) { doExportTable(handler, conn, tableName); } } catch (SQLException e) { LOG.error("While exporting database content", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("While closing tables result set", e); } } DataSourceUtils.releaseConnection(conn, dataSource); } handler.endElement("", "", ROOT_ELEMENT); handler.endDocument(); }
From source file:org.cfr.capsicum.test.AbstractCayenneJUnit4DbUnitSpringContextTests.java
/** * Executes a set of commands to drop/create database objects. *//*from w ww.j a v a 2s . c o m*/ protected boolean safeExecute(String sql) throws SQLException { Connection connection = DataSourceUtils.getConnection(dataSource); Statement statement = connection.createStatement(); try { statement.execute(sql); return true; } catch (SQLException ex) { return false; } finally { statement.close(); DataSourceUtils.releaseConnection(connection, dataSource); } }
From source file:lib.JdbcTemplate.java
@Override public <T> T execute(ConnectionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(getDataSource()); try {// w w w. j a v a 2 s.com Connection conToUse = con; if (this.nativeJdbcExtractor != null) { // Extract native JDBC Connection, castable to OracleConnection or the like. conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } else { // Create close-suppressing Connection proxy, also preparing returned Statements. 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. DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw getExceptionTranslator().translate("ConnectionCallback", getSql(action), ex); } finally { DataSourceUtils.releaseConnection(con, getDataSource()); } }
From source file:org.cfr.capsicum.test.AbstractCayenneJUnit4DbUnitSpringContextTests.java
/** * Set the DataSource, typically provided via Dependency Injection. *///from ww w . j a va 2 s .c om @Autowired(required = false) public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); if (this.dataSource != null) { Connection con = null; try { con = DataSourceUtils.getConnection(dataSource); adapter = AdapterFactory.getCurrentAdapter(con); } catch (SQLException e) { throw new RuntimeException(e); } finally { DataSourceUtils.releaseConnection(con, dataSource); } } }
From source file:com.bluexml.side.Integration.alfresco.sql.synchronization.schemaManagement.SchemaCreation.java
private void checkMetaData() { logger.debug("Checking meta-data"); DatabaseMetaData dmd = null;/*w w w . j a va 2 s . c o m*/ Connection connection = DataSourceUtils.getConnection(dataSource); try { dmd = connection.getMetaData(); String dbname = dmd.getDatabaseProductName(); String dbversion = dmd.getDatabaseProductVersion(); if (logger.isDebugEnabled()) logger.debug("Running sql synchronization on " + dbname + " " + dbversion); } catch (SQLException e) { logger.error(e); } finally { DataSourceUtils.releaseConnection(connection, dataSource); } }
From source file:org.apache.syncope.core.util.ImportExport.java
public void export(final OutputStream os) throws SAXException, TransformerConfigurationException { StreamResult streamResult = new StreamResult(os); final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory .newInstance();/*from ww w. j a v a 2 s. co m*/ TransformerHandler handler = transformerFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(streamResult); handler.startDocument(); handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl()); final Connection conn = DataSourceUtils.getConnection(dataSource); ResultSet rs = null; try { final DatabaseMetaData meta = conn.getMetaData(); final String schema = readSchema(); rs = meta.getTables(null, schema, null, new String[] { "TABLE" }); final Set<String> tableNames = new HashSet<String>(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); // these tables must be ignored if (!tableName.toUpperCase().startsWith("QRTZ_") && !tableName.toUpperCase().startsWith("LOGGING_")) { tableNames.add(tableName); } } // then sort tables based on foreign keys and dump for (String tableName : sortByForeignKeys(conn, tableNames, schema)) { doExportTable(handler, conn, tableName); } } catch (SQLException e) { LOG.error("While exporting database content", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("While closing tables result set", e); } } DataSourceUtils.releaseConnection(conn, dataSource); } handler.endElement("", "", ROOT_ELEMENT); handler.endDocument(); }
From source file:lib.JdbcTemplate.java
@Override public <T> T execute(StatementCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null;/* w w w .j ava 2 s . co m*/ try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } stmt = conToUse.createStatement(); applyStatementSettings(stmt); Statement stmtToUse = stmt; if (this.nativeJdbcExtractor != null) { stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt); } T result = action.doInStatement(stmtToUse); 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. JdbcUtils.closeStatement(stmt); stmt = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } }
From source file:cc.tooyoung.common.db.JdbcTemplate.java
public Object execute(StatementCallback action, boolean isWrite) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); long start = System.currentTimeMillis(); DataSource ds = getDataSource(isWrite); Connection con = safeGetConnection(ds, isWrite); Statement stmt = null;// w w w. jav a 2 s .c o m try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } stmt = conToUse.createStatement(); applyStatementSettings(ds, stmt); Statement stmtToUse = stmt; if (this.nativeJdbcExtractor != null) { stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt); } Object result = action.doInStatement(stmtToUse); handleWarnings(stmt); return result; } catch (Exception ex) { // Release Connection early, to avoid potential connection pool deadlock // in the case when the exception translator hasn't been initialized yet. JdbcUtils.closeStatement(stmt); stmt = null; DataSourceUtils.releaseConnection(con, ds); con = null; if (ex instanceof SQLException) { throw getExceptionTranslator(ds).translate("StatementCallback", getSql(action), (SQLException) ex); } else { throw new RuntimeException("StatementCallback " + getSql(action), ex); } } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, ds); //add slow log long useTime = System.currentTimeMillis() - start; if (useTime > ApiLogger.DB_FIRE_TIME) { ApiLogger.fire(new StringBuffer().append("DB ") .append(((com.mchange.v2.c3p0.ComboPooledDataSource) ds).getJdbcUrl()).append(" too slow :") .append(useTime).append(" isWrite:").append(isWrite)); } TimeStatUtil.addElapseTimeStat(resource, isWrite, start, useTime); } }
From source file:net.tirasa.connid.bundles.soap.wssample.ProvisioningImpl.java
/** * Close connection to underlying db/*from w w w . j a v a 2s.c o m*/ * * @throws SQLException */ private void close(final Connection conn) throws SQLException { DataSourceUtils.releaseConnection(conn, DefaultContentLoader.localDataSource); }