Search in sources :

Example 1 with DefaultStatementListener

use of org.firebirdsql.gds.ng.listeners.DefaultStatementListener in project jaybird by FirebirdSQL.

the class TestReconnectTransaction method testReconnectTransaction.

@Test
public void testReconnectTransaction() throws Exception {
    FbConnectionProperties connectionInfo = new FbConnectionProperties();
    connectionInfo.setServerName(FBTestProperties.DB_SERVER_URL);
    connectionInfo.setPortNumber(FBTestProperties.DB_SERVER_PORT);
    connectionInfo.setUser(DB_USER);
    connectionInfo.setPassword(DB_PASSWORD);
    connectionInfo.setDatabaseName(FBTestProperties.getDatabasePath());
    connectionInfo.setEncoding("NONE");
    FbDatabaseFactory databaseFactory = FBTestProperties.getFbDatabaseFactory();
    try (FbDatabase dbHandle1 = databaseFactory.connect(connectionInfo)) {
        dbHandle1.attach();
        FbTransaction trHandle1 = dbHandle1.startTransaction(tpb.getTransactionParameterBuffer());
        trHandle1.prepare(message);
    // No commit! We leave trHandle1 in Limbo.
    }
    try (FbDatabase dbHandle2 = databaseFactory.connect(connectionInfo)) {
        dbHandle2.attach();
        GDSHelper gdsHelper2 = new GDSHelper(dbHandle2);
        FbTransaction trHandle2 = dbHandle2.startTransaction(tpb.getTransactionParameterBuffer());
        gdsHelper2.setCurrentTransaction(trHandle2);
        FbStatement stmtHandle2 = dbHandle2.createStatement(trHandle2);
        stmtHandle2.prepare(RECOVERY_QUERY);
        final List<RowValue> rows = new ArrayList<>();
        StatementListener stmtListener = new DefaultStatementListener() {

            @Override
            public void receivedRow(FbStatement sender, RowValue rowValues) {
                rows.add(rowValues);
            }
        };
        stmtHandle2.addStatementListener(stmtListener);
        stmtHandle2.execute(RowValue.EMPTY_ROW_VALUE);
        stmtHandle2.fetchRows(10);
        DataProvider dataProvider0 = new DataProvider(rows, 0);
        DataProvider dataProvider1 = new DataProvider(rows, 1);
        FBField field0 = FBField.createField(stmtHandle2.getFieldDescriptor().getFieldDescriptor(0), dataProvider0, gdsHelper2, false);
        FBField field1 = FBField.createField(stmtHandle2.getFieldDescriptor().getFieldDescriptor(1), dataProvider1, gdsHelper2, false);
        boolean foundInLimboTx = false;
        int row = 0;
        while (row < rows.size()) {
            dataProvider0.setRow(row);
            dataProvider1.setRow(row);
            long inLimboTxId = field0.getLong();
            byte[] inLimboMessage = field1.getBytes();
            if (Arrays.equals(message, inLimboMessage)) {
                foundInLimboTx = true;
                FbTransaction inLimboTrHandle = dbHandle2.reconnectTransaction(inLimboTxId);
                assertEquals(inLimboTxId, inLimboTrHandle.getTransactionId());
                inLimboTrHandle.rollback();
                break;
            }
            row++;
        }
        stmtHandle2.close();
        trHandle2.commit();
        assertTrue("Should find in-limbo tx.", foundInLimboTx);
    }
}
Also used : FBField(org.firebirdsql.jdbc.field.FBField) DefaultStatementListener(org.firebirdsql.gds.ng.listeners.DefaultStatementListener) StatementListener(org.firebirdsql.gds.ng.listeners.StatementListener) ArrayList(java.util.ArrayList) GDSHelper(org.firebirdsql.gds.impl.GDSHelper) FieldDataProvider(org.firebirdsql.jdbc.field.FieldDataProvider) DefaultStatementListener(org.firebirdsql.gds.ng.listeners.DefaultStatementListener) RowValue(org.firebirdsql.gds.ng.fields.RowValue) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 GDSHelper (org.firebirdsql.gds.impl.GDSHelper)1 RowValue (org.firebirdsql.gds.ng.fields.RowValue)1 DefaultStatementListener (org.firebirdsql.gds.ng.listeners.DefaultStatementListener)1 StatementListener (org.firebirdsql.gds.ng.listeners.StatementListener)1 FBField (org.firebirdsql.jdbc.field.FBField)1 FieldDataProvider (org.firebirdsql.jdbc.field.FieldDataProvider)1 Test (org.junit.Test)1