检查 org.jooq.exception.DataAccessException 是否序列化失败

check if org.jooq.exception.DataAccessException is serialization failure

我在 Java 中收到 org.jooq.exception.DataAccessException 一条消息

ERROR: could not serialize access due to read/write dependencies among transactions Detail: Reason code: Canceled on identification as a pivot, during conflict in checking. Hint: The transaction might succeed if retried.

我想查看数据访问异常的错误代码,这是幕后的Postgres数据库。例如序列化失败异常错误码为40001.

如何查看 org.jooq.exception.DataAccessException 的错误代码?

我的动机是在序列化失败时重试事务。

在 jOOQ 3.8 中,SQLStateClass and SQLStateSubclass were introduced (#4904) 使您能够安全地访问 SQL 标准 SQL 状态值。

catch (DataAccessException e) {
    System.out.println(e.sqlStateClass());
    System.out.println(e.sqlStateSubclass());

    if (SQLStateSubclass.C40001_SERIALIZATION_FAILURE == e.sqlStateSubclass()) {
        ...
    }
}

在 jOOQ 3.8 之前,您可以访问底层 SQLException 以访问这些 SQL 状态值:

catch (DataAccessException e) {
    if (e.getCause() instanceof SQLException &&
       ("40001".equals(((SQLException) e.getCause()).getSQLState()))) {
        ...
    }
}