List of usage examples for org.springframework.jdbc.datasource DataSourceUtils getConnection
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException
From source file:com.bluexml.side.Integration.alfresco.sql.synchronization.schemaManagement.SchemaCreation.java
private void checkMetaData() { logger.debug("Checking meta-data"); DatabaseMetaData dmd = null;/* ww w . j a v a 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: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;//from w ww . j a v 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(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:net.tirasa.connid.bundles.soap.wssample.ProvisioningImpl.java
/** * Establish a connection to underlying db. * * @return//from www .j av a2 s. co m * @throws ClassNotFoundException * @throws SQLException */ private Connection connect() throws SQLException { if (DefaultContentLoader.localDataSource == null) { LOG.error("Data Source is null"); return null; } final Connection conn = DataSourceUtils.getConnection(DefaultContentLoader.localDataSource); if (conn == null) { LOG.error("Connection is null"); } return conn; }
From source file:com.p5solutions.core.jpa.orm.EntityUtility.java
/** * Build the database-meta-data for all table entities. */// ww w. j a v a2 s . c o m protected void buildColumnMetaDataAll() { Connection connection = null; try { connection = DataSourceUtils.getConnection(dataSource); for (EntityDetail<?> detail : this.cacheEntityDetails.values()) { Table table = detail.getTableAnnotation(); if (table != null) { buildColumnMetaData(table, detail, connection); } } } catch (Exception e) { logger.error(e.toString()); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { ; } connection = null; } } }
From source file:cc.tooyoung.common.db.JdbcTemplate.java
/** * try 3 times to get connection/*w w w. ja v a 2 s. c o m*/ * @param ds * @return * @throws CannotGetJdbcConnectionException */ private Connection safeGetConnection(DataSource ds, boolean isWrite) throws CannotGetJdbcConnectionException { Connection con = null; int retryCount, count; retryCount = count = (isWrite ? writeTryGetConCount : readTryGetConCount); while (count-- > 0) { try { con = DataSourceUtils.getConnection(ds); return con; } catch (CannotGetJdbcConnectionException e) { ApiLogger.info(new StringBuilder(64).append("get connection try count:") .append((retryCount - count)).append(", ds=") .append(((com.mchange.v2.c3p0.ComboPooledDataSource) ds).getJdbcUrl())); DataSourceUtils.releaseConnection(con, ds); } } ApiLogger.fire(new StringBuffer().append("DB ") .append(((com.mchange.v2.c3p0.ComboPooledDataSource) ds).getJdbcUrl()).append(" Error:") .append("Could not get JDBC Connection: ")); throw new CannotGetJdbcConnectionException("Could not get JDBC Connection: " + ", ds=" + ((com.mchange.v2.c3p0.ComboPooledDataSource) ds).getJdbcUrl(), new SQLException()); }
From source file:lib.JdbcTemplate.java
@Override public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action) throws DataAccessException { Assert.notNull(psc, "PreparedStatementCreator must not be null"); Assert.notNull(action, "Callback object must not be null"); if (logger.isDebugEnabled()) { String sql = getSql(psc); logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : "")); }//from w w w .j a v a 2 s . c om Connection con = DataSourceUtils.getConnection(getDataSource()); PreparedStatement ps = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } ps = psc.createPreparedStatement(conToUse); applyStatementSettings(ps); PreparedStatement psToUse = ps; if (this.nativeJdbcExtractor != null) { psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps); } T result = action.doInPreparedStatement(psToUse); handleWarnings(ps); 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. if (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } String sql = getSql(psc); psc = null; JdbcUtils.closeStatement(ps); ps = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw getExceptionTranslator().translate("PreparedStatementCallback", sql, ex); } finally { if (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.releaseConnection(con, getDataSource()); } }
From source file:annis.administration.DefaultAdministrationDao.java
private void bulkloadTableFromResource(String table, Resource resource) { log.debug("bulk-loading data from '" + resource.getFilename() + "' into table '" + table + "'"); String sql = "COPY " + table + " FROM STDIN WITH DELIMITER E'\t' NULL AS 'NULL'"; try {/*from ww w. j av a2s. c o m*/ // retrieve the currently open connection if running inside a transaction Connection con = DataSourceUtils.getConnection(dataSource); // Postgres JDBC4 8.4 driver now supports the copy API PGConnection pgCon = (PGConnection) con; pgCon.getCopyAPI().copyIn(sql, resource.getInputStream()); DataSourceUtils.releaseConnection(con, dataSource); } catch (SQLException e) { throw new DatabaseAccessException(e); } catch (IOException e) { throw new FileAccessException(e); } }
From source file:lib.JdbcTemplate.java
@Override public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action) throws DataAccessException { Assert.notNull(csc, "CallableStatementCreator must not be null"); Assert.notNull(action, "Callback object must not be null"); if (logger.isDebugEnabled()) { String sql = getSql(csc); logger.debug("Calling stored procedure" + (sql != null ? " [" + sql + "]" : "")); }//from w w w .ja v a2 s.com Connection con = DataSourceUtils.getConnection(getDataSource()); CallableStatement cs = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } cs = csc.createCallableStatement(conToUse); applyStatementSettings(cs); CallableStatement csToUse = cs; if (this.nativeJdbcExtractor != null) { csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs); } T result = action.doInCallableStatement(csToUse); handleWarnings(cs); 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. if (csc instanceof ParameterDisposer) { ((ParameterDisposer) csc).cleanupParameters(); } String sql = getSql(csc); csc = null; JdbcUtils.closeStatement(cs); cs = null; DataSourceUtils.releaseConnection(con, getDataSource()); con = null; throw getExceptionTranslator().translate("CallableStatementCallback", sql, ex); } finally { if (csc instanceof ParameterDisposer) { ((ParameterDisposer) csc).cleanupParameters(); } JdbcUtils.closeStatement(cs); DataSourceUtils.releaseConnection(con, getDataSource()); } }
From source file:annis.administration.AdministrationDao.java
private void bulkloadTableFromResource(String table, Resource resource) { log.debug("bulk-loading data from '" + resource.getFilename() + "' into table '" + table + "'"); String sql = "COPY " + table + " FROM STDIN WITH DELIMITER E'\t' NULL AS 'NULL'"; try {// www .j a va2 s . c o m // retrieve the currently open connection if running inside a transaction Connection originalCon = DataSourceUtils.getConnection(getDataSource()); Connection con = originalCon; if (con instanceof DelegatingConnection) { DelegatingConnection<?> delCon = (DelegatingConnection<?>) con; con = delCon.getInnermostDelegate(); } Preconditions.checkState(con instanceof PGConnection, "bulk-loading only works with a PostgreSQL JDBC connection"); // Postgres JDBC4 8.4 driver now supports the copy API PGConnection pgCon = (PGConnection) con; pgCon.getCopyAPI().copyIn(sql, resource.getInputStream()); DataSourceUtils.releaseConnection(originalCon, getDataSource()); } catch (SQLException e) { throw new DatabaseAccessException(e); } catch (IOException e) { throw new FileAccessException(e); } }
From source file:org.agnitas.beans.impl.MailingImpl.java
@Override public boolean triggerMailing(int maildropStatusID, Hashtable<String, Object> opts, ApplicationContext con) { Mailgun aMailgun = null;/*from ww w . j av a2 s .c o m*/ DataSource ds = (DataSource) con.getBean("dataSource"); Connection dbCon = DataSourceUtils.getConnection(ds); boolean exitValue = true; try { if (maildropStatusID == 0) { throw new Exception("maildropStatusID is 0"); } aMailgun = (Mailgun) con.getBean("Mailgun"); aMailgun.initializeMailgun(Integer.toString(maildropStatusID)); aMailgun.prepareMailgun(new Hashtable<String, Object>()); aMailgun.executeMailgun(opts); } catch (Exception e) { logger.error("triggerMailing", e); exitValue = false; } DataSourceUtils.releaseConnection(dbCon, ds); return exitValue; }