Search in sources :

Example 31 with ConnectionPoolDataSource

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

the class InstanceKeyDataSource method testCPDS.

protected ConnectionPoolDataSource testCPDS(final String userName, final String userPassword) 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);
        }
        final Object ds = ctx.lookup(dataSourceName);
        if (!(ds instanceof ConnectionPoolDataSource)) {
            throw new SQLException("Illegal configuration: " + "DataSource " + dataSourceName + " (" + ds.getClass().getName() + ")" + " doesn't implement javax.sql.ConnectionPoolDataSource");
        }
        cpds = (ConnectionPoolDataSource) ds;
    }
    // try to get a connection with the supplied userName/password
    PooledConnection conn = null;
    try {
        if (userName != null) {
            conn = cpds.getPooledConnection(userName, userPassword);
        } 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 (final 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 32 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project narayana by jbosstm.

the class PerformanceTestCommitMarkableResource method testCommitMarkableResource.

// @org.junit.Ignore
@Test
public void testCommitMarkableResource() throws Exception {
    System.out.println("testCommitMarkableResource: " + new Date());
    ConnectionPoolDataSource dataSource = null;
    DataSource recoveryDataSource = null;
    if (dbType.equals("oracle")) {
        // ORA-01795: maximum number of expressions in a list is 1000
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(1000);
        Class clazz = Class.forName("oracle.jdbc.pool.OracleConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setDriverType", new Class[] { String.class }).invoke(dataSource, new Object[] { "thin" });
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz.getMethod("setNetworkProtocol", new Class[] { String.class }).invoke(dataSource, new Object[] { "tcp" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "orcl" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "dtf11" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "dtf11" });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 1521 });
        recoveryDataSource = (DataSource) dataSource;
    } else if (dbType.equals("sybase")) {
        // wide table support?
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(2000);
        Class clazz = Class.forName("com.sybase.jdbc3.jdbc.SybConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "192.168.1.5" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "LOCALHOST" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "sa" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "sybase" });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 5000 });
        Class clazz2 = Class.forName("com.sybase.jdbc3.jdbc.SybDataSource");
        recoveryDataSource = (DataSource) clazz2.newInstance();
        clazz2.getMethod("setServerName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "192.168.1.5" });
        clazz2.getMethod("setDatabaseName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "LOCALHOST" });
        clazz2.getMethod("setUser", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "sa" });
        clazz2.getMethod("setPassword", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "sybase" });
        clazz2.getMethod("setPortNumber", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 5000 });
    } else if (dbType.equals("h2")) {
        // Smaller batch size as H2 uses a hashtable in the delete which is
        // inefficent for bytearray clause
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(100);
        dataSource = new JdbcDataSource();
        ((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
        recoveryDataSource = ((JdbcDataSource) dataSource);
    } else if (dbType.equals("postgres")) {
        dataSource = new PGConnectionPoolDataSource();
        ((PGConnectionPoolDataSource) dataSource).setPortNumber(5432);
        ((PGConnectionPoolDataSource) dataSource).setUser("dtf11");
        ((PGConnectionPoolDataSource) dataSource).setPassword("dtf11");
        ((PGConnectionPoolDataSource) dataSource).setServerName("tywin.eng.hst.ams2.redhat.com");
        ((PGConnectionPoolDataSource) dataSource).setDatabaseName("jbossts");
        recoveryDataSource = new PGSimpleDataSource();
        ((PGSimpleDataSource) recoveryDataSource).setPortNumber(5432);
        ((PGSimpleDataSource) recoveryDataSource).setUser("dtf11");
        ((PGSimpleDataSource) recoveryDataSource).setPassword("dtf11");
        ((PGSimpleDataSource) recoveryDataSource).setServerName("tywin.eng.hst.ams2.redhat.com");
        ((PGSimpleDataSource) recoveryDataSource).setDatabaseName("jbossts");
    } else if (dbType.equals("mysql")) {
        // com.mysql.jdbc.PacketTooBigException: Packet for query is too
        // large (1318148 > 1048576). You can change this value on the
        // server by setting the max_allowed_packet' variable
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(3500);
        dataSource = new MysqlConnectionPoolDataSource();
        // need paranoid as otherwise it sends a connection change user
        ((MysqlConnectionPoolDataSource) dataSource).setUrl("jdbc:mysql://tywin.eng.hst.ams2.redhat.com:3306/jbossts?user=dtf11&password=dtf11&paranoid=true");
        recoveryDataSource = (DataSource) dataSource;
    } else if (dbType.equals("db2")) {
        Class clazz = Class.forName("com.ibm.db2.jcc.DB2ConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "BTDB1" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "db2" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "db2" });
        clazz.getMethod("setDriverType", new Class[] { int.class }).invoke(dataSource, new Object[] { 4 });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 50001 });
        Class clazz2 = Class.forName("com.ibm.db2.jcc.DB2DataSource");
        recoveryDataSource = (DataSource) clazz2.newInstance();
        clazz2.getMethod("setServerName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz2.getMethod("setDatabaseName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "BTDB1" });
        clazz2.getMethod("setUser", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "db2" });
        clazz2.getMethod("setPassword", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "db2" });
        clazz2.getMethod("setDriverType", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 4 });
        clazz2.getMethod("setPortNumber", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 50001 });
    } else if (dbType.equals("sqlserver")) {
        Class clazz = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "dev30.mw.lab.eng.bos.redhat.com" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setSendStringParametersAsUnicode", new Class[] { Boolean.class }).invoke(dataSource, new Object[] { false });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 3918 });
        recoveryDataSource = (DataSource) dataSource;
    }
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    Utils.createTables(pooledConnection.getConnection());
    doTest(new Handler(dataSource, recoveryDataSource), "_testCommitMarkableResource_" + dbType);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) JTAEnvironmentBean(com.arjuna.ats.jta.common.JTAEnvironmentBean) Date(java.util.Date) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) XADataSource(javax.sql.XADataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) MysqlXADataSource(com.mysql.cj.jdbc.MysqlXADataSource) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) MysqlConnectionPoolDataSource(com.mysql.cj.jdbc.MysqlConnectionPoolDataSource) Test(org.junit.Test)

Example 33 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project bboss by bbossgroups.

the class SharedPoolDataSource method registerPool.

private void registerPool(String username, String password) throws javax.naming.NamingException, SQLException {
    ConnectionPoolDataSource cpds = testCPDS(username, password);
    // Create an object pool to contain our PooledConnections
    GenericKeyedObjectPool tmpPool = new GenericKeyedObjectPool(null);
    tmpPool.setMaxActive(getMaxActive());
    tmpPool.setMaxIdle(getMaxIdle());
    tmpPool.setMaxWait(getMaxWait());
    tmpPool.setWhenExhaustedAction(whenExhaustedAction(maxActive, maxWait));
    tmpPool.setTestOnBorrow(getTestOnBorrow());
    tmpPool.setTestOnReturn(getTestOnReturn());
    tmpPool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    tmpPool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
    tmpPool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    tmpPool.setTestWhileIdle(getTestWhileIdle());
    pool = tmpPool;
    // 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)
    factory = new KeyedCPDSConnectionFactory(cpds, pool, getValidationQuery(), isRollbackAfterValidation());
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) GenericKeyedObjectPool(com.frameworkset.commons.pool.impl.GenericKeyedObjectPool)

Example 34 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project bboss by bbossgroups.

the class InstanceKeyDataSource method testCPDS.

protected ConnectionPoolDataSource testCPDS(final String userName, final String userPassword) 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);
        }
        final 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, userPassword);
        } 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 (final 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 35 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project bboss by bbossgroups.

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(), getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password);
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
    // Create an object pool to contain our PooledConnections
    final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<PooledConnectionAndInfo>(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.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
    pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
    pool.setMinIdle(getPerUserMinIdle(userName));
    pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
    pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
    pool.setTestOnCreate(getPerUserTestOnCreate(userName));
    pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
    pool.setTestOnReturn(getPerUserTestOnReturn(userName));
    pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
    pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));
    pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
    final Object 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(com.frameworkset.commons.dbcp2.SwallowedExceptionLogger) GenericObjectPool(com.frameworkset.commons.pool2.impl.GenericObjectPool)

Aggregations

ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)71 PooledConnection (javax.sql.PooledConnection)33 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)19 XAConnection (javax.sql.XAConnection)15 XADataSource (javax.sql.XADataSource)12 Statement (java.sql.Statement)8 CallableStatement (java.sql.CallableStatement)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)5 InitialContext (javax.naming.InitialContext)5 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 SQLiteConnectionPoolDataSource (org.sqlite.javax.SQLiteConnectionPoolDataSource)2