Repository navigation
Description
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
- Configure a Spring Data JPA application using Spring Framework 7.0.2.
- Connect to a Percona XtraDB Cluster or MySQL Galera Cluster.
- Simulate a high-concurrency scenario causing a WSREP certification conflict (detected at commit time).
- The database returns error 149.
- 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
jhoeller commentedon Mar 20, 2026
While I don't mind adding this code to our embedded
sql-error-codes.xmlfile, note that the error codes mechanism is not active by default anymore but rather just triggered through customSQLExceptionTranslatorconfiguration - or through the presence of a (potentially empty) user-providedsql-error-codes.xmlfile in the root of the classpath which also loads the default error codes file.Also,
DeadlockLoserDataAccessExceptionhas been deprecated for a few years now, in favor ofCannotAcquireLockExceptionand the common superclassPessimisticLockingFailureException. The latter or the even higher superclassConcurrencyFailureException(which includesOptimisticLockingFailureExceptionas well) is what I would recommend for retry handling in application code.ThomasVerhoeven1998 commentedon Mar 21, 2026
@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
jhoeller commentedon Mar 21, 2026
By default since Spring Framework 6.0, it's a
SQLExceptionSubclassTranslatorwhich acts on specificSQLExceptionsubclasses. ASQLTransactionRollbackExceptionwill be translated to aPessimisticLockingFailureException, or to the more specificCannotAcquireLockExceptionif the SQL state is "40001".As a fallback, there is our old
SQLStateSQLExceptionTranslatorkicking in. Most importantly, this applies to old-school JDBC drivers which always throw a plainSQLException(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
SQLErrorCodeSQLExceptionTranslatorfor the time being. Instead of modifying the state ofSQLErrorCodesFactory, you could put your own variant ofsql-error-codes.xmlinto the root of the classpath. This will trigger loading of the default file fromspring-jdbc.jarbut 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.xmland you simply wantSQLErrorCodeSQLExceptionTranslatorto act on those default error codes, you may put an emptysql-error-codes.xmlinto the root of your classpath. This implicitly suppresses the default use ofSQLExceptionSubclassTranslator.In order to decouple your application code from such translation setup specifics, I recommend retrying on
PessimisticLockingFailureExceptionor even theConcurrencyFailureExceptionsuperclass. This avoids an implicit dependency onSQLErrorCodeSQLExceptionTranslatorwith its selectiveDeadlockLoserDataAccessExceptionthrowing, as opposed toSQLExceptionSubclassTranslatorwhich always throwsCannotAcquireLockExceptionin such a case.So in summary for your scenario, I recommend placing a custom
sql-error-codes.xmlfile (which can be empty once we include the 149 error code by default) into the root of the classpath, and to avoid hard references toDeadlockLoserDataAccessExceptionin your application code.Recognize error code 149 as deadlock loser (MySQL Galera)
Recognize error code 149 as deadlock loser (MySQL Galera)