Use try-with-resources to ensure scroll and stream tests close underlying resources

This commit is contained in:
Christian Beikov 2023-08-15 12:39:57 +02:00
parent 95efb07da7
commit b2fda37773
26 changed files with 712 additions and 680 deletions

View File

@ -455,10 +455,10 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
q.setParameter( "count", new Integer( -1 ) );
assertTrue( q.list().size() == 1 );
ScrollableResults sr = s.createQuery( "from Simple s" ).scroll();
try (ScrollableResults sr = s.createQuery( "from Simple s" ).scroll()) {
sr.next();
sr.get();
sr.close();
}
s.delete( other );
s.delete( simple );

View File

@ -92,16 +92,17 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
s.setCacheMode( CacheMode.IGNORE );
t = s.beginTransaction();
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get();
DataPoint dp = (DataPoint) sr.get();
dp.setDescription( "done!" );
if ( ++i % nBeforeFlush == 0 ) {
s.flush();
s.clear();
}
}
}
t.commit();
s.close();
@ -109,16 +110,17 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
s.setCacheMode( CacheMode.IGNORE );
t = s.beginTransaction();
i = 0;
sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get();
DataPoint dp = (DataPoint) sr.get();
s.delete( dp );
if ( ++i % nBeforeFlush == 0 ) {
s.flush();
s.clear();
}
}
}
t.commit();
s.close();
}

View File

@ -79,7 +79,7 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
@ -95,6 +95,7 @@ public class EagerManyToOneStreamTest {
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
}
);
}
@ -106,7 +107,7 @@ public class EagerManyToOneStreamTest {
session -> {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c ", Child.class );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() );
// with Stream the association is not batch loaded
@ -130,6 +131,7 @@ public class EagerManyToOneStreamTest {
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
}
}
);
}
@ -140,10 +142,12 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c", Child.class );
query.getResultStream().forEach(
try (Stream<Child> resultStream = query.getResultStream()) {
resultStream.forEach(
child -> assertThat( child.getParent() ).isNotNull()
);
}
}
);
}
@ -154,11 +158,12 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse();
assertThat( child.get().getParent() ).isNotNull();
}
}
);
}

View File

@ -80,7 +80,7 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
@ -95,6 +95,7 @@ public class LazyManyToOneStreamTest {
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
}
);
}
@ -106,7 +107,7 @@ public class LazyManyToOneStreamTest {
session -> {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c order by id", Child.class );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
@ -130,6 +131,7 @@ public class LazyManyToOneStreamTest {
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
}
);
}
@ -140,7 +142,7 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse();
@ -150,6 +152,7 @@ public class LazyManyToOneStreamTest {
assertThat( Hibernate.isInitialized( parent ) ).isFalse();
}
}
);
}

View File

@ -138,7 +138,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select d from D d";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final DEntity dEntity = (DEntity) scrollableResults.get();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
@ -147,6 +147,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
}
}
}
);
inSession(
@ -231,7 +232,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select e from E e join fetch e.d";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final EEntity eEntity = (EEntity) scrollableResults.get();
final DEntity dEntity = eEntity.getD();
@ -239,6 +240,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
}
}
}
);
inStatelessSession(
@ -251,7 +253,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0;
while ( scrollableResults.next() ) {
i++;
@ -260,6 +262,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
}
assertThat( i, is( 1 ) );
}
}
);
inStatelessSession(
@ -267,11 +270,12 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select g from G g join fetch g.dEntities";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final Object o = scrollableResults.get();
}
}
}
);
}
@ -296,7 +300,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0;
while ( scrollableResults.next() ) {
i++;
@ -307,6 +311,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
}
assertThat( i, is( 1 ) );
}
}
);
}

View File

@ -111,14 +111,14 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
// both scroll() and iterate() cause batching to hold on
// to resources, which should make aggressive-release not release
// the connection (and thus cause serialization to fail)
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
sr.next();
try {
SerializationHelper.serialize( s );
fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
}
catch( IllegalStateException e ) {
catch (IllegalStateException e) {
// expected behavior
}
@ -128,7 +128,7 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
// Closing the ScrollableResults does currently force batching to
// aggressively release the connection
sr.close();
}
SerializationHelper.serialize( s );
s.delete( silly );
@ -146,22 +146,19 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
s.save( silly );
s.flush();
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
assertTrue( sr.next() );
Silly silly2 = ( Silly ) sr.get();
Silly silly2 = (Silly) sr.get();
assertEquals( silly, silly2 );
sr.close();
sr = s.createQuery( "from Silly" ).scroll();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll();
}
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll()) {
assertTrue( sr.next() );
assertEquals( silly, sr.get() );
assertTrue( sr2.next() );
assertEquals( silly, sr2.get() );
sr.close();
sr2.close();
}
s.delete( silly );
s.flush();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.orm.test.connections;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.engine.spi.SessionImplementor;
@ -134,14 +135,15 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
Session sessionUnderTest = getSessionUnderTest();
// force the connection to be retained
sessionUnderTest.createQuery( "from Silly" ).scroll().next();
try (ScrollableResults sr = sessionUnderTest.createQuery( "from Silly" ).scroll()) {
sr.next();
try {
SerializationHelper.serialize( sessionUnderTest );
fail( "Serialization of connected session allowed!" );
}
catch( IllegalStateException e ) {
catch (IllegalStateException e) {
// expected behaviour
}
finally {
@ -149,6 +151,7 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
done();
}
}
}
/**
* Tests to validate that a session holding JDBC resources will not
@ -247,7 +250,7 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
sessionUnderTest.save( silly );
sessionUnderTest.flush();
sessionUnderTest.createQuery( "from Silly" ).scroll();
try (ScrollableResults sr = sessionUnderTest.createQuery( "from Silly" ).scroll()) {
disconnect( sessionUnderTest );
SerializationHelper.serialize( sessionUnderTest );
@ -260,6 +263,7 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
release( sessionUnderTest );
done();
}
}
/**
* Test that the basic session usage template works in all environment

View File

@ -3267,17 +3267,17 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
assertTrue( ( (Map) results.get( 0 ) ).containsKey("descr") );
assertTrue( ( (Map) results.get( 0 ) ).containsKey("bw") );
ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll();
try (ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
obj = sr.get();
assertTrue( "Incorrect return type", obj instanceof Map );
assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 );
sr.close();
}
sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll();
try (ScrollableResults sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect return type", sr.get() instanceof Animal );
sr.close();
}
// caching...
QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" );
@ -3558,13 +3558,12 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
session = openSession();
t = session.beginTransaction();
ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll();
try (ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect return type", sr.get() instanceof Animal );
assertFalse( session.contains( sr.get() ) );
sr.close();
}
t.commit();
session.close();
@ -3621,12 +3620,11 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
session = openSession();
t = session.beginTransaction();
ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll();
try (ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect return type", sr.get() instanceof Map );
sr.close();
}
t.commit();
session.close();
@ -3816,7 +3814,7 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
s.beginTransaction();
Query query = s.createQuery( hql );
preparer.prepare( query );
query.scroll();
query.scroll().close();
s.getTransaction().commit();
s.close();
return this;

View File

@ -958,20 +958,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
.collect(Collectors.toList());
}
//end::jpql-api-stream-example[]
// tag::jpql-api-stream-terminal-operation[]
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where p.name like :name",
Person.class)
.setParameter("name", "J%")
.getResultStream()
.skip(5)
.limit(5)
.collect(Collectors.toList());
//end::jpql-api-stream-terminal-operation[]
});
}

View File

@ -53,11 +53,12 @@ public class ScrollableCollectionFetchingTest {
session -> {
session.createMutationQuery("insert Mammal (description, bodyWeight, pregnant) values ('Human', 80.0, false)").executeUpdate();
assertEquals( 1L, session.createSelectionQuery("select count(*) from Mammal").getSingleResult() );
ScrollableResults results = session.createQuery("select a, a.bodyWeight from Animal a left join fetch a.offspring").scroll();
try (ScrollableResults results = session.createQuery("select a, a.bodyWeight from Animal a left join fetch a.offspring").scroll()) {
assertTrue( results.next() );
Object[] result = (Object[]) results.get();
assertTrue(Hibernate.isInitialized(((Animal) result[0]).getOffspring()));
session.createMutationQuery("delete Mammal").executeUpdate();
assertTrue( Hibernate.isInitialized( ( (Animal) result[0] ).getOffspring() ) );
session.createMutationQuery( "delete Mammal" ).executeUpdate();
}
}
);
}
@ -66,8 +67,9 @@ public class ScrollableCollectionFetchingTest {
public void testTupleReturnWithFetchFailure(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
try {
session.createQuery( "select a.description, a.bodyWeight from Animal a inner join fetch a.offspring" ).scroll();
try (ScrollableResults<?> sr = session.createQuery(
"select a.description, a.bodyWeight from Animal a inner join fetch a.offspring" )
.scroll()) {
fail( "scroll allowed with fetch and projection result" );
}
catch (IllegalArgumentException e) {
@ -82,8 +84,8 @@ public class ScrollableCollectionFetchingTest {
public void testUknownPathFailure(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
try {
session.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll();
try (ScrollableResults<?> sr = session.createQuery(
"select a, a.weight from Animal a inner join fetch a.offspring" ).scroll()) {
fail( "scroll allowed with unknown path" );
}
catch (IllegalArgumentException e) {
@ -106,7 +108,7 @@ public class ScrollableCollectionFetchingTest {
assertEquals( 0, size );
// now get the scrollable results
ScrollableResults results = s.createQuery( query ).setParameter( "desc", "root%" ).scroll();
try (ScrollableResults results = s.createQuery( query ).setParameter( "desc", "root%" ).scroll()) {
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
@ -157,6 +159,7 @@ public class ScrollableCollectionFetchingTest {
assertFalse( results.isLast() );
}
}
}
);
}
@ -190,10 +193,10 @@ public class ScrollableCollectionFetchingTest {
.setParameter( "desc", "root%" )
.uniqueResult() );
ScrollableResults results = session
try (ScrollableResults results = session
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ).scroll();
.setParameter( "desc", "root%" ).scroll()) {
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
@ -271,6 +274,7 @@ public class ScrollableCollectionFetchingTest {
}
}
}
}
);
scope.inTransaction(
@ -292,11 +296,11 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
s -> {
ScrollableResults results = s
try (ScrollableResults results = s
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
int counter = 0;
while ( results.next() ) {
@ -305,7 +309,7 @@ public class ScrollableCollectionFetchingTest {
checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
}
}
);
}
@ -319,10 +323,10 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
s -> {
ScrollableResults results = s
try (ScrollableResults results = s
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ).scroll();
.setParameter( "desc", "root%" ).scroll()) {
results.afterLast();
@ -333,7 +337,7 @@ public class ScrollableCollectionFetchingTest {
checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
}
}
);
}
@ -347,11 +351,11 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
session -> {
ScrollableResults results = session
try (ScrollableResults results = session
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" )
.scroll();
.scroll()) {
results.first();
Animal animal = (Animal) results.get();
@ -373,6 +377,7 @@ public class ScrollableCollectionFetchingTest {
animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" );
}
}
);
}

View File

@ -55,10 +55,11 @@ public class ScrollableResultsObjectArrayCastingTest {
scope.inTransaction(
(entityManager) -> {
TypedQuery<byte[]> typedQuery = entityManager.createQuery( "select p.binaryValue from Product p", byte[].class );
Stream<byte[]> stream = typedQuery.getResultStream();
try (Stream<byte[]> stream = typedQuery.getResultStream()) {
//noinspection ResultOfMethodCallIgnored
stream.findFirst();
}
}
);
}

View File

@ -51,7 +51,7 @@ public class ScrollTest {
scope.inTransaction(
s -> {
ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll();
try (ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll()) {
assertTrue( sr.next() );
Item i1 = (Item) sr.get();
assertTrue( sr.next() );
@ -64,6 +64,7 @@ public class ScrollTest {
s.delete( i1 );
s.delete( i2 );
}
}
);
assertThat( scope.getSessionFactory().getStatistics().getEntityFetchCount(), is( 0L ) );

View File

@ -113,25 +113,27 @@ public class IdentityJoinedSubclassBatchingTest {
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
e.setTitle( "Unknown" );
}
}
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}
@ -158,14 +160,15 @@ public class IdentityJoinedSubclassBatchingTest {
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}
@ -191,14 +194,15 @@ public class IdentityJoinedSubclassBatchingTest {
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}

View File

@ -106,25 +106,27 @@ public class JoinedSubclassBatchingTest {
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
e.setTitle( "Unknown" );
}
}
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}

View File

@ -111,25 +111,27 @@ public class SequenceJoinedSubclassBatchingTest {
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
e.setTitle( "Unknown" );
}
}
} );
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}
@ -153,14 +155,15 @@ public class SequenceJoinedSubclassBatchingTest {
scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery(
try (ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
Employee e = (Employee) sr.get();
s.delete( e );
}
}
} );
}

View File

@ -218,9 +218,9 @@ public class CriteriaCompilingTest extends BaseEntityManagerFunctionalTestCase {
TypedQuery<Order> jpaQuery = session.createQuery( query );
Query<?> hibQuery = jpaQuery.unwrap( Query.class );
ScrollableResults sr = hibQuery.scroll( ScrollMode.FORWARD_ONLY );
hibQuery.scroll( ScrollMode.FORWARD_ONLY ).close();
hibQuery.setCacheMode( CacheMode.IGNORE ).scroll( ScrollMode.FORWARD_ONLY );
hibQuery.setCacheMode( CacheMode.IGNORE ).scroll( ScrollMode.FORWARD_ONLY ).close();
Query<Order> anotherQuery = session.createQuery(
"select o from Order o where totalPrice in :totalPrices",

View File

@ -85,12 +85,13 @@ public class CriteriaToScrollableResultsFetchTest extends BaseEntityManagerFunct
hibernateQuery.setCacheable( false );
List<OrderLine> lines = new ArrayList<>();
ScrollableResults scrollableResults = hibernateQuery.scroll();
try (ScrollableResults scrollableResults = hibernateQuery.scroll()) {
scrollableResults.last();
int rows = scrollableResults.getRowNumber() + 1;
scrollableResults.beforeFirst();
while ( scrollableResults.next() ) {
lines.add( (OrderLine) scrollableResults.get( ) );
lines.add( (OrderLine) scrollableResults.get() );
}
}
assertNotNull( lines );
assertEquals( "Expected one order line", 1, lines.size() );

View File

@ -25,8 +25,9 @@ public class ProcedureCallImplTest {
scope.inTransaction( em -> {
em.createNativeQuery("CREATE ALIAS GET_RANDOM_VALUE FOR \"java.lang.Math.random\";").executeUpdate();
Query query = em.createStoredProcedureQuery("GET_RANDOM_VALUE");
Stream stream = query.getResultStream();
Assert.assertEquals(1, stream.count());
try (Stream stream = query.getResultStream()) {
Assert.assertEquals( 1, stream.count() );
}
} );
}
}

View File

@ -49,8 +49,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isInstanceOf( Product.class );
}
{
final ScrollableResults<?> results = query.scroll();
try (ScrollableResults<?> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object result = results.get();
assertThat( result ).isInstanceOf( Product.class );
@ -71,8 +70,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull();
}
{
final ScrollableResults<Product> results = query.scroll();
try (ScrollableResults<Product> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Product result = results.get();
assertThat( result ).isNotNull();
@ -96,8 +94,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( (Object[]) result ).hasExactlyElementsOfTypes(Product.class, Vendor.class, Product.class, Vendor.class);
}
{
final ScrollableResults<?> results = query.scroll();
try (ScrollableResults<?> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object result = results.get();
assertThat( result ).isNotNull();
@ -153,8 +150,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).hasExactlyElementsOfTypes(Product.class, Vendor.class, Product.class, Vendor.class);
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -182,8 +178,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).hasExactlyElementsOfTypes(Product.class, Vendor.class);
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (final ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -212,8 +207,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result[ 0 ] ).isInstanceOf( Product.class );
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -237,8 +231,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull();
}
{
final ScrollableResults<Product> results = query.scroll();
try (ScrollableResults<Product> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Product result = results.get();
assertThat( result ).isNotNull();

View File

@ -68,7 +68,7 @@ public class ScrollableResultsTests {
scope.inTransaction(
session -> {
final QueryImplementor<String> query = session.createQuery( SINGLE_SELECTION_QUERY, String.class );
final ScrollableResultsImplementor<String> results = query.scroll( ScrollMode.SCROLL_INSENSITIVE );
try (ScrollableResultsImplementor<String> results = query.scroll( ScrollMode.SCROLL_INSENSITIVE )) {
// try to initially read in reverse - should be false
assertThat( results.previous(), is( false ) );
@ -115,7 +115,7 @@ public class ScrollableResultsTests {
assertThat( results.scroll( -1 ), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
}
}
);
}
@ -262,7 +262,7 @@ public class ScrollableResultsTests {
}
private static <R> void verifyScroll(Query<R> query, Consumer<R> validator) {
try ( final ScrollableResults<R> results = query.scroll( ScrollMode.FORWARD_ONLY ) ) {
try (final ScrollableResults<R> results = query.scroll( ScrollMode.FORWARD_ONLY ) ) {
assertThat( results.next(), is( true ) );
validator.accept( results.get() );
}

View File

@ -109,8 +109,8 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction();
s.setDefaultReadOnly( true );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false );
while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get();
@ -119,6 +119,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -148,9 +149,9 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction();
s.setDefaultReadOnly( true );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) {
@ -158,6 +159,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -187,9 +189,9 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction();
assertFalse( s.isDefaultReadOnly() );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) {
@ -197,6 +199,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -228,7 +231,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
int i = 0;
Query query = s.createQuery( "from DataPoint dp order by dp.x asc" );
s.setDefaultReadOnly( true );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false );
while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get();
@ -237,6 +240,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -287,14 +291,14 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertTrue( query.isReadOnly() );
s.setDefaultReadOnly( false );
assertFalse( s.isDefaultReadOnly() );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0;
try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
assertFalse( s.isDefaultReadOnly() );
assertTrue( query.isReadOnly() );
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertFalse( s.isReadOnly( dpLast ) );
query.setReadOnly( false );
assertFalse( query.isReadOnly() );
int nExpectedChanges = 0;
assertFalse( s.isDefaultReadOnly() );
while ( sr.next() ) {
assertFalse( s.isDefaultReadOnly() );
@ -313,6 +317,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
dp.setDescription( "done!" );
}
assertFalse( s.isDefaultReadOnly() );
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -363,13 +368,13 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertFalse( query.isReadOnly() );
s.setDefaultReadOnly( true );
assertTrue( s.isDefaultReadOnly() );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0;
try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
assertFalse( query.isReadOnly() );
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertTrue( s.isReadOnly( dpLast ) );
query.setReadOnly( true );
assertTrue( query.isReadOnly() );
int nExpectedChanges = 0;
assertTrue( s.isDefaultReadOnly() );
while ( sr.next() ) {
assertTrue( s.isDefaultReadOnly() );
@ -388,6 +393,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
dp.setDescription( "done!" );
}
assertTrue( s.isDefaultReadOnly() );
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -690,10 +696,10 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertFalse( s.isReadOnly( dpLast ) );
s.setDefaultReadOnly( true );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
s.setDefaultReadOnly( false );
int nExpectedChanges = 0;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false );
while ( sr.next() ) {
dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
@ -709,6 +715,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();
@ -741,10 +748,10 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertTrue( s.isReadOnly( dpLast ) );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
@ -760,6 +767,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
t = s.beginTransaction();

View File

@ -115,9 +115,9 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
try {
session.getTransaction().begin();
int i = 0;
ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) {
@ -125,6 +125,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
session.getTransaction().commit();
assertUpdateCount( 1, scope );
@ -172,9 +173,9 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
assertInsertCount( 0, scope );
assertUpdateCount( 0, scope );
ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
assertInsertCount( 100, scope );
assertUpdateCount( 0, scope );
@ -186,6 +187,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
session.delete( dp );
}
}
}
);
assertUpdateCount( 0, scope );
@ -462,10 +464,10 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertFalse( s.isReadOnly( dpLast ) );
int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
@ -481,6 +483,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();
@ -529,10 +532,10 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
assertUpdateCount( 0, scope );
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
@ -548,6 +551,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
}
dp.setDescription( "done!" );
}
}
t.commit();
s.clear();

View File

@ -60,7 +60,7 @@ public class ResultTransformerTest {
}
);
ScrollableResults sr = q.scroll();
try (ScrollableResults sr = q.scroll()) {
// HANA supports only ResultSet.TYPE_FORWARD_ONLY and
// does not support java.sql.ResultSet.first()
if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof AbstractHANADialect ) {
@ -71,10 +71,10 @@ public class ResultTransformerTest {
}
Object obj = sr.get();
assertTrue(obj instanceof PartnerA);
assertTrue( obj instanceof PartnerA );
PartnerA obj2 = (PartnerA) obj;
assertEquals("Partner A", obj2.getName());
assertEquals( "Partner A", obj2.getName() );
}
} );
}
}

View File

@ -75,11 +75,11 @@ public class StatelessSessionTest {
assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() );
ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
.scroll( ScrollMode.FORWARD_ONLY )) {
sr.next();
doc2 = (Document) sr.get();
sr.close();
}
assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() );
@ -100,10 +100,10 @@ public class StatelessSessionTest {
criteria = criteriaBuilder.createQuery( Document.class );
criteria.from( Document.class );
sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) {
sr.next();
doc2 = (Document) sr.get();
sr.close();
}
assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() );

View File

@ -105,7 +105,7 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction(
session -> {
final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" );
final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( scrollableResults.next() ) {
Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) );
@ -114,6 +114,7 @@ public class StatelessSessionFetchingTest {
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
}
}
}
);
}
@ -148,7 +149,7 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction(
session -> {
final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" );
final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY );
try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( scrollableResults.next() ) {
Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) );
@ -157,6 +158,7 @@ public class StatelessSessionFetchingTest {
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
}
}
}
);
}
@ -189,12 +191,12 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction(
session -> {
final Query query = session.createQuery( "select p from Producer p join fetch p.products" );
final ScrollableResults scrollableResults = getScrollableResults(
try (ScrollableResults scrollableResults = getScrollableResults(
query,
scope.getSessionFactory()
.getJdbcServices()
.getDialect()
);
)) {
while ( scrollableResults.next() ) {
Producer producer = (Producer) scrollableResults.get();
@ -207,6 +209,7 @@ public class StatelessSessionFetchingTest {
}
}
}
}
);
}

View File

@ -60,26 +60,32 @@ public class JpaStreamTest {
scope.inTransaction( session -> {
// Test stream query without type.
Object result = session.createQuery( "From MyEntity" ).getResultStream().findFirst().orElse( null );
Object result;
try (Stream stream = session.createQuery( "From MyEntity" ).getResultStream()) {
result = stream.findFirst().orElse( null );
}
assertTyping( MyEntity.class, result );
// Test stream query with type.
result = session.createQuery( "From MyEntity", MyEntity.class )
.getResultStream()
.findFirst()
.orElse( null );
try (Stream stream = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream()) {
result = stream.findFirst().orElse( null );
}
assertTyping( MyEntity.class, result );
// Test stream query using forEach
session.createQuery( "From MyEntity", MyEntity.class ).getResultStream().forEach( i -> {
try (Stream<MyEntity> stream = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream()) {
stream.forEach( i -> {
assertTyping( MyEntity.class, i );
} );
}
Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" ).getResultStream();
try (Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" )
.getResultStream()) {
data.forEach( i -> {
assertTyping( Integer.class, i[0] );
assertTyping( String.class, i[1] );
} );
}
} );
}