Given
* statement batch is enabled * A versioned entity * One managed instance of the versioned entity has dirty attributes * Another managed instance of the versioned entity has a dirty collection
When
* flushing
Then
* OptimisticLockException: Batch update returned unexpected row count from update
{code:java}@Entity @Table(name = "ENTITY_A") public class EntityA {
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
@Version @Column(name = "VERSION") long version;
@Column(name = "PROPERTY_A") int propertyA;
@ManyToMany @JoinTable(name = "ENTITY_A_TO_ENTITY_A", // inverseJoinColumns = @JoinColumn(name = "SIDE_B"), // joinColumns = @JoinColumn(name = "SIDE_A")) final List<EntityA> owners = new ArrayList<>();{code}
{code:java} @Override protected void configure(Configuration configuration) { super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString()); configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString()); configuration.setProperty(AvailableSettings.STATEMENT_BATCH_SIZE, "2"); }
// Add your tests, using standard JUnit. @Test public void hhhXXXTest() throws Exception { // BaseCoreFunctionalTestCase automatically creates the SessionFactory and // provides the Session. try (Session s = openSession()) { Transaction tx = s.beginTransaction(); EntityA ownerA1 = new EntityA(); EntityA entityA1 = new EntityA();
EntityA ownerA2 = new EntityA(); EntityA entityA2 = new EntityA();
s.persist(ownerA1); s.persist(ownerA2); s.persist(entityA1); s.persist(entityA2); s.flush();
entityA1.propertyA = 1; entityA2.owners.add(ownerA2); tx.commit(); } }{code}
{noformat}jakarta.persistence.OptimisticLockException: Batch update returned unexpected row count from update [1]; actual row count: 0; expected: 1; statement executed: update ENTITY_A set PROPERTY_A=?,VERSION=? where ID=? and VERSION=? at org.hibernate.internal.ExceptionConverterImpl.wrapStaleStateException(ExceptionConverterImpl.java:216) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:93) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:168) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1416) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:485) at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2296) at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:1961) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:426) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:169) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:267) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:75) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [1]; actual row count: 0; expected: 1; statement executed: update ENTITY_A set PROPERTY_A=?,VERSION=? where ID=? and VERSION=? at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67) at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54) at org.hibernate.engine.jdbc.batch.internal.BatchImpl.checkRowCounts(BatchImpl.java:320) at org.hibernate.engine.jdbc.batch.internal.BatchImpl.lambda$performExecution$2(BatchImpl.java:283) at org.hibernate.engine.jdbc.mutation.internal.PreparedStatementGroupSingleTable.forEachStatement(PreparedStatementGroupSingleTable.java:59) at org.hibernate.engine.jdbc.batch.internal.BatchImpl.performExecution(BatchImpl.java:265) at org.hibernate.engine.jdbc.batch.internal.BatchImpl.addToBatch(BatchImpl.java:155) at org.hibernate.engine.jdbc.mutation.internal.MutationExecutorSingleBatched.performBatchedOperations(MutationExecutorSingleBatched.java:60) at org.hibernate.engine.jdbc.mutation.internal.AbstractMutationExecutor.execute(AbstractMutationExecutor.java:45) at org.hibernate.persister.entity.mutation.UpdateCoordinatorStandard.doVersionUpdate(UpdateCoordinatorStandard.java:485) at org.hibernate.persister.entity.mutation.UpdateCoordinatorStandard.handlePotentialImplicitForcedVersionIncrement(UpdateCoordinatorStandard.java:407) at org.hibernate.persister.entity.mutation.UpdateCoordinatorStandard.coordinateUpdate(UpdateCoordinatorStandard.java:146) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2739) at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:166) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:616) at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:487) at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:484) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:358) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1412) ... 23 more
{noformat}
the failing test scenario to be is attached and also available here [https://github.com/ratoaq2/HHH-16394|https://github.com/ratoaq2/HHH-16394|smart-link] |
|