From 5d2c58982e7905497fc7ada50cf7f0574c7f2e6a Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Mon, 4 Nov 2024 22:39:23 +0100 Subject: [PATCH] HHH-18866 - Fix CockroachDB test failures --- .../entity/BasicHibernateAnnotationsTest.java | 12 ++++++++++-- .../orm/test/batch/BatchOptimisticLockingTest.java | 12 +++++++----- .../naturalid/compound/CompoundNaturalIdTest.java | 6 ++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/entity/BasicHibernateAnnotationsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/entity/BasicHibernateAnnotationsTest.java index f189d36938..58f75e5341 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/entity/BasicHibernateAnnotationsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/entity/BasicHibernateAnnotationsTest.java @@ -14,10 +14,12 @@ import java.util.List; import java.util.Locale; import java.util.Set; +import jakarta.persistence.RollbackException; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.community.dialect.DerbyDialect; +import org.hibernate.dialect.CockroachDialect; import org.hibernate.dialect.OracleDialect; import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.dialect.SybaseDialect; @@ -117,8 +119,14 @@ public class BasicHibernateAnnotationsTest extends BaseCoreFunctionalTestCase { parallelTx.commit(); fail( "All optimistic locking should have make it fail" ); } - catch (OptimisticLockException e) { - if ( parallelTx != null ) parallelTx.rollback(); + catch (Exception e) { + if (getDialect() instanceof CockroachDialect) { + // CockroachDB always runs in SERIALIZABLE isolation, and throws a RollbackException + assertTrue( e instanceof RollbackException ); + } else { + assertTrue( e instanceof OptimisticLockException ); + } + parallelTx.rollback(); } finally { parallelSession.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchOptimisticLockingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchOptimisticLockingTest.java index b8014ce734..f299c62442 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchOptimisticLockingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchOptimisticLockingTest.java @@ -10,6 +10,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import jakarta.persistence.RollbackException; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.CockroachDialect; @@ -29,7 +30,7 @@ import static org.junit.Assert.fail; /** * @author Vlad Mihalcea */ -public class BatchOptimisticLockingTest extends +public class BatchOptimisticLockingTest extends BaseNonConfigCoreFunctionalTestCase { private final ExecutorService executorService = Executors.newSingleThreadExecutor(); @@ -94,17 +95,18 @@ public class BatchOptimisticLockingTest extends } ); } catch (Exception expected) { - assertEquals( OptimisticLockException.class, expected.getClass() ); if ( getDialect() instanceof CockroachDialect ) { // CockroachDB always runs in SERIALIZABLE isolation, and uses SQL state 40001 to indicate - // serialization failure. - var msg = "org.hibernate.exception.LockAcquisitionException: could not execute batch"; + // serialization failure. The failure is mapped to a RollbackException. + assertEquals( RollbackException.class, expected.getClass() ); + var msg = "could not execute batch"; assertEquals( - "org.hibernate.exception.LockAcquisitionException: could not execute batch", + msg, expected.getMessage().substring( 0, msg.length() ) ); } else { + assertEquals( OptimisticLockException.class, expected.getClass() ); assertTrue( expected.getMessage() .startsWith("Batch update returned unexpected row count from update 1 (expected row count 1 but was 0) [update Person set name=?,version=? where id=? and version=?]") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/compound/CompoundNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/compound/CompoundNaturalIdTest.java index 5796cc1e20..f034acba09 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/compound/CompoundNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/compound/CompoundNaturalIdTest.java @@ -6,6 +6,7 @@ package org.hibernate.orm.test.mapping.naturalid.compound; import org.hibernate.annotations.NaturalId; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.CockroachDialect; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.JpaCriteriaQuery; @@ -15,6 +16,7 @@ import org.hibernate.testing.orm.junit.ServiceRegistry; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SkipForDialect; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -41,6 +43,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; } ) @SessionFactory +@SkipForDialect( + dialectClass = CockroachDialect.class, + reason = "On CockroachDB the difference between simple and compound natural id is very high" +) public class CompoundNaturalIdTest { private static final int OBJECT_NUMBER = 2000;