HHH-18866 - Fix CockroachDB test failures

This commit is contained in:
Karel Maesen 2024-11-04 22:39:23 +01:00 committed by Christian Beikov
parent 21095565bc
commit 894fa8475b
3 changed files with 23 additions and 7 deletions

View File

@ -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();

View File

@ -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=?]")

View File

@ -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;