Search in sources :

Example 71 with ConnectionPoolDataSource

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;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) InitialContext(javax.naming.InitialContext)

Example 72 with ConnectionPoolDataSource

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();
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Properties(java.util.Properties)

Aggregations

ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)72 PooledConnection (javax.sql.PooledConnection)34 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)19 XAConnection (javax.sql.XAConnection)15 XADataSource (javax.sql.XADataSource)12 Statement (java.sql.Statement)9 CallableStatement (java.sql.CallableStatement)7 DataSource (javax.sql.DataSource)7 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)6 JDBCDataSource (org.apache.derbyTesting.junit.JDBCDataSource)6 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 Properties (java.util.Properties)3 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Savepoint (java.sql.Savepoint)2 SQLiteConnectionPoolDataSource (org.sqlite.javax.SQLiteConnectionPoolDataSource)2