Skip to content

Commit

Permalink
HHH-18866 - Fix CockroachDB test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Nov 20, 2024
1 parent de34086 commit 5d2c589
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
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;
Expand Down Expand Up @@ -117,8 +119,14 @@ public void testVersioning() throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jakarta.persistence.RollbackException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.CockroachDialect;

Expand All @@ -29,7 +30,7 @@
/**
* @author Vlad Mihalcea
*/
public class BatchOptimisticLockingTest extends
public class BatchOptimisticLockingTest extends
BaseNonConfigCoreFunctionalTestCase {

private final ExecutorService executorService = Executors.newSingleThreadExecutor();
Expand Down Expand Up @@ -94,17 +95,18 @@ public void testBatchAndOptimisticLocking() {
} );
}
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=?]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;

Expand All @@ -15,6 +16,7 @@
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;

Expand All @@ -41,6 +43,10 @@
}
)
@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;
Expand Down

0 comments on commit 5d2c589

Please sign in to comment.