Skip to content

Consistent CannotAcquireLockException translation for PostgreSQL serialization failure behind JPA #31274

Closed
@ah1508

Description

@ah1508

When isolation is Serializable Snapshot Isolation (SSI) the application must be prepared to retry rejected transactions. Quote from postgresql documentation: "It is important that an environment which uses this technique have a generalized way of handling serialization failures (which always return with an SQLSTATE value of '40001'), because it will be very hard to predict exactly which transactions might contribute to the read/write dependencies and need to be rolled back to prevent serialization anomalies.".

Spring retry could be the "generalized way" but based on what type of exception ?

With JDBC the SQLSTATE 40001 is translated in a CannotAcquireLockException.

With Jpa and postgresql it is depends:

  • if the commit is rejected (transactionManager.commit(tx)) a JpaSystemException (Unable to commit against JDBC Connection) is thrown. But the root cause of this exception is a SQLException with SQLSTATE 40001 .
  • if a statement is rejected the SQLException thrown by postgresql has SQLSTATE 40001 and is translated into a CannotAcquireLockException.

An option is to test the root exception:

boolean serializationFailure = NestedExceptionUtils.getRootCause(exception) instanceof SQLException se && "40001".equals(se.getSQLState());

At a high level the common denominator is DataAccessException, but then any data access problem would triggers a retry. Bad sql queries should result in test failure during dev but still, a specific SerializationFailureException would help. I don't know if it is realistic, is state 40001 really reliable across databases ? How about NoSQL ACID database ?

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions