use of javax.sql.ConnectionPoolDataSource in project derby by apache.
the class cdsXid method testDerby2026LoginTimeout.
/**
* DERBY-2026 - Make sure login timeout does not impact
* queries.
*/
public void testDerby2026LoginTimeout() throws SQLException {
DataSource jds = null;
try {
jds = JDBCDataSource.getDataSource();
jds.setLoginTimeout(10);
Connection conn = jds.getConnection();
CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
cs.execute();
// rollback to make sure our connection is ok.
conn.rollback();
} finally {
if (jds != null)
jds.setLoginTimeout(0);
}
ConnectionPoolDataSource cpds = null;
try {
cpds = J2EEDataSource.getConnectionPoolDataSource();
cpds.setLoginTimeout(10);
PooledConnection pc = cpds.getPooledConnection();
Connection conn = pc.getConnection();
CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
cs.execute();
// rollback to make sure our connection is ok.
conn.rollback();
// Close the logical connection and get a new one.
// This will invoke reset which also needs its timeout reset
conn.close();
conn = pc.getConnection();
cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
cs.execute();
// rollback to make sure our connection is ok.
conn.rollback();
} finally {
if (cpds != null)
cpds.setLoginTimeout(0);
}
XADataSource xads = null;
try {
xads = J2EEDataSource.getXADataSource();
xads.setLoginTimeout(10);
XAConnection xac = xads.getXAConnection();
Connection conn = xac.getConnection();
CallableStatement cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
cs.execute();
// rollback to make sure our connection is ok.
conn.rollback();
// Close the logical connection and get a new one.
// This will invoke reset which also needs its timeout reset
conn.close();
conn = xac.getConnection();
cs = conn.prepareCall("CALL TESTROUTINE.SLEEP(20000)");
cs.execute();
// rollback to make sure our connection is ok.
conn.rollback();
} finally {
if (xads != null)
xads.setLoginTimeout(0);
}
}
use of javax.sql.ConnectionPoolDataSource in project derby by apache.
the class InternationalConnectTest method testCPDSConnect.
/**
* Test pooled connetion for chinese database name, user and password.
* @throws SQLException
*/
public void testCPDSConnect() throws SQLException {
// Test chinese database name.
ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
J2EEDataSource.setBeanProperty(ds, "databaseName", "\u4e10");
J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
PooledConnection poolConn = ds.getPooledConnection();
Connection conn = poolConn.getConnection();
conn.close();
poolConn.close();
// Chinese user
J2EEDataSource.setBeanProperty(ds, "user", "\u4e10");
poolConn = ds.getPooledConnection();
conn = poolConn.getConnection();
conn.close();
poolConn.close();
// Chinese password
J2EEDataSource.setBeanProperty(ds, "password", "\u4e10");
poolConn = ds.getPooledConnection();
conn = poolConn.getConnection();
conn.close();
poolConn.close();
/* Add the created database for cleanup by tearDown() */
databasesForCleanup.add("\u4e10");
}
use of javax.sql.ConnectionPoolDataSource in project derby by apache.
the class cdsXid method testConnectionErrorEvent.
/* comment out. leaving in, just in case it's ever relevant.
* when uncommented, this will run when network server tests are
* started, and then reflect the results of the embedded checks.
// perform security analysis of the public api for the embedded engine
public void testDataSourceAPI() throws SQLException, ClassNotFoundException
{
SecurityCheck.report();
}
*/
/**
* Test case for DERBY-3172
* When the Derby engine is shutdown or Network Server is brought down, any
* api on JDBC Connection object should generate a Connection error event.
*/
public void testConnectionErrorEvent() throws SQLException, Exception {
AssertEventCatcher aes12 = new AssertEventCatcher(12);
ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
PooledConnection pc = ds.getPooledConnection();
// Add a connection event listener to ConnectionPoolDataSource
pc.addConnectionEventListener(aes12);
Connection conn = pc.getConnection();
dropTable(conn, "TAB1");
// No event should have been generated at this point
assertFalse(aes12.didConnectionClosedEventHappen());
assertFalse(aes12.didConnectionErrorEventHappen());
aes12.resetState();
// mode we are running in.
if (usingEmbedded()) {
getTestConfiguration().shutdownDatabase();
} else {
getTestConfiguration().stopNetworkServer();
}
// before shutdown and they all should generate connection error event.
try {
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)");
fail("SQLException should be thrown!");
} catch (SQLException e) {
// meaning No current connection
if (usingEmbedded())
assertSQLState("08003", e);
else
assertSQLState("08006", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)", 1);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
int[] columnIndexes = { 1 };
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)", columnIndexes);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
String[] columnNames = { "col1" };
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)", columnNames);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareStatement("CREATE TABLE TAB1(COL1 INT NOT NULL)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.createStatement();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareCall("CREATE TABLE TAB1(COL1 INT NOT NULL)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareCall("CREATE TABLE TAB1(COL1 INT NOT NULL)");
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.prepareCall("CREATE TABLE TAB1(COL1 INT NOT NULL)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.nativeSQL("CREATE TABLE TAB1(COL1 INT NOT NULL)");
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getAutoCommit();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setAutoCommit(false);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getHoldability();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setHoldability(1);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.commit();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.rollback();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setSavepoint();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setSavepoint("savept1");
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.rollback((Savepoint) null);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.releaseSavepoint((Savepoint) null);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getTransactionIsolation();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getWarnings();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.clearWarnings();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getMetaData();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.isReadOnly();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setReadOnly(true);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setCatalog(null);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getCatalog();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.getTypeMap();
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
try {
conn.setTypeMap(null);
fail("SQLException of 08003 should be thrown!");
} catch (SQLException e) {
assertSQLState("08003", e);
}
assertFalse(aes12.didConnectionClosedEventHappen());
assertTrue(aes12.didConnectionErrorEventHappen());
aes12.resetState();
if (usingEmbedded()) {
Class<?> clazz = Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
clazz.getConstructor().newInstance();
} else {
getTestConfiguration().startNetworkServer();
}
// Get a new connection to the database
conn = getConnection();
conn.close();
}
use of javax.sql.ConnectionPoolDataSource in project derby by apache.
the class cdsXid method testConnectionFlowCommitRollback.
/**
* Performs a test sequence accessing the server, then parses the client
* connection trace file to obtain the number of commit or rollback
* commands flowed from the client to the server.
*
* @param ds data source used to obtain a connection to the database
* (must be using the test framework defaults)
* @param invokeExtra if {@code true} extra invocations of either commit or
* rollback are performed (depending on value of {@code isCommit})
* @param isCommit if {@code true}, commits are invoked, otherwise
* rollbacks are invoked
* @return The number of wire flows detected (depending on value of
* {@code isCommit}).
* @throws IOException if reading/parsing the trace file fails
* @throws SQLException if something goes wrong
*/
private int testConnectionFlowCommitRollback(Object ds, boolean invokeExtra, boolean isCommit) throws IOException, SQLException {
final int extraInvokations = invokeExtra ? 25 : 0;
final int rowCount = 10;
final boolean isXA = ds instanceof XADataSource;
final boolean isCP = ds instanceof ConnectionPoolDataSource;
// Generate trace file name and define trace behavior.
String dsType = (isXA ? "xa_" : (isCP ? "cp_" : ""));
String tbl = "ds_" + dsType + (invokeExtra ? "base_" : "extra_") + (isCommit ? "commit" : "rollback");
File traceFile = SupportFilesSetup.getReadWrite(tbl + ".trace");
J2EEDataSource.setBeanProperty(ds, "traceFile", PrivilegedFileOpsForTests.getAbsolutePath(traceFile));
J2EEDataSource.setBeanProperty(ds, "traceFileAppend", Boolean.FALSE);
J2EEDataSource.setBeanProperty(ds, "traceLevel", BasicClientDataSource40.TRACE_ALL);
// Obtain connection.
PooledConnection physicalCon = null;
Connection con;
if (isXA) {
physicalCon = ((XADataSource) ds).getXAConnection();
con = physicalCon.getConnection();
} else if (isCP) {
physicalCon = ((ClientConnectionPoolDataSourceInterface) ds).getPooledConnection();
con = physicalCon.getConnection();
} else {
con = ((DataSource) ds).getConnection();
}
con.setAutoCommit(false);
// Run test sequence.
// step 0: create table
Statement stmt = con.createStatement();
stmt.executeUpdate("create table " + tbl + " (id int)");
// Unconditional commit to persist table
con.commit();
endTranscation(con, isCommit, extraInvokations);
// step 1: insert data
PreparedStatement ps = con.prepareStatement("insert into " + tbl + " values (?)");
for (int i = 0; i < rowCount; i++) {
ps.setInt(1, i);
ps.executeUpdate();
endTranscation(con, isCommit, extraInvokations);
}
ps.close();
// Unconditional commit, should catch "missed" rollbacks above when we
// do a select with another connection at the end.
con.commit();
// step 2: select data
ResultSet rs = stmt.executeQuery("select count(*) from " + tbl);
rs.next();
rs.getInt(1);
rs.close();
endTranscation(con, isCommit, extraInvokations);
// step 3: values clause
rs = stmt.executeQuery("values 7");
assertTrue(rs.next());
assertEquals(7, rs.getInt(1));
rs.close();
stmt.close();
endTranscation(con, isCommit, extraInvokations);
con.close();
if (physicalCon != null) {
physicalCon.close();
}
// step 4: table content validation
stmt = createStatement();
rs = stmt.executeQuery("select count(*) from " + tbl);
rs.next();
assertEquals("Potential COMMIT/ROLLBACK protocol error", isCommit ? rowCount : 0, rs.getInt(1));
// Parse the trace file for commits or rollbacks.
String token = "SEND BUFFER: " + (isXA ? "SYNCCTL" : (isCommit ? "RDBCMM" : "RDBRLLBCK"));
int tokenCount = 0;
BufferedReader r = new BufferedReader(PrivilegedFileOpsForTests.getFileReader(traceFile));
String line;
while ((line = r.readLine()) != null) {
if (line.startsWith("[derby]") && line.indexOf(token) != -1) {
println((isCommit ? "COMMIT: " : "ROLLBACK: ") + line);
tokenCount++;
}
}
r.close();
assertTrue("Parsing failed, no COMMITS/ROLLBACKS detected", tokenCount > 0);
println(ds.getClass().getName() + ", invokeExtra=" + invokeExtra + ", isCommit=" + isCommit + ", tokenCount=" + tokenCount);
return tokenCount;
}
use of javax.sql.ConnectionPoolDataSource in project derby by apache.
the class cdsXid method testPooledReuseOnClose.
/**
* 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());
}
Aggregations