-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable batching in a stateless session when no transaction is active Signed-off-by: Jan Schatteman <jschatte@redhat.com>
- Loading branch information
Showing
5 changed files
with
147 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...ate-core/src/test/java/org/hibernate/orm/test/batch/BatchSizeAndStatelessSessionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |