@Override public void reload() throws Exception { // switch data source BoneCPDataSource oldTargetDataSource = (BoneCPDataSource)proxyDataSource.getTargetDataSource(); BoneCPDataSource newTargetDataSource = createNewDataSource(); newTargetDataSource.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool proxyDataSource.setTargetDataSource(newTargetDataSource); oldTargetDataSource.close(); } });
/** * {@inheritDoc} * * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) */ public Connection getConnection(String username, String password) throws SQLException { try { return this.multiDataSource.get(new UsernamePassword(username, password)).getConnection(); } catch (ExecutionException e) { throw PoolUtil.generateSQLException("Unable to obtain connection", e); } }
result = connectionPool.getConnection();
public static DataSource getDataSource(File file) throws ClassNotFoundException, IOException, DaoException { Class.forName("org.h2.Driver"); BoneCPDataSource ds = new BoneCPDataSource(); ds.setPartitionCount(8); ds.setMaxConnectionsPerPartition(4); ds.setJdbcUrl("jdbc:h2:"+file.getAbsolutePath()); ds.setUsername("sa"); ds.setPassword(""); // Initialize the database to create files try { ds.getConnection().close(); } catch (SQLException e) { throw new DaoException(e); } return ds; } }
try (Connection testConn = dataSource.getConnection(); PreparedStatement stmt = testConn.prepareStatement(validationSQL))