javax.sql.ConnectionPoolDataSource Java Examples

The following examples show how to use javax.sql.ConnectionPoolDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies that the schema is reset when creating a new logical connection.
 * <p>
 * The test is run in a non-statement pooling configuration first,
 * and then with statement pooling enabled if the environment supports it.
 * <p>
 * Relevant Jira issue: DERBY-3690.
 *
 * @throws SQLException if something goes wrong
 */
public void testSchemaIsReset()
        throws SQLException {
    final String userSchema = "USERSCHEMA";
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    // Connect with a user specified, which should cause the schema to be
    // set to the user name.
    // Test without statement pooling first.
    doTestSchemaIsReset(cpDs.getPooledConnection(userSchema, "secret"),
            userSchema);

    // Try to enable statement pooling.
    // This is currently only implemented in the client driver.
    if (usingDerbyNetClient()) {
        J2EEDataSource.setBeanProperty(
                cpDs, "maxStatements",new Integer(7));
        doTestSchemaIsReset(cpDs.getPooledConnection(userSchema, "secret"),
                userSchema);
    }
}
 
Example #2
Source File: StatementPoolingTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the statement cache is able to throw out prepared statements
 * when it reaches maximum capacity.
 *
 * @throws SQLException if something goes wrong...
 */
public void testCacheOverflow()
        throws SQLException {
    final int stmtCount = 150;
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "maxStatements", new Integer(11));
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    PooledConnection pc = cpDs.getPooledConnection();
    Connection con = pc.getConnection();
    for (int i=0; i < stmtCount; i++) {
        // Yes, the "values + i" is intended here.
        PreparedStatement pStmt = con.prepareStatement("values " + i);
        ResultSet rs = pStmt.executeQuery();
        JDBC.assertSingleValueResultSet(rs, Integer.toString(i));
        pStmt.close();
    }
    con.close();
    pc.close();
}
 
Example #3
Source File: ConnectionPoolDataSourceConnector.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public Connection openConnection(String databaseName) throws SQLException {
    JDBCDataSource.setBeanProperty(ds, "databaseName", databaseName);
    try {
        return ds.getPooledConnection().getConnection();
    } catch (SQLException e) {
        // Expected state for database not found.
        // For the client the generic 08004 is returned,
        // will just retry on that.
        String expectedState = 
            config.getJDBCClient().isEmbedded() ? "XJ004" : "08004";

        // If there is a database not found exception
        // then retry the connection request with
        // a new DataSource with the createDtabase property set.
        if (!expectedState.equals(e.getSQLState()))
            throw e;
        ConnectionPoolDataSource tmpDs =
                singleUseDS("createDatabase", "create");
        JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
        return tmpDs.getPooledConnection().getConnection();
   }
}
 
Example #4
Source File: RolesTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Verifies that the current role is reset when creating a new logical
 * connection.
 * <p>
 * The test is run in a non-statement pooling configuration first,
 * and then with statement pooling enabled if the environment supports it.
 * <p>
 * The test pattern is borrowed from the test case in J2EEDataSourceTest.
 *
 * @see com.splicemachine.dbTesting.functionTests.tests.jdbcapi.J2EEDataSourceTest#testSchemaIsReset
 *
 * @throws SQLException if something goes wrong
 */
private void testCurrentRoleIsReset()
        throws SQLException {

    if (_authLevel == SQLAUTHORIZATION && isDbo() /* once is enough */) {
        final String user = "DonaldDuck";
        final String passwd = user.concat(pwSuffix);
        ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
        // Test without statement pooling first.
        doTestCurrentRoleIsReset(cpDs.getPooledConnection(user, passwd),
                                 user);

        // Try to enable statement pooling.
        // This is currently only implemented in the client driver.
        if (usingDerbyNetClient()) {
            J2EEDataSource.setBeanProperty(
                cpDs, "maxStatements",new Integer(7));
            doTestCurrentRoleIsReset(cpDs.getPooledConnection(user, passwd),
                                     user);
        }
    }
}
 
Example #5
Source File: MiniConnectionPoolManager.java    From fixflow with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a MiniConnectionPoolManager object.
 * 
 * @param dataSource
 *            the data source for the connections.
 * @param maxConnections
 *            the maximum number of connections.
 * @param timeout
 *            the maximum time in seconds to wait for a free connection.
 */
public MiniConnectionPoolManager(ConnectionPoolDataSource dataSource, int maxConnections, int timeout) {
	this.dataSource = dataSource;
	this.maxConnections = maxConnections;
	this.timeoutMs = timeout * 1000L;
	try {
		logWriter = dataSource.getLogWriter();
	} catch (SQLException e) {
	}
	if (maxConnections < 1) {
		throw new IllegalArgumentException("Invalid maxConnections value.");
	}
	semaphore = new Semaphore(maxConnections, true);
	recycledConnections = new LinkedList<PooledConnection>();
	poolConnectionEventListener = new PoolConnectionEventListener();
}
 
Example #6
Source File: ConnectionPoolDataSourceConnector.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public  Connection openConnection
    (String databaseName, String user, String password, Properties connectionProperties)
     throws SQLException
{
    JDBCDataSource.setBeanProperty(ds, "databaseName", databaseName);
    try {
        return ds.getPooledConnection(user, password).getConnection();
    } catch (SQLException e) {
        // If there is a database not found exception
        // then retry the connection request with
        // a new DataSource with the createDatabase property set.
        if (!"XJ004".equals(e.getSQLState()))
            throw e;
        HashMap hm = DataSourceConnector.makeCreateDBAttributes( config );
        if ( connectionProperties != null ) { hm.putAll( connectionProperties ); }
        ConnectionPoolDataSource tmpDs = singleUseDS( hm );
        JDBCDataSource.setBeanProperty(tmpDs, "databaseName", databaseName);
        return tmpDs.getPooledConnection(user, password).getConnection(); 
   }
}
 
Example #7
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * check whether commit without statement will flow by checking its transaction id
 * on client. This test is run only for client where commits without an
 * active transactions will not flow to the server.
 * DERBY-4653
 * 
 * @throws SQLException
 **/
public void testConnectionFlowCommit()
        throws SQLException {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();

    PooledConnection pc = ds.getPooledConnection();
    Connection conn = pc.getConnection();

    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for XADataSource
    XADataSource xs = J2EEDataSource.getXADataSource();
    XAConnection xc = xs.getXAConnection();
    conn = xc.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for DataSource
    DataSource jds = JDBCDataSource.getDataSource();
    conn = jds.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();       
}
 
Example #8
Source File: PoolXADSCreateShutdownDBTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertConSetOK(Object ds, String expectedSQLState, 
    String dbName, String connAttrValue, String setter, String setValue) 
throws SQLException {
    JDBCDataSource.setBeanProperty(ds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(ds, setter, setValue);
    JDBCDataSource.setBeanProperty(
        ds, "ConnectionAttributes", connAttrValue);
    // check that the db exists; execute an unnecessary, but harmless, stmt
    try {
        if (ds instanceof javax.sql.ConnectionPoolDataSource)
            ((ConnectionPoolDataSource)ds).getPooledConnection();
        else
            ((XADataSource)ds).getXAConnection();
        fail("expected an sqlexception " + expectedSQLState);
    } catch (SQLException se) {
        assertSQLState(expectedSQLState, se);
    }
    JDBCDataSource.clearStringBeanProperty(ds, setter);
    JDBCDataSource.clearStringBeanProperty(ds, "ConnectionAttributes");
}
 
Example #9
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test that a PooledConnection can be reused and closed
 * (separately) during the close event raised by the
 * closing of its logical connection.
 * DERBY-2142.
 * @throws SQLException 
 *
 */
public void testPooledReuseOnClose() throws SQLException
{
	// PooledConnection from a ConnectionPoolDataSource
	ConnectionPoolDataSource cpds =
		J2EEDataSource.getConnectionPoolDataSource();
	subtestPooledReuseOnClose(cpds.getPooledConnection());
    subtestPooledCloseOnClose(cpds.getPooledConnection());
    // DERBY-3401 - removing a callback during a close causes problems.
    subtestPooledRemoveListenerOnClose(cpds.getPooledConnection());
    subtestPooledAddListenerOnClose(cpds.getPooledConnection());

	// PooledConnection from an XDataSource
	XADataSource xads = J2EEDataSource.getXADataSource();
	subtestPooledReuseOnClose(xads.getXAConnection());
    subtestPooledCloseOnClose(xads.getXAConnection());
    // DERBY-3401 - removing a callback during a close causes problems.
    subtestPooledRemoveListenerOnClose(xads.getXAConnection());
    subtestPooledAddListenerOnClose(xads.getXAConnection());
}
 
Example #10
Source File: NSSecurityMechanismTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private javax.sql.ConnectionPoolDataSource getCPDS(
    String user, String password)
{
    HashMap attrs = new HashMap();
    if (user != null)
        attrs.put("user", user);
    if (password != null)
        attrs.put("password", password);
    attrs = addRequiredAttributes(attrs);
    ConnectionPoolDataSource cpds = 
        J2EEDataSource.getConnectionPoolDataSource();
    for (Iterator i = attrs.keySet().iterator(); i.hasNext(); )
    {
        String property = (String) i.next();
        Object value = attrs.get(property);
        JDBCDataSource.setBeanProperty(cpds, property, value);
    }
    return cpds;
}
 
Example #11
Source File: DataSourcePropertiesTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #12
Source File: StatementPoolingTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the statement cache is able to throw out prepared statements
 * when it reaches maximum capacity.
 *
 * @throws SQLException if something goes wrong...
 */
public void testCacheOverflow()
        throws SQLException {
    final int stmtCount = 150;
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "maxStatements", new Integer(11));
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    PooledConnection pc = cpDs.getPooledConnection();
    Connection con = pc.getConnection();
    for (int i=0; i < stmtCount; i++) {
        // Yes, the "values + i" is intended here.
        PreparedStatement pStmt = con.prepareStatement("values " + i);
        ResultSet rs = pStmt.executeQuery();
        JDBC.assertSingleValueResultSet(rs, Integer.toString(i));
        pStmt.close();
    }
    con.close();
    pc.close();
}
 
Example #13
Source File: PoolDSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertSystemShutdownOK(
    String dbName, String user, String password)
throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.clearStringBeanProperty(pds, "databaseName");
    JDBCDataSource.setBeanProperty(pds, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(pds, "user", user);
    JDBCDataSource.setBeanProperty(pds, "password", password);
    try {
        pds.getPooledConnection();
        fail("expected system shutdown resulting in XJ015 error");
    } catch (SQLException e) {
        // expect XJ015, system shutdown, on successful shutdown
        assertSQLState("XJ015", e);
    }
}
 
Example #14
Source File: PoolDSAuthenticationTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void assertShutdownWOUPOK(
    String dbName, String user, String password)
throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(pds, "user", user);
    JDBCDataSource.setBeanProperty(pds, "password", password);
    JDBCDataSource.setBeanProperty(pds, "shutdownDatabase","shutdown");
    try {
        pds.getPooledConnection();
        fail ("expected a failed shutdown connection");
    } catch (SQLException e) {
        // expect 08006 on successful shutdown
        assertSQLState("08006", e);
    }
}
 
Example #15
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * check whether commit without statement will flow by checking its transaction id
 * on client. This test is run only for client where commits without an
 * active transactions will not flow to the server.
 * DERBY-4653
 * 
 * @throws SQLException
 **/
public void testConnectionFlowCommit()
        throws SQLException {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();

    PooledConnection pc = ds.getPooledConnection();
    Connection conn = pc.getConnection();

    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for XADataSource
    XADataSource xs = J2EEDataSource.getXADataSource();
    XAConnection xc = xs.getXAConnection();
    conn = xc.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for DataSource
    DataSource jds = JDBCDataSource.getDataSource();
    conn = jds.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();       
}
 
Example #16
Source File: PoolDSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertSystemShutdownFail(
        String expectedError, String dbName, String user, String password)
throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.clearStringBeanProperty(pds, "databaseName");
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(pds, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(pds, "user", user);
    JDBCDataSource.setBeanProperty(pds, "password", password);
    try {
        pds.getPooledConnection();
        fail("expected shutdown to fail");
    } catch (SQLException e) {
        assertSQLState(expectedError, e);
    }
}
 
Example #17
Source File: PoolDSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void assertConnectionFail(String dbName) throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    // Reset to no user/password though client requires
    // 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);
    }
}
 
Example #18
Source File: PoolDSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertShutdownWOUPOK(
    String dbName, String user, String password)
throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(pds, "user", user);
    JDBCDataSource.setBeanProperty(pds, "password", password);
    JDBCDataSource.setBeanProperty(pds, "shutdownDatabase","shutdown");
    try {
        pds.getPooledConnection();
        fail ("expected a failed shutdown connection");
    } catch (SQLException e) {
        // expect 08006 on successful shutdown
        assertSQLState("08006", e);
    }
}
 
Example #19
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Regression test for a NullPointerException when trying to use the LOB
 * stored procedures after closing and then getting a new logical
 * connection. The problem was that the LOB stored procedure objects on the
 * server side were closed and not reprepared.
 * See Jira issue DERBY-3799.
 */
public void testDerby3799() throws SQLException {
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    PooledConnection pc = cpDs.getPooledConnection();
    // Get first logical connection.
    Connection con1 = pc.getConnection();
    Statement stmt = con1.createStatement();
    ResultSet rs = stmt.executeQuery("select dClob from derby3799");
    assertTrue(rs.next());
    rs.getString(1);
    rs.close();
    con1.close();
    // Get second logical connection.
    Connection con2 = pc.getConnection();
    stmt = con2.createStatement();
    rs = stmt.executeQuery("select dClob from derby3799");
    assertTrue(rs.next());
    rs.getString(1); // NPE happened here.
    con2.close();
}
 
Example #20
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Tests for DERBY-1144
 * 
 * This test tests that holdability, autocomit, and transactionIsolation 
 * are reset on getConnection for PooledConnections obtaind from 
 * connectionPoolDataSources 
 * 
 * DERBY-1134 has been filed for more comprehensive testing of client 
 * connection state. 
 * 
 * @throws SQLException
 */
public void timeoutTestDerby1144PooledDS() throws SQLException {

    PooledConnection pc1 = null;

    // Test holdability   
    ConnectionPoolDataSource ds = 
        J2EEDataSource.getConnectionPoolDataSource();
    pc1 = ds.getPooledConnection();
    assertPooledConnHoldability("PooledConnection", pc1);
    pc1.close();
    
    // Test autocommit
    pc1 = ds.getPooledConnection();
    assertPooledConnAutoCommit("PooledConnection", pc1);
    pc1.close();
    
    // Test pooled connection isolation
    pc1 = ds.getPooledConnection();
    assertPooledConnIso("PooledConnection" , pc1);   
    pc1.close();
}
 
Example #21
Source File: PoolDSAuthenticationTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void assertSystemShutdownFail(
        String expectedError, String dbName, String user, String password)
throws SQLException {
    ConnectionPoolDataSource pds = J2EEDataSource.getConnectionPoolDataSource();
    JDBCDataSource.clearStringBeanProperty(pds, "databaseName");
    JDBCDataSource.setBeanProperty(pds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(pds, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(pds, "user", user);
    JDBCDataSource.setBeanProperty(pds, "password", password);
    try {
        pds.getPooledConnection();
        fail("expected shutdown to fail");
    } catch (SQLException e) {
        assertSQLState(expectedError, e);
    }
}
 
Example #22
Source File: StatementPoolingTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test sequence for testing if the connection holdability is reset.
 *
 * @param closeConnection determines if the logical connection is
 *      explicitly closed before a new one is obtained
 * @throws SQLException if something goes wrong...
 */
private void doTestHoldabilityIsReset(final boolean closeConnection)
        throws SQLException {
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "maxStatements", new Integer(7));
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    PooledConnection pc = cpDs.getPooledConnection();
    // Keep track of our own connection, the framework currently creates
    // a new pooled connection and then obtains a connection from that.
    // Statement pooling only works within a single pooled connection.
    Connection con = pc.getConnection();
    assertEquals("Unexpected default holdability",
            ResultSet.HOLD_CURSORS_OVER_COMMIT, con.getHoldability());
    con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
    assertEquals("Holdability not updated",
            ResultSet.CLOSE_CURSORS_AT_COMMIT, con.getHoldability());
    if (closeConnection) {
        con.close();
    }
    con = pc.getConnection();
    assertEquals("Holdability not reset",
            ResultSet.HOLD_CURSORS_OVER_COMMIT, con.getHoldability());
    pc.close();
}
 
Example #23
Source File: PoolXADSCreateShutdownDBTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void assertConflictedSettersOK(Object ds, String dbName) 
throws SQLException {
    JDBCDataSource.setBeanProperty(ds, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(ds, "CreateDatabase", "create");
    JDBCDataSource.setBeanProperty(ds, "shutdownDatabase", "shutdown");
    try {
        if (ds instanceof javax.sql.ConnectionPoolDataSource)
            ((ConnectionPoolDataSource)ds).getPooledConnection();
        else
            ((XADataSource)ds).getXAConnection();
        fail("expected an sqlexception " + DBNotFoundState);
    } catch (SQLException se) {
        assertSQLState(DBNotFoundState, se);
    }
}
 
Example #24
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static void dsConnectionRequest(String expectedValue, 
    ConnectionPoolDataSource ds, String user, String ConnAttr)
{
    try {
        ds.getPooledConnection(user, ConnAttr);
        if (!expectedValue.equals("OK"))
            fail (" expected connection to fail, but was OK");
    } catch (SQLException sqle) {
        assertSQLState(expectedValue, sqle);
    }
}
 
Example #25
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that deleting the current / default schema doesn't cause the next
 * logical connection to fail.
 * <p>
 * Relevant Jira issue: DERBY-3690.
 *
 * @throws SQLException if something goes wrong
 */
public void testSchemaIsResetWhenDeleted()
        throws SQLException {
    final String userSchema = "AUSER";
    ConnectionPoolDataSource cpDs =
            J2EEDataSource.getConnectionPoolDataSource();
    J2EEDataSource.setBeanProperty(cpDs, "createDatabase", "create");
    PooledConnection pc = cpDs.getPooledConnection(userSchema, "secret");
    // Get first connection, create a table, then drop schema.
    Connection con = pc.getConnection();
    JDBC.assertCurrentSchema(con, userSchema);
    Statement stmt = con.createStatement();
    stmt.executeUpdate("create table schematest (id int)");
    stmt.executeUpdate("drop table schematest");
    stmt.executeUpdate("drop schema " + userSchema + " restrict");
    stmt.close();
    con.close();
    // Get second connection.
    con = pc.getConnection();
    JDBC.assertCurrentSchema(con, userSchema);
    stmt = con.createStatement();
    stmt.executeUpdate("create table schematest (id int)");
    stmt.executeUpdate("drop table schematest");
    stmt.close();
    JDBC.assertCurrentSchema(con, userSchema);
    con.close();
    pc.close();
}
 
Example #26
Source File: J2EEDataSource.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new DataSource object setup from the passed in TestConfiguration.
 * The getPooledConnection() method is configured
 * to use the user name and password from the configuration.
 */
static ConnectionPoolDataSource getConnectionPoolDataSource(
        TestConfiguration config,
        HashMap beanProperties)
{
    if (beanProperties == null)
         beanProperties = JDBCDataSource.getDataSourceProperties(config);
    
    String dataSourceClass = config.getJDBCClient().getConnectionPoolDataSourceClassName();
    
    return (ConnectionPoolDataSource) JDBCDataSource.getDataSourceObject(
            dataSourceClass, beanProperties);
}
 
Example #27
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Check that traceFile connection attribute functions correctly.
 * tracefile was tested in checkDriver, but not for DataSources.
 * tracefile= was used in datasourcepermissions_net, but that's 
 * incorrect syntax. Note that we're not checking the contents of
 * the tracefile.
 *
 * Note also that this test cannot run against a remote server.
 *  
 * @throws SQLException
 */
public void testClientTraceFileDSConnectionAttribute() throws SQLException
{
    if (usingEmbedded())
        return;

    String traceFile;

    // with ConnectionPoolDataSource
    ConnectionPoolDataSource cpds = J2EEDataSource.getConnectionPoolDataSource();

    traceFile = "trace3.out";
    JDBCDataSource.setBeanProperty(cpds, "connectionAttributes",
    		"traceFile="+traceFile);
    // DERBY-2468 - trace3.out does not get created
    ((ClientConnectionPoolDataSource) cpds).getConnection();
    JDBCDataSource.clearStringBeanProperty(cpds, "connectionAttributes");

    traceFile = "trace4.out";
    JDBCDataSource.setBeanProperty(cpds, "traceFile", traceFile);
    ((ClientConnectionPoolDataSource) cpds).getConnection();
    cpds = null;

    // now with XADataSource
    XADataSource xads = J2EEDataSource.getXADataSource();

    traceFile = "trace5.out";
    JDBCDataSource.setBeanProperty(xads, "connectionAttributes",
    		"traceFile="+traceFile);
    ((ClientXADataSource) xads).getConnection();
    // DERBY-2468 - trace5.out does not get created
    JDBCDataSource.clearStringBeanProperty(xads, "connectionAttributes");

    traceFile = "trace6.out";
    JDBCDataSource.setBeanProperty(xads, "traceFile", traceFile);
    ((ClientXADataSource) xads).getConnection();

    assertTraceFilesExist();
}
 
Example #28
Source File: PerUserPoolDataSource.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
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<>(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);
    }
}
 
Example #29
Source File: xaHelper.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public void CPDataSourceStatement(ij parser, Token dbname, Token protocol)
	 throws SQLException
{
	try {
		currentCPDataSource = (ConnectionPoolDataSource) (Class.forName("com.splicemachine.db.jdbc.EmbeddedConnectionPoolDataSource").newInstance());
	} catch (Exception e) {
		throw new SQLException(e.toString());
	}
	databaseName = parser.stringValue(dbname.image);
	xaHelper.setDataSourceProperty(currentCPDataSource, "databaseName", databaseName);
}
 
Example #30
Source File: PoolXADSCreateShutdownDBTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void assertDSConnectionFailed(
    String expectedSQLState, Object ds) throws SQLException {
    try {
        if (ds instanceof javax.sql.ConnectionPoolDataSource)
            ((ConnectionPoolDataSource)ds).getPooledConnection();
        else
            ((XADataSource)ds).getXAConnection();
        fail("expected an sqlexception " + expectedSQLState);
    } catch (SQLException sqle) {
        assertSQLState(expectedSQLState, sqle);
    }
}