Search in sources :

Example 1 with StatementEventListener

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

the class StatementEventsTest method testRemoveListenerFromListener.

/**
 * Test that removing a listener from a listener works. (DERBY-3401)
 */
public void testRemoveListenerFromListener() throws SQLException {
    // First element is number of times the close listeners below have
    // been triggered, second element is number of times the error
    // listeners have been triggered.
    final int[] counters = new int[2];
    // Add some listeners that remove themselves
    for (int i = 0; i < 5; i++) {
        StatementEventListener close = new StatementEventListener() {

            public void statementClosed(StatementEvent event) {
                pooledConnection.removeStatementEventListener(this);
                counters[0]++;
            }

            public void statementErrorOccurred(StatementEvent event) {
            }
        };
        pooledConnection.addStatementEventListener(close);
        StatementEventListener error = new StatementEventListener() {

            public void statementClosed(StatementEvent event) {
            }

            public void statementErrorOccurred(StatementEvent event) {
                pooledConnection.removeStatementEventListener(this);
                counters[1]++;
            }
        };
        pooledConnection.addStatementEventListener(error);
    }
    // in the second iteration.
    for (int i = 0; i < 2; i++) {
        prepare("VALUES (1)").close();
        assertEquals("unexpected number of close events", 5, counters[0]);
        assertEquals("unexpected number of error events", 0, counters[1]);
    }
    // reset counters
    Arrays.fill(counters, 0);
    // the first iteration since the listeners remove themselves.
    for (int i = 0; i < 2; i++) {
        PreparedStatement ps = prepare("VALUES (1)");
        connection.close();
        try {
            ps.execute();
            fail("Execute on closed connection should fail");
        } catch (SQLNonTransientConnectionException e) {
            assertSQLState("08003", e);
        }
        assertEquals("unexpected number of close events", 0, counters[0]);
        assertEquals("unexpected number of error events", 5, counters[1]);
        connection = pooledConnection.getConnection();
    }
    // The listeners that are automatically added for all test cases have
    // been active all the time.
    assertEquals("Incorrect error count", 2, errorCount);
    // expected difference.
    if (usingEmbedded()) {
        assertEquals("Incorrect close count", 2, closedCount);
    } else if (usingDerbyNetClient()) {
        assertEquals("Incorrect close count", 4, closedCount);
    } else {
        fail("unknown framework");
    }
}
Also used : SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) StatementEventListener(javax.sql.StatementEventListener) StatementEvent(javax.sql.StatementEvent) PreparedStatement(java.sql.PreparedStatement)

Example 2 with StatementEventListener

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

the class StatementEventsTest method setUp.

// TEST SETUP
/**
 * Set up the connection to the database and register a statement event
 * listener.
 *
 * @exception SQLException if a database error occurs
 */
public void setUp() throws SQLException {
    if (xa) {
        XADataSource ds = J2EEDataSource.getXADataSource();
        J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
        pooledConnection = ds.getXAConnection();
    } else {
        ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
        J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
        pooledConnection = ds.getPooledConnection();
    }
    StatementEventListener listener = new StatementEventListener() {

        public void statementClosed(StatementEvent event) {
            closedStatement = event.getStatement();
            closedCount++;
        }

        public void statementErrorOccurred(StatementEvent event) {
            errorStatement = event.getStatement();
            errorCount++;
        }
    };
    pooledConnection.addStatementEventListener(listener);
    connection = pooledConnection.getConnection();
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) XADataSource(javax.sql.XADataSource) StatementEventListener(javax.sql.StatementEventListener) StatementEvent(javax.sql.StatementEvent)

Example 3 with StatementEventListener

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

the class StatementEventsTest method testAddListenerFromListener.

/**
 * Test that adding a listener from a listener works. (DERBY-3401)
 */
public void testAddListenerFromListener() throws SQLException {
    // First element is number of times the close listeners below have
    // been triggered, second element is number of times the error
    // listeners have been triggered. Third element is the number of
    // times listeners added by close listeners have been triggered,
    // fourth element is the number of times listeners added by error
    // listeners have been triggered.
    final int[] counters = new int[4];
    // Add some listeners that add another listener
    for (int i = 0; i < 5; i++) {
        StatementEventListener close = new StatementEventListener() {

            public void statementClosed(StatementEvent event) {
                counters[0]++;
                pooledConnection.addStatementEventListener(new StatementEventListener() {

                    public void statementClosed(StatementEvent e) {
                        counters[2]++;
                    }

                    public void statementErrorOccurred(StatementEvent e) {
                        counters[2]++;
                    }
                });
            }

            public void statementErrorOccurred(StatementEvent event) {
            }
        };
        pooledConnection.addStatementEventListener(close);
        StatementEventListener error = new StatementEventListener() {

            public void statementClosed(StatementEvent event) {
            }

            public void statementErrorOccurred(StatementEvent event) {
                counters[1]++;
                pooledConnection.addStatementEventListener(new StatementEventListener() {

                    public void statementClosed(StatementEvent e) {
                        counters[3]++;
                    }

                    public void statementErrorOccurred(StatementEvent e) {
                        counters[3]++;
                    }
                });
            }
        };
        pooledConnection.addStatementEventListener(error);
    }
    // Generate close event
    prepare("VALUES (1)").close();
    assertEquals("unexpected number of close events", 5, counters[0]);
    assertEquals("unexpected number of error events", 0, counters[1]);
    assertEquals("unexpected number of added close listeners triggered", 0, counters[2]);
    assertEquals("unexpected number of added error listeners triggered", 0, counters[3]);
    // Generate another close event
    prepare("VALUES (1)").close();
    assertEquals("unexpected number of close events", 10, counters[0]);
    assertEquals("unexpected number of error events", 0, counters[1]);
    assertEquals("unexpected number of added close listeners triggered", 5, counters[2]);
    assertEquals("unexpected number of added error listeners triggered", 0, counters[3]);
    // Generate a statement that doesn't work
    PreparedStatement ps = prepare("VALUES (1)");
    connection.close();
    // reset counters
    Arrays.fill(counters, 0);
    // Generate an error event
    try {
        ps.execute();
        fail("Execute on closed connection should fail");
    } catch (SQLNonTransientConnectionException e) {
        assertSQLState("08003", e);
    }
    assertEquals("unexpected number of close events", 0, counters[0]);
    assertEquals("unexpected number of error events", 5, counters[1]);
    // difference between embedded and client because client gets
    // statement-closed event when the connection is closed, whereas
    // embedded doesn't
    assertEquals("unexpected number of added close listeners triggered", usingEmbedded() ? 10 : 15, counters[2]);
    assertEquals("unexpected number of added error listeners triggered", 0, counters[3]);
    // reset counters
    Arrays.fill(counters, 0);
    // Generate another error event, now with more listeners active
    try {
        ps.execute();
        fail("Execute on closed connection should fail");
    } catch (SQLNonTransientConnectionException e) {
        assertSQLState("08003", e);
    }
    assertEquals("unexpected number of close events", 0, counters[0]);
    assertEquals("unexpected number of error events", 5, counters[1]);
    // difference between embedded and client because client gets
    // statement-closed event when the connection is closed, whereas
    // embedded doesn't
    assertEquals("unexpected number of added close listeners triggered", usingEmbedded() ? 10 : 15, counters[2]);
    assertEquals("unexpected number of added error listeners triggered", 5, counters[3]);
    // The listeners that are automatically added for all test cases have
    // been active all the time.
    assertEquals("Incorrect error count", 2, errorCount);
    // expected difference.
    if (usingEmbedded()) {
        assertEquals("Incorrect close count", 2, closedCount);
    } else if (usingDerbyNetClient()) {
        assertEquals("Incorrect close count", 3, closedCount);
    } else {
        fail("unknown framework");
    }
}
Also used : SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) StatementEventListener(javax.sql.StatementEventListener) StatementEvent(javax.sql.StatementEvent) PreparedStatement(java.sql.PreparedStatement)

Example 4 with StatementEventListener

use of javax.sql.StatementEventListener in project spanner-jdbc by olavloite.

the class CloudSpannerPooledConnectionTest method testEventListeners.

@Test
public void testEventListeners() throws SQLException {
    CloudSpannerPooledConnection subject = createConnection();
    ConnectionEventListener listener = Mockito.mock(ConnectionEventListener.class);
    subject.addConnectionEventListener(listener);
    subject.removeConnectionEventListener(listener);
    StatementEventListener statementListener = Mockito.mock(StatementEventListener.class);
    subject.addStatementEventListener(statementListener);
    subject.removeStatementEventListener(statementListener);
    subject.close();
}
Also used : StatementEventListener(javax.sql.StatementEventListener) ConnectionEventListener(javax.sql.ConnectionEventListener) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 5 with StatementEventListener

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

the class JDBC4FromJDBC3DataSourceTest method checkJDBC4Interface.

/**
 * Make sure that the received PooledConnection, which we assume came
 * from a JDBC 3 data source, is nonetheless a JDBC 4 object.
 */
private void checkJDBC4Interface(PooledConnection pc) throws Exception {
    // Create dummy event listener.
    StatementEventListener listener = new StatementEventListener() {

        public void statementClosed(StatementEvent event) {
        }

        public void statementErrorOccurred(StatementEvent event) {
        }
    };
    /* Assert that metadata reports JDBC 4 for the connection, which
         * it should even though the connection was created from a JDBC 3
         * datasource.
         */
    Connection conn = pc.getConnection();
    assertEquals(4, conn.getMetaData().getJDBCMajorVersion());
    conn.close();
    conn = null;
    /* The way we check to see if we actually have JDBC 4 objects is
         * to call two methods that only exist in JDBC 4.  These should
         * succeed.  Before DERBY-2488 they would fail with an Abstract
         * MethodError.
         */
    pc.addStatementEventListener(listener);
    pc.removeStatementEventListener(listener);
    pc.close();
}
Also used : StatementEventListener(javax.sql.StatementEventListener) StatementEvent(javax.sql.StatementEvent) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection)

Aggregations

StatementEventListener (javax.sql.StatementEventListener)5 StatementEvent (javax.sql.StatementEvent)4 PreparedStatement (java.sql.PreparedStatement)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 Connection (java.sql.Connection)1 ConnectionEventListener (javax.sql.ConnectionEventListener)1 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)1 PooledConnection (javax.sql.PooledConnection)1 XADataSource (javax.sql.XADataSource)1 UnitTest (nl.topicus.jdbc.test.category.UnitTest)1 Test (org.junit.Test)1