Search in sources :

Example 26 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project derby by apache.

the class DataSourcePropertiesTest method embeddedTestAttributesAsPasswordWithPassword_pooled.

/**
 * Tests that the <code>attributesAsPassword</code> property of a
 * <code>ConnectionPoolDataSource</code> causes an explicitly specified
 * password to be sent as a property string.
 */
public void embeddedTestAttributesAsPasswordWithPassword_pooled() throws Exception {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
    try {
        PooledConnection pc = ds.getPooledConnection("username", "mypassword");
        fail("Expected getPooledConnection to fail.");
    } catch (SQLException e) {
        // expect error because of malformed url
        assertSQLState("XJ028", e);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException)

Example 27 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project derby by apache.

the class PoolDSAuthenticationTest method assertConnectionFail.

public void assertConnectionFail(String dbName) throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    // a valid name, so reset to the default
    if (usingDerbyNetClient())
        JDBCDataSource.setBeanProperty(pds, "user", "APP");
    else
        JDBCDataSource.clearStringBeanProperty(pds, "user");
    JDBCDataSource.clearStringBeanProperty(pds, "password");
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    try {
        pds.getPooledConnection();
        fail("expected connection to fail");
    } catch (SQLException e) {
        assertSQLState("08004", e);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) SQLException(java.sql.SQLException)

Example 28 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project derby by apache.

the class DeclareGlobalTempTableJavaJDBC30Test method testPooledConnectionClosed.

/**
 *  Temporary tables declared in a pooled connection should get dropped
 *  when that pooled connection is closed.
 *
 *  @throws SQLException
 */
public void testPooledConnectionClosed() throws SQLException {
    ConnectionPoolDataSource dscsp = J2EEDataSource.getConnectionPoolDataSource();
    // In the first connection handle to the pooled connection, create
    // physical session schema, create table t1 in it
    PooledConnection pc = dscsp.getPooledConnection();
    Connection pcon = pc.getConnection();
    Statement s = pcon.createStatement();
    try {
        s.executeUpdate("CREATE schema SESSION");
    } catch (SQLException e) {
        assertSQLState("X0Y68", e);
    }
    s.executeUpdate("CREATE TABLE SESSION.tx(c21 int)");
    s.executeUpdate("insert into SESSION.tx values(11)");
    s.executeUpdate("insert into SESSION.tx values(12)");
    s.executeUpdate("insert into SESSION.tx values(13)");
    JDBC.assertSingleValueResultSet(s.executeQuery("select count(*) from SESSION.tx"), "3");
    // Declare temp table with same name as physical table in SESSION schema
    s.executeUpdate("declare global temporary table SESSION.tx(" + "c11 int, c12 int) on commit preserve rows not logged");
    s.executeUpdate("insert into SESSION.tx values(11,1)");
    JDBC.assertSingleValueResultSet(s.executeQuery("select count(*) from SESSION.tx"), "1");
    commit();
    // Now close the connection handle to the pooled connection
    getConnection().close();
    // Do another getConnection() to get a new connection handle to the
    // pooled connection
    s = getConnection().createStatement();
    // In this new handle, a select * from SESSION.tx should be looking at
    // the physical session table
    JDBC.assertSingleValueResultSet(s.executeQuery("select count(*) from SESSION.tx"), "3");
    s.executeUpdate("drop table SESSION.tx");
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection)

Example 29 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)

Example 30 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project commons-dbcp by apache.

the class PerUserPoolDataSource method registerPool.

private synchronized void registerPool(final String userName, final String password) throws NamingException, SQLException {
    final ConnectionPoolDataSource cpds = testCPDS(userName, password);
    // Set up the factory we will use (passing the pool associates
    // the factory with the pool, so we do not have to do so
    // explicitly)
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeoutDuration(), isRollbackAfterValidation(), userName, password);
    factory.setMaxConn(getMaxConnDuration());
    // Create an object pool to contain our PooledConnections
    final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
    pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
    pool.setLifo(getPerUserLifo(userName));
    pool.setMaxIdle(getPerUserMaxIdle(userName));
    pool.setMaxTotal(getPerUserMaxTotal(userName));
    pool.setMaxWait(Duration.ofMillis(getPerUserMaxWaitMillis(userName)));
    pool.setMinEvictableIdle(getPerUserMinEvictableIdleDuration(userName));
    pool.setMinIdle(getPerUserMinIdle(userName));
    pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
    pool.setSoftMinEvictableIdle(getPerUserSoftMinEvictableIdleDuration(userName));
    pool.setTestOnCreate(getPerUserTestOnCreate(userName));
    pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
    pool.setTestOnReturn(getPerUserTestOnReturn(userName));
    pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
    pool.setTimeBetweenEvictionRuns(getPerUserDurationBetweenEvictionRuns(userName));
    pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
    final PooledConnectionManager old = managers.put(getPoolKey(userName), factory);
    if (old != null) {
        throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) SwallowedExceptionLogger(org.apache.commons.dbcp2.SwallowedExceptionLogger) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)63 PooledConnection (javax.sql.PooledConnection)29 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)17 XAConnection (javax.sql.XAConnection)15 XADataSource (javax.sql.XADataSource)12 CallableStatement (java.sql.CallableStatement)7 Statement (java.sql.Statement)7 DataSource (javax.sql.DataSource)7 PreparedStatement (java.sql.PreparedStatement)6 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)6 JDBCDataSource (org.apache.derbyTesting.junit.JDBCDataSource)6 ResultSet (java.sql.ResultSet)5 Context (javax.naming.Context)3 InitialContext (javax.naming.InitialContext)3 Test (org.junit.Test)3 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 JTAEnvironmentBean (com.arjuna.ats.jta.common.JTAEnvironmentBean)1 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)1