Skip to content

MySQL Error 149 (Galera/WSREP conflict) not translated to ConcurrencyFailureException in Spring JDBC/ORM #36499

Closed

Description

@ThomasVerhoeven1998

Description

When using Spring Framework 7.0.2 with a MySQL/Percona Galera Cluster (v8.0.41), transactions failing due to a certification conflict during the COMMIT phase result in a generic org.springframework.orm.jpa.JpaSystemException instead of the expected org.springframework.dao.ConcurrencyFailureException.

In Galera-based environments, a certification conflict returns vendor error code 149 (ER_LOCK_DEADLOCK). Unlike the standard InnoDB deadlock code (1213), code 149 is not currently present in sql-error-codes.xml.

Currently, it falls through to JpaSystemException. Adding it to deadlockLoserCodes will ensure it is translated to PessimisticLockingFailureException (which replaced the deprecated DeadlockLoserDataAccessException).

Furthermore, in some environments (including the one observed), the driver/server returns this exception with an empty SQLState string. This prevents the SQLErrorCodeSQLExceptionTranslator from falling back to standard SQLState-based translation (e.g., mapping 40001 to a deadlock).

MySQL Error 149 (ER_LOCK_DEADLOCK) specifically indicates a deadlock where the transaction should be retried. While Spring typically handles MySQL error 1213 for deadlocks, error 149—which often appears during COMMIT operations in specific MySQL configurations or storage engines—is currently missed by the default translation logic.

Root cause

org.springframework.orm.jpa.JpaSystemException: could not execute statement [Got error 149 - 'Lock deadlock; Retry transaction' during COMMIT]
...
Caused by: java.sql.SQLException: Got error 149 - 'Lock deadlock; Retry transaction' during COMMIT
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121)
    ... 
    (Note: SQLState is "" and VendorCode is 149)

Steps to Reproduce

  1. Configure a Spring Data JPA application using Spring Framework 7.0.2.
  2. Connect to a Percona XtraDB Cluster or MySQL Galera Cluster.
  3. Simulate a high-concurrency scenario causing a WSREP certification conflict (detected at commit time).
  4. The database returns error 149.
  5. Spring throws JpaSystemException (via Hibernate's GenericJDBCException) instead of a ConcurrencyFailureException subclass.

Expected Behavior

The exception should be translated to org.springframework.dao.DeadlockLoserDataAccessException to allow for consistent exception handling and to trigger @retryable mechanisms that look for transient data access failures.

Possible Solution

The MySQL error code 149 should be added to the deadlockCodes list in Spring JDBC's sql-error-codes.xml for the MySQL entry.

Location: spring-jdbc/src/main/resources/org/springframework/jdbc/support/sql-error-codes.xml

<bean id="MySQL" class="org.springframework.jdbc.support.SQLErrorCodes">
    <property name="deadlockCodes">
        <value>1213,149</value> </property>
    ...
</bean>

Possible workaround

    @Bean
    public SQLErrorCodes sqlErrorCodes() {
        SQLErrorCodes mysqlCodes = SQLErrorCodesFactory.getInstance().getErrorCodes("MySQL");

        List<String> deadlockCodes = new ArrayList<>(Arrays.asList(mysqlCodes.getDeadlockLoserCodes()));

        if (!deadlockCodes.contains("149")) {
            deadlockCodes.add("149");
        }

        mysqlCodes.setDeadlockLoserCodes(deadlockCodes.toArray(new String[0]));

        return mysqlCodes;
    }

Activity

  1. added
    in: dataIssues in data modules (jdbc, orm, oxm, tx)
    on Mar 19, 2026
  2. self-assigned this
    on Mar 20, 2026
  3. added this to the 7.0.7 milestone on Mar 20, 2026
  4. jhoeller commented on Mar 20, 2026

    @jhoeller
    Contributor

    While I don't mind adding this code to our embedded sql-error-codes.xml file, note that the error codes mechanism is not active by default anymore but rather just triggered through custom SQLExceptionTranslator configuration - or through the presence of a (potentially empty) user-provided sql-error-codes.xml file in the root of the classpath which also loads the default error codes file.

    Also, DeadlockLoserDataAccessException has been deprecated for a few years now, in favor of CannotAcquireLockException and the common superclass PessimisticLockingFailureException. The latter or the even higher superclass ConcurrencyFailureException (which includes OptimisticLockingFailureException as well) is what I would recommend for retry handling in application code.

  5. ThomasVerhoeven1998 commented on Mar 21, 2026

    @ThomasVerhoeven1998
    Author

    @jhoeller thanks for the detailed reply. Could you explain how the translation happens at this moment? I assume there is still translation happening somewhere. Is there a replacement for the sql-error-codes.xml

  6. jhoeller commented on Mar 21, 2026

    @jhoeller
    Contributor

    By default since Spring Framework 6.0, it's a SQLExceptionSubclassTranslator which acts on specific SQLException subclasses. A SQLTransactionRollbackException will be translated to a PessimisticLockingFailureException, or to the more specific CannotAcquireLockException if the SQL state is "40001".

    As a fallback, there is our old SQLStateSQLExceptionTranslator kicking in. Most importantly, this applies to old-school JDBC drivers which always throw a plain SQLException (that is, never one of the JDBC 4 subclasses). According to the stacktrace, this seems to be the case with a MySQL cluster, unfortunately even with an empty SQL state there.

    So for scenarios like yours, it's probably best to stay on SQLErrorCodeSQLExceptionTranslator for the time being. Instead of modifying the state of SQLErrorCodesFactory, you could put your own variant of sql-error-codes.xml into the root of the classpath. This will trigger loading of the default file from spring-jdbc.jar but may override certain database definitions with custom beans, for example redefining the "MySQL" bean with a copy that adds the 149 error code.

    If there is no need to customize the error codes in org/springframework/jdbc/support/sql-error-codes.xml and you simply want SQLErrorCodeSQLExceptionTranslator to act on those default error codes, you may put an empty sql-error-codes.xml into the root of your classpath. This implicitly suppresses the default use of SQLExceptionSubclassTranslator.

    In order to decouple your application code from such translation setup specifics, I recommend retrying on PessimisticLockingFailureException or even the ConcurrencyFailureException superclass. This avoids an implicit dependency on SQLErrorCodeSQLExceptionTranslator with its selective DeadlockLoserDataAccessException throwing, as opposed to SQLExceptionSubclassTranslator which always throws CannotAcquireLockException in such a case.

    So in summary for your scenario, I recommend placing a custom sql-error-codes.xml file (which can be empty once we include the 149 error code by default) into the root of the classpath, and to avoid hard references to DeadlockLoserDataAccessException in your application code.

  7. added a commit that references this issue on Mar 21, 2026
    de562de
  8. added
    status: backportedAn issue that has been backported to maintenance branches
    and removed
    for: backport-to-7.0.xMarks an issue as a candidate for backport to 7.0.x
    on Mar 21, 2026
  9. added a commit that references this issue on Mar 21, 2026
    3997551
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions