List of usage examples for org.springframework.jdbc.datasource DataSourceUtils getConnection
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException
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"); }//ww w. j a v a2 s. com 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:work.db.CustomJDBCAppender.java
@Override protected java.sql.Connection getConnection() throws java.sql.SQLException { if (connection == null) { org.springframework.jdbc.datasource.DataSourceTransactionManager dstm = (org.springframework.jdbc.datasource.DataSourceTransactionManager) ExternalBeanFactory .getBean("txManager"); if (dstm == null) throw new java.sql.SQLException("dstm is null"); System.out.println("here"); connection = DataSourceUtils.getConnection(dstm.getDataSource()); return connection; } else {/* www .ja v a2 s . c om*/ return connection; } }
From source file:org.skife.jdbi.v2.spring.SpringDataSourceConnectionFactory.java
@Override public Connection openConnection() throws SQLException { return DataSourceUtils.getConnection(dataSource); }
From source file:fi.luontola.cqrshotel.JdbcConfiguration.java
@PostConstruct public void statusReport() throws SQLException { try (Connection connection = DataSourceUtils.getConnection(dataSource)) { DatabaseMetaData metaData = connection.getMetaData(); log.info("Database: {} {}", metaData.getDatabaseProductName(), metaData.getDatabaseProductVersion()); log.info("User: {}", metaData.getUserName()); log.info("Connection URL: {} (configuration was {})", metaData.getURL(), dataSourceProperties.getUrl()); log.info("Flyway locations: {}", flywayProperties.getLocations()); }//from w w w . j a v a 2 s .c om }
From source file:de.brands4friends.daleq.spring.SpringConnectionFactory.java
@Override @SuppressWarnings("PMD.CloseResource") // this is a factory. it will not close the resource for sure! protected Connection getConnection(final DataSource dataSource) throws SQLException { return DataSourceUtils.getConnection(dataSource); }
From source file:com.winit.vms.base.db.mybatis.support.SQLHelp.java
/** * /*from w w w .j av a2 s. 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); } } } }
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 ava 2s . 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.copperengine.spring.SpringTransaction.java
public void run(PlatformTransactionManager transactionManager, DataSource dataSource, TransactionDefinition def) throws Exception { TransactionStatus txnStatus = transactionManager.getTransaction(def); try {/*from w w w . j av a 2 s.co m*/ 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:com.jp.systemdirector.projectzero.zab01.db.dao.ConnectionUtilForSpring.java
/** * <p>Connection ??</p>/*ww w .j a va 2 s . c om*/ * @return Connection * @throws SQLException */ public Connection getConnection() throws SQLException { // ApplicationContext?dataSource? ApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); dataSource = (DataSource) context.getBean("dataSource"); conn = DataSourceUtils.getConnection(dataSource); return conn; }
From source file:org.javersion.json.web.JsonStoreApplication.java
@Bean public SQLQueryFactory queryFactory(final DataSource dataSource) { com.mysema.query.sql.Configuration configuration = new com.mysema.query.sql.Configuration( new H2Templates()); ObjectVersionStoreJdbc.registerTypes(configuration); return new SQLQueryFactory(configuration, () -> DataSourceUtils.getConnection(dataSource)); }