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();
}
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);
}
}
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();
}
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;
}
}
});
}
}
};
}
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());
}
Aggregations