Search in sources :

Example 16 with ConnectionEvent

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

the class cdsXid method subtestPooledReuseOnClose.

/**
 * Tests that a pooled connection can successfully be reused
 * (a new connection obtained from it) during the processing
 * of its close event by its listener.
 * Sections 11.2 and 12.5 of JDBC 4 specification indicate that the
 * connection can be returned to the pool when the
 * ConnectionEventListener.connectionClosed() is called.
 */
private void subtestPooledReuseOnClose(final PooledConnection pc) throws SQLException {
    final Connection[] newConn = new Connection[1];
    pc.addConnectionEventListener(new ConnectionEventListener() {

        /**
         * Mimic a pool handler that returns the PooledConnection
         * to the pool and then reallocates it to a new logical connection.
         */
        public void connectionClosed(ConnectionEvent event) {
            PooledConnection pce = (PooledConnection) event.getSource();
            assertSame(pc, pce);
            try {
                // open a new logical connection and pass
                // back to the fixture.
                newConn[0] = pce.getConnection();
            } catch (SQLException e) {
                // the api method. Wrap it in a RuntimeException.
                throw new RuntimeException(e);
            }
        }

        public void connectionErrorOccurred(ConnectionEvent event) {
        }
    });
    // Open a connection then close it to trigger the
    // fetching of a new connection in the callback.
    Connection c1 = pc.getConnection();
    c1.close();
    // Fetch the connection created in the close callback
    Connection c2 = newConn[0];
    assertNotNull(c2);
    // Ensure the connection is useable, this hit a NPE before DERBY-2142
    // was fixed (for embedded).
    c2.createStatement().close();
    pc.close();
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) ConnectionEvent(javax.sql.ConnectionEvent) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 17 with ConnectionEvent

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

the class cdsXid method subtestPooledCloseOnClose.

/**
 * Tests that a pooled connection can successfully be closed
 * during the processing of its close event by its listener.
 */
private void subtestPooledCloseOnClose(final PooledConnection pc) throws SQLException {
    pc.addConnectionEventListener(new ConnectionEventListener() {

        /**
         * Mimic a pool handler that closes the PooledConnection
         * (say it no longer needs it, pool size being reduced)
         */
        public void connectionClosed(ConnectionEvent event) {
            PooledConnection pce = (PooledConnection) event.getSource();
            assertSame(pc, pce);
            try {
                pce.close();
            } catch (SQLException e) {
                // the api method. Wrap it in a RuntimeException.
                throw new RuntimeException(e);
            }
        }

        public void connectionErrorOccurred(ConnectionEvent event) {
        }
    });
    // Open and close a connection to invoke the logic above
    // through the callback
    pc.getConnection().close();
    // should fail.
    try {
        pc.getConnection();
        fail("PooledConnection should be closed");
    } catch (SQLException sqle) {
        assertSQLState("08003", sqle);
    }
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) ConnectionEvent(javax.sql.ConnectionEvent) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 18 with ConnectionEvent

use of javax.sql.ConnectionEvent in project h2database by h2database.

the class TestDataSource method testXAConnection.

private void testXAConnection(boolean userInDataSource) throws Exception {
    deleteDb("dataSource");
    JdbcDataSource ds = new JdbcDataSource();
    String url = getURL("dataSource", true);
    String user = getUser();
    ds.setURL(url);
    if (userInDataSource) {
        ds.setUser(user);
        ds.setPassword(getPassword());
    }
    if (userInDataSource) {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
    } else {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
    }
    XAConnection xaConn;
    if (userInDataSource) {
        xaConn = ds.getXAConnection();
    } else {
        xaConn = ds.getXAConnection(user, getPassword());
    }
    int traceId = ((JdbcXAConnection) xaConn).getTraceId();
    assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
    xaConn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionClosed(ConnectionEvent event) {
        // nothing to do
        }

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
        // nothing to do
        }
    });
    XAResource res = xaConn.getXAResource();
    assertFalse(res.setTransactionTimeout(1));
    assertEquals(0, res.getTransactionTimeout());
    assertTrue(res.isSameRM(res));
    assertFalse(res.isSameRM(null));
    Connection conn = xaConn.getConnection();
    assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
    Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    Statement stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
    conn.close();
    xaConn.close();
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionEvent(javax.sql.ConnectionEvent) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 19 with ConnectionEvent

use of javax.sql.ConnectionEvent in project drools by kiegroup.

the class PoolingDataSourceFactory method getXAResourceRecoveryHelper.

static XAResourceRecoveryHelper getXAResourceRecoveryHelper(XADataSource xaDataSource, Properties properties) {
    return new XAResourceRecoveryHelper() {

        private final Object lock = new Object();

        private XAConnection connection;

        @Override
        public boolean initialise(String p) throws Exception {
            return true;
        }

        @Override
        public synchronized XAResource[] getXAResources() throws Exception {
            synchronized (lock) {
                initialiseConnection();
                try {
                    return new XAResource[] { connection.getXAResource() };
                } catch (SQLException ex) {
                    return new XAResource[0];
                }
            }
        }

        private void initialiseConnection() throws SQLException {
            // we don't have a hook
            if (connection == null) {
                final String user = properties.getProperty(PROP_USERNAME);
                final String password = properties.getProperty(PROP_PASSWORD);
                if (user != null && password != null) {
                    connection = xaDataSource.getXAConnection(user, password);
                } else {
                    connection = xaDataSource.getXAConnection();
                }
                connection.addConnectionEventListener(new ConnectionEventListener() {

                    @Override
                    public void connectionClosed(ConnectionEvent event) {
                        log.warn("The connection was closed: " + connection);
                        synchronized (lock) {
                            connection = null;
                        }
                    }

                    @Override
                    public void connectionErrorOccurred(ConnectionEvent event) {
                        log.warn("A connection error occurred: " + connection);
                        synchronized (lock) {
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                // Ignore
                                log.warn("Could not close failing connection: " + connection);
                            }
                            connection = null;
                        }
                    }
                });
            }
        }
    };
}
Also used : XAResource(javax.transaction.xa.XAResource) SQLException(java.sql.SQLException) ConnectionEvent(javax.sql.ConnectionEvent) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XAConnection(javax.sql.XAConnection) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 20 with ConnectionEvent

use of javax.sql.ConnectionEvent in project j2objc by google.

the class OldConnectionEventTest method testGetSQLException.

public void testGetSQLException() {
    Impl_PooledConnection ipc = new Impl_PooledConnection();
    ConnectionEvent ce = new ConnectionEvent(ipc);
    ConnectionEvent ce2 = new ConnectionEvent(ipc, null);
    assertNull(ce.getSQLException());
    assertEquals(ce2.getSQLException(), ce.getSQLException());
    SQLException e = new SQLException();
    ConnectionEvent ce3 = new ConnectionEvent(ipc, e);
    assertNotNull(ce3.getSQLException());
    assertNotSame(ce3.getSQLException(), ce2.getSQLException());
}
Also used : SQLException(java.sql.SQLException) ConnectionEvent(javax.sql.ConnectionEvent)

Aggregations

ConnectionEvent (javax.sql.ConnectionEvent)27 ConnectionEventListener (javax.sql.ConnectionEventListener)23 SQLException (java.sql.SQLException)10 PooledConnection (javax.sql.PooledConnection)4 XAConnection (javax.sql.XAConnection)4 Connection (java.sql.Connection)3 Iterator (java.util.Iterator)3 XAResource (javax.transaction.xa.XAResource)3 Filter (com.alibaba.druid.filter.Filter)2 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)1 PreparedStatement (java.sql.PreparedStatement)1 Savepoint (java.sql.Savepoint)1 Statement (java.sql.Statement)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Xid (javax.transaction.xa.Xid)1 PooledObject (org.apache.tomcat.dbcp.pool2.PooledObject)1 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)1