List of usage examples for org.springframework.jdbc.datasource DataSourceUtils getConnection
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException
From source file:org.springframework.batch.item.database.AbstractCursorItemReader.java
protected void initializeConnection() { Assert.state(getDataSource() != null, "DataSource must not be null."); try {/*from ww w . j av a2s . c o m*/ if (useSharedExtendedConnection) { if (!(getDataSource() instanceof ExtendedConnectionDataSourceProxy)) { throw new InvalidDataAccessApiUsageException( "You must use a ExtendedConnectionDataSourceProxy for the dataSource when " + "useSharedExtendedConnection is set to true."); } this.con = DataSourceUtils.getConnection(dataSource); ((ExtendedConnectionDataSourceProxy) dataSource).startCloseSuppression(this.con); } else { this.con = dataSource.getConnection(); } } catch (SQLException se) { close(); throw getExceptionTranslator().translate("Executing query", getSql(), se); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(final StatementCallback action) { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null;/*from w w w . ja va 2s . c o m*/ try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } stmt = conToUse.createStatement(); DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); Statement stmtToUse = stmt; if (this.nativeJdbcExtractor != null) { stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt); } Object result = action.doInStatement(stmtToUse); SQLWarning warning = stmt.getWarnings(); throwExceptionOnWarningIfNotIgnoringWarnings(warning); return result; } catch (SQLException ex) { throw getExceptionTranslator().translate("executing StatementCallback", getSql(action), ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.closeConnectionIfNecessary(con, getDataSource()); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action) { //??/*from ww w .ja v a 2 s . c o m*/ 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); DataSourceUtils.applyTransactionTimeout(ps, getDataSource()); PreparedStatement psToUse = ps; if (this.nativeJdbcExtractor != null) { psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps); } // Object result = action.doInPreparedStatement(psToUse); SQLWarning warning = ps.getWarnings(); //??? throwExceptionOnWarningIfNotIgnoringWarnings(warning); return result; } catch (SQLException ex) { throw getExceptionTranslator().translate("executing PreparedStatementCallback [" + psc + "]", getSql(psc), ex); } finally {//? if (psc instanceof ParameterDisposer) { ((ParameterDisposer) psc).cleanupParameters(); } JdbcUtils.closeStatement(ps); DataSourceUtils.closeConnectionIfNecessary(con, getDataSource()); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object execute(CallableStatementCreator csc, CallableStatementCallback action) { if (logger.isDebugEnabled()) { String sql = getSql(csc); logger.debug("Calling stored procedure" + (sql != null ? " [" + sql + "]" : "")); }/* w ww .j av a 2 s . c o m*/ Connection con = DataSourceUtils.getConnection(getDataSource()); CallableStatement cs = null; try { Connection conToUse = con; if (this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeCallableStatements()) { conToUse = this.nativeJdbcExtractor.getNativeConnection(con); } cs = csc.createCallableStatement(conToUse); DataSourceUtils.applyTransactionTimeout(cs, getDataSource()); CallableStatement csToUse = cs; if (this.nativeJdbcExtractor != null) { csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs); } Object result = action.doInCallableStatement(csToUse); SQLWarning warning = cs.getWarnings(); throwExceptionOnWarningIfNotIgnoringWarnings(warning); return result; } catch (SQLException ex) { throw getExceptionTranslator().translate("executing CallableStatementCallback [" + csc + "]", getSql(csc), ex); } finally { if (csc instanceof ParameterDisposer) { ((ParameterDisposer) csc).cleanupParameters(); } JdbcUtils.closeStatement(cs); DataSourceUtils.closeConnectionIfNecessary(con, getDataSource()); } }
From source file:org.springframework.jdbc.support.JdbcUtils.java
/** * Extract database meta data via the given DatabaseMetaDataCallback. * <p>This method will open a connection to the database and retrieve the database metadata. * Since this method is called before the exception translation feature is configured for * a datasource, this method can not rely on the SQLException translation functionality. * <p>Any exceptions will be wrapped in a MetaDataAccessException. This is a checked exception * and any calling code should catch and handle this exception. You can just log the * error and hope for the best, but there is probably a more serious error that will * reappear when you try to access the database again. * @param dataSource the DataSource to extract metadata for * @param action callback that will do the actual work * @return object containing the extracted information, as returned by * the DatabaseMetaDataCallback's {@code processMetaData} method * @throws MetaDataAccessException if meta data access failed *//* ww w. j a v a2s . c om*/ public static Object extractDatabaseMetaData(DataSource dataSource, DatabaseMetaDataCallback action) throws MetaDataAccessException { Connection con = null; try { con = DataSourceUtils.getConnection(dataSource); DatabaseMetaData metaData = con.getMetaData(); if (metaData == null) { // should only happen in test environments throw new MetaDataAccessException("DatabaseMetaData returned by Connection [" + con + "] was null"); } return action.processMetaData(metaData); } catch (CannotGetJdbcConnectionException ex) { throw new MetaDataAccessException("Could not get Connection for extracting meta data", ex); } catch (SQLException ex) { throw new MetaDataAccessException("Error while extracting DatabaseMetaData", ex); } catch (AbstractMethodError err) { throw new MetaDataAccessException( "JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver", err); } finally { DataSourceUtils.releaseConnection(con, dataSource); } }
From source file:org.unitils.database.core.DataSourceWrapper.java
/** * @return A connection from the data source, not null *///from w w w. j av a 2 s .c o m public Connection getConnection() { try { return DataSourceUtils.getConnection(wrappedDataSource); } catch (Exception e) { throw new UnitilsException("Unable to connect to database for " + databaseConfiguration + ".", e); } }