Skip to content

Commit

Permalink
HHH-16249 - Add test for issue
Browse files Browse the repository at this point in the history
Disable batching in a stateless session when no transaction is active

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat authored and beikov committed Mar 27, 2023
1 parent edc60f1 commit ba9ea8b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public interface Expectation {

/**
* Is it acceptable to combiner this expectation with statement batching?
* Is it acceptable to combine this expectation with statement batching?
*
* @return True if batching can be combined with this expectation; false otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ private MutationExecutor executor(SharedSessionContractImplementor session, Muta
return session.getFactory()
.getServiceRegistry()
.getService( MutationExecutorService.class )
.createExecutor( () -> batchKey, group, session );
.createExecutor( ( session.getTransactionCoordinator() != null &&
session.getTransactionCoordinator().isTransactionActive() ? () -> batchKey : () -> null ),
group, session );
}

protected void applyLocking(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@
@Internal
public class InsertCoordinator extends AbstractMutationCoordinator {
private final MutationOperationGroup staticInsertGroup;
private final BasicBatchKey insertBatchKey;
private final BasicBatchKey batchKey;

public InsertCoordinator(AbstractEntityPersister entityPersister, SessionFactoryImplementor factory) {
super( entityPersister, factory );

if ( entityPersister.hasInsertGeneratedProperties() ) {
// disable batching in case of insert generated properties
insertBatchKey = null;
batchKey = null;
}
else {
insertBatchKey = new BasicBatchKey(
batchKey = new BasicBatchKey(
entityPersister.getEntityName() + "#INSERT",
null
);
Expand All @@ -80,7 +80,7 @@ public MutationOperationGroup getStaticInsertGroup() {
}

public BasicBatchKey getInsertBatchKey() {
return insertBatchKey;
return batchKey;
}

/**
Expand Down Expand Up @@ -301,11 +301,13 @@ protected Object doDynamicInserts(Object id, Object[] values, Object object, Sha
}
}

private MutationExecutor executor(SharedSessionContractImplementor session, MutationOperationGroup insertGroup) {
private MutationExecutor executor(SharedSessionContractImplementor session, MutationOperationGroup group) {
return session.getFactory()
.getServiceRegistry()
.getService( MutationExecutorService.class )
.createExecutor( () -> insertBatchKey, insertGroup, session );
.createExecutor( ( session.getTransactionCoordinator() != null &&
session.getTransactionCoordinator().isTransactionActive() ? () -> batchKey : () -> null ),
group, session );
}

protected static TableInclusionChecker getTableInclusionChecker(InsertValuesAnalysis insertValuesAnalysis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,9 @@ private MutationExecutor executor(SharedSessionContractImplementor session, Muta
return session.getSessionFactory()
.getServiceRegistry()
.getService( MutationExecutorService.class )
.createExecutor( () -> batchKey, group, session );
.createExecutor( ( session.getTransactionCoordinator() != null &&
session.getTransactionCoordinator().isTransactionActive() ? () -> batchKey : () -> null ),
group, session );
}

protected MutationOperationGroup generateDynamicUpdateGroup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.batch;

import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.query.SelectionQuery;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Jan Schatteman
*/
@DomainModel(
annotatedClasses = { BatchSizeAndStatelessSessionTest.TestEntity.class }
)
@SessionFactory
@TestForIssue( jiraKey = "HHH-16249")
public class BatchSizeAndStatelessSessionTest {

private final String countQuery = "select count(id) from TestEntity";
private final int batchSize = 3;
private final int total = 10;

@AfterEach
public void cleanup( SessionFactoryScope scope ) {
scope.inTransaction(
session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate()
);
}

@Test
public void testBatchWithStatelessSessionTx( SessionFactoryScope scope ) {
scope.inStatelessTransaction(
ss -> {
SelectionQuery<Long> query = ss.createSelectionQuery( countQuery, Long.class );
ss.setJdbcBatchSize( batchSize );
long intermediateCount = 0;
for ( int i = 1; i <= total; i++ ) {
ss.insert( new TestEntity(i) );
long count = query.getSingleResult();
// This should be batched, so the count should remain 0 or a multiple of the batch size and only change
// when a batch is executed
if ( i % batchSize == 0 ) {
assertEquals( i, count );
intermediateCount += batchSize;
} else {
assertEquals( intermediateCount, count );
}
}
}
);

checkTotal( scope );
}

@Test
public void testBatchWithStatelessSessionNoTx( SessionFactoryScope scope ) {
scope.inStatelessSession(
ss -> {
ss.setJdbcBatchSize( batchSize );
SelectionQuery<Long> query = ss.createSelectionQuery( countQuery, Long.class );
for ( int i = 1; i <= total; i++ ) {
ss.insert( new TestEntity( i ) );
long count = query.getSingleResult();
// There shouldn't be any batching here, so the count should go up one at a time
assertEquals( i, count );
}
}
);

checkTotal( scope );
}

@Test
public void testBatchWithStatelessSessionInParentTx( SessionFactoryScope scope ) {
scope.inSession(
s -> {
s.beginTransaction();
try (StatelessSession ss = s.getSessionFactory().openStatelessSession()) {
SelectionQuery<Long> query = ss.createSelectionQuery( countQuery, Long.class );
ss.setJdbcBatchSize(batchSize);
for ( int i = 1; i <= total; i++ ) {
ss.insert( new TestEntity(i) );
long count = query.getSingleResult();
// Even though it's inside a parent Tx, there's no batching here, so the count should go up one at a time
assertEquals( i, count );
}
}
s.getTransaction().commit();
}
);

checkTotal( scope );
}

private void checkTotal(SessionFactoryScope scope) {
scope.inSession(
s -> {
SelectionQuery<Long> q = s.createSelectionQuery( countQuery, Long.class );
assertEquals( total, q.getSingleResult() );
}
);
}

@Entity( name = "TestEntity" )
public static class TestEntity {
@Id
int id;

public TestEntity() {
}

public TestEntity( int id ) {
this.id = id;
}
}
}

0 comments on commit ba9ea8b

Please sign in to comment.