Выполните восстановление транзакции на postgres db, используя jdbc - PullRequest
0 голосов
/ 31 января 2020

Я пытаюсь проверить восстановление XA (транзакции) на postgres.

Настройка:

  • База данных: postgres: 12 docker image
  • JDB C Драйвер: org. postgresql: postgresql: 42.2.9
  • Сервер приложений: Open Liberty последняя

Настройка тестирования

public void testXARecovery() throws Throwable {
    Connection[] cons = new Connection[3];
    tran.begin();
    try {
        // Use unsharable connections, so that they all get their own XA resources
        cons[0] = ds4u_2.getConnection();
        cons[1] = ds4u_2.getConnection();
        cons[2] = ds4u_2.getConnection();

        // Verify isolation-level="TRANSACTION_READ_COMMITTED"
        ...

        PreparedStatement pstmt;

        //Use all three connection to insert row into table
        ...

        //Ensure only the first connection is successful
        TestXAResource.assignSuccessLimit(1, cons);
        try {
            tran.commit();
            throw new Exception("Commit should not have succeeded because the test infrastructure is supposed to cause an in-doubt transaction.");
        } catch (HeuristicMixedException x) {
            //Allow prepared transactions to once again succeed
            TestXAResource.removeSuccessLimit(cons);
            System.out.println("Caught expected HeuristicMixedException: " + x.getCause() + x.getMessage());
        }
    } catch (Throwable x) {
        TestXAResource.removeSuccessLimit(cons);
        //cleanup
        ...
    } finally {
        //close connections
        ...
    }

    // At this point, the transaction is in-doubt.
    // We won't be able to access the data until the transaction manager recovers
    // the transaction and resolves it.
    //
    // A connection configured with TRANSACTION_SERIALIZABLE is necessary in
    // order to allow the recovery to kick in before using the connection.

    System.out.println("attempting to access data (only possible after recovery)");
    Connection con = ds4u_8.getConnection();

    try {
        ResultSet result;
        PreparedStatement pstmt = con.prepareStatement("select name, population, county from cities where name = ?");

        pstmt.setString(1, "Edina");
        result = pstmt.executeQuery();
        if (!result.next())
            throw new Exception("Missing first entry in database.");

        pstmt.setString(1, "St. Louis Park");
        result = pstmt.executeQuery();
        if (!result.next())
            throw new Exception("Missing second entry in database."); //THIS IS WHERE WE FAIL

        pstmt.setString(1, "Moorhead");
        result = pstmt.executeQuery();
        if (!result.next())
            throw new Exception("Missing third entry in database.");

        System.out.println("successfully accessed the data");
    } finally {
        con.close();
    }
}

Эта настройка тестирования работает для derby, sqlserver и db2. Но по какой-то причине postgres никогда не выполняет восстановление xa.

Дополнительная информация: Нет необычного вывода из драйвера JDB C. Все выглядит так же, как и для других баз данных. Хотите знать, если это доходит до фактического уровня базы данных?

Вопросы:

  • Есть ли параметр, который мне нужно установить, чтобы включить восстановление xa?
  • Поддерживает ли драйвер postgres jdb c восстановление xa?
  • Есть ли что-то на стороне базы данных, которое необходимо настроить? (уже включены подготовленные транзакции)
...