Skip to content

Commit

Permalink
HHH-18760 - CockroachDB dialect remaps serialization error
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Oct 29, 2024
1 parent ac74e3b commit e566b0a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.TransactionSerializationException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
Expand Down Expand Up @@ -1088,6 +1089,10 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return null;
}
return switch (sqlState) {
// Serialization Exception
case "40001" -> message.contains("WriteTooOldError")
? new TransactionSerializationException( message, sqlException, sql )
: null;
// DEADLOCK DETECTED
case "40P01" -> new LockAcquisitionException( message, sqlException, sql );
// LOCK NOT AVAILABLE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import org.hibernate.JDBCException;

import java.sql.SQLException;
/**
* A {@link JDBCException} indicating a transaction failed because it could not be placed into a serializable ordering
* among all of the currently-executing transactions
*
* @author Karel Maesen
*/
public class TransactionSerializationException extends JDBCException {
public TransactionSerializationException(String message, SQLException cause) {
super( message, cause );
}

public TransactionSerializationException(String message, SQLException cause, String sql) {
super( message, cause, sql );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hibernate.engine.spi.ExceptionConverter;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.TransactionSerializationException;
import org.hibernate.loader.MultipleBagFetchException;
import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.SemanticException;
Expand Down Expand Up @@ -139,6 +140,11 @@ else if ( exception instanceof TransientObjectException ) {
//Spec 3.2.3 Synchronization rules
return new IllegalStateException( exception );
}
else if ( exception instanceof TransactionSerializationException ) {
final PersistenceException converted = new RollbackException( exception.getMessage(), exception );
rollbackIfNecessary( converted );
return converted;
}
else {
rollbackIfNecessary( exception );
return exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import java.util.List;

import jakarta.persistence.OptimisticLockException;
import jakarta.persistence.RollbackException;
import org.hibernate.StaleObjectStateException;
import org.hibernate.cfg.AvailableSettings;

import org.hibernate.exception.TransactionSerializationException;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
Expand Down Expand Up @@ -136,6 +138,10 @@ public void testFailedUpdate(SessionFactoryScope scope) {
catch (OptimisticLockException ole) {
assertTrue( ole.getCause() instanceof StaleObjectStateException );
}
//CockroachDB errors with a Serialization Exception
catch (RollbackException rbe) {
assertTrue( rbe.getCause() instanceof TransactionSerializationException );
}
}

@Entity(name = "EntityA")
Expand Down

0 comments on commit e566b0a

Please sign in to comment.