Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-18760 - CockroachDB dialect remaps serialization error #9152

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading