use of javax.sql.ConnectionPoolDataSource in project bboss by bbossgroups.
the class InstanceKeyDataSource method testCPDS.
protected ConnectionPoolDataSource testCPDS(String username, String password) throws javax.naming.NamingException, SQLException {
// The source of physical db connections
ConnectionPoolDataSource cpds = this.dataSource;
if (cpds == null) {
Context ctx = null;
if (jndiEnvironment == null) {
ctx = new InitialContext();
} else {
ctx = new InitialContext(jndiEnvironment);
}
Object ds = ctx.lookup(dataSourceName);
if (ds instanceof ConnectionPoolDataSource) {
cpds = (ConnectionPoolDataSource) ds;
} else {
throw new SQLException("Illegal configuration: " + "DataSource " + dataSourceName + " (" + ds.getClass().getName() + ")" + " doesn't implement javax.sql.ConnectionPoolDataSource");
}
}
// try to get a connection with the supplied username/password
PooledConnection conn = null;
try {
if (username != null) {
conn = cpds.getPooledConnection(username, password);
} else {
conn = cpds.getPooledConnection();
}
if (conn == null) {
throw new SQLException("Cannot connect using the supplied username/password");
}
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// at least we could connect
}
}
}
return cpds;
}
use of javax.sql.ConnectionPoolDataSource in project mssql-jdbc by microsoft.
the class DataFactoryTest method verifyFactoryPooledConnection.
private void verifyFactoryPooledConnection(DataSourceFactory dsFactory) throws SQLException {
Properties props = new Properties();
props.setProperty(DataSourceFactory.JDBC_URL, connectionString);
ConnectionPoolDataSource ds = dsFactory.createConnectionPoolDataSource(props);
PooledConnection c = ds.getPooledConnection();
try (Statement s = c.getConnection().createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT 1")) {
assertTrue("Resultset is empty.", rs.next());
}
} finally {
c.close();
}
}
Aggregations