From b2fda37773d2df15de37cd164e0021cdbfd4b13f Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 15 Aug 2023 12:39:57 +0200 Subject: [PATCH] Use try-with-resources to ensure scroll and stream tests close underlying resources --- .../cache/SQLFunctionsInterSystemsTest.java | 8 +- .../hibernate/orm/test/batch/BatchTest.java | 34 ++- .../batchfetch/EagerManyToOneStreamTest.java | 73 ++--- .../batchfetch/LazyManyToOneStreamTest.java | 69 +++-- .../lazy/proxy/FetchGraphTest.java | 69 +++-- .../connections/AggressiveReleaseTest.java | 59 ++-- .../ConnectionManagementTestCase.java | 44 +-- .../orm/test/hql/ASTParserLoadingTest.java | 46 ++- .../org/hibernate/orm/test/hql/HQLTest.java | 14 - .../hql/ScrollableCollectionFetchingTest.java | 283 +++++++++--------- ...rollableResultsObjectArrayCastingTest.java | 7 +- .../orm/test/iterate/ScrollTest.java | 25 +- .../IdentityJoinedSubclassBatchingTest.java | 44 +-- .../JoinedSubclassBatchingTest.java | 22 +- .../SequenceJoinedSubclassBatchingTest.java | 33 +- .../jpa/criteria/CriteriaCompilingTest.java | 4 +- .../CriteriaToScrollableResultsFetchTest.java | 13 +- .../internal/ProcedureCallImplTest.java | 5 +- .../results/ImplicitSelectWithJoinTests.java | 21 +- .../query/returns/ScrollableResultsTests.java | 76 ++--- .../test/readonly/ReadOnlySessionTest.java | 216 ++++++------- .../orm/test/readonly/ReadOnlyTest.java | 100 ++++--- .../ResultTransformerTest.java | 28 +- .../test/stateless/StatelessSessionTest.java | 18 +- .../StatelessSessionFetchingTest.java | 49 +-- .../orm/test/stream/basic/JpaStreamTest.java | 32 +- 26 files changed, 712 insertions(+), 680 deletions(-) diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/functional/cache/SQLFunctionsInterSystemsTest.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/functional/cache/SQLFunctionsInterSystemsTest.java index e122ea95eb..4388d7cb4b 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/functional/cache/SQLFunctionsInterSystemsTest.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/functional/cache/SQLFunctionsInterSystemsTest.java @@ -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(); - sr.next(); - sr.get(); - sr.close(); + try (ScrollableResults sr = s.createQuery( "from Simple s" ).scroll()) { + sr.next(); + sr.get(); + } s.delete( other ); s.delete( simple ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchTest.java index 9e4623cd8f..8360958e7c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchTest.java @@ -92,14 +92,15 @@ 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 ); - while ( sr.next() ) { - DataPoint dp = ( DataPoint ) sr.get(); - dp.setDescription( "done!" ); - if ( ++i % nBeforeFlush == 0 ) { - s.flush(); - s.clear(); + 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(); + dp.setDescription( "done!" ); + if ( ++i % nBeforeFlush == 0 ) { + s.flush(); + s.clear(); + } } } t.commit(); @@ -109,14 +110,15 @@ 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 ); - while ( sr.next() ) { - DataPoint dp = ( DataPoint ) sr.get(); - s.delete( dp ); - if ( ++i % nBeforeFlush == 0 ) { - s.flush(); - s.clear(); + 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(); + s.delete( dp ); + if ( ++i % nBeforeFlush == 0 ) { + s.flush(); + s.clear(); + } } } t.commit(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/EagerManyToOneStreamTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/EagerManyToOneStreamTest.java index 885038cb33..76857a868a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/EagerManyToOneStreamTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/EagerManyToOneStreamTest.java @@ -79,21 +79,22 @@ public class EagerManyToOneStreamTest { QueryImplementor query = session .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .setParameter( "someField", FIELD_VALUE ); - Stream resultStream = query.getResultStream(); + try (Stream resultStream = query.getResultStream()) { - List children = resultStream.collect( Collectors.toList() ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + List children = resultStream.collect( Collectors.toList() ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); - assertThat( children.size() ).isEqualTo( 1 ); + assertThat( children.size() ).isEqualTo( 1 ); - Parent parent = children.get( 0 ).getParent(); - assertThat( parent ).isNotNull(); + Parent parent = children.get( 0 ).getParent(); + assertThat( parent ).isNotNull(); - assertThat( Hibernate.isInitialized( parent ) ).isTrue(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + assertThat( Hibernate.isInitialized( parent ) ).isTrue(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); - assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + } } ); } @@ -106,29 +107,30 @@ public class EagerManyToOneStreamTest { session -> { QueryImplementor query = session .createQuery( "select c from Child as c ", Child.class ); - Stream resultStream = query.getResultStream(); + try (Stream resultStream = query.getResultStream()) { - List children = resultStream.collect( Collectors.toList() ); - // with Stream the association is not batch loaded - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + List children = resultStream.collect( Collectors.toList() ); + // with Stream the association is not batch loaded + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); - assertThat( children.size() ).isEqualTo( 2 ); + assertThat( children.size() ).isEqualTo( 2 ); - Parent parent = children.get( 0 ).getParent(); - assertThat( parent ).isNotNull(); - assertThat( Hibernate.isInitialized( parent ) ).isTrue(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + Parent parent = children.get( 0 ).getParent(); + assertThat( parent ).isNotNull(); + assertThat( Hibernate.isInitialized( parent ) ).isTrue(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); - assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); - Parent parent1 = children.get( 1 ).getParent(); - assertThat( parent1 ).isNotNull(); - assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + Parent parent1 = children.get( 1 ).getParent(); + assertThat( parent1 ).isNotNull(); + assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); - assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); + } } ); } @@ -140,9 +142,11 @@ public class EagerManyToOneStreamTest { QueryImplementor query = session .createQuery( "select c from Child as c", Child.class ); - query.getResultStream().forEach( - child -> assertThat( child.getParent() ).isNotNull() - ); + try (Stream resultStream = query.getResultStream()) { + resultStream.forEach( + child -> assertThat( child.getParent() ).isNotNull() + ); + } } ); } @@ -154,10 +158,11 @@ public class EagerManyToOneStreamTest { QueryImplementor query = session .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .setParameter( "someField", FIELD_VALUE ); - Stream resultStream = query.getResultStream(); - Optional child = resultStream.findFirst(); - assertThat( child.isEmpty() ).isFalse(); - assertThat( child.get().getParent() ).isNotNull(); + try (Stream resultStream = query.getResultStream()) { + Optional child = resultStream.findFirst(); + assertThat( child.isEmpty() ).isFalse(); + assertThat( child.get().getParent() ).isNotNull(); + } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/LazyManyToOneStreamTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/LazyManyToOneStreamTest.java index 419786838a..f18fc3e593 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/LazyManyToOneStreamTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batchfetch/LazyManyToOneStreamTest.java @@ -80,20 +80,21 @@ public class LazyManyToOneStreamTest { QueryImplementor query = session .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .setParameter( "someField", FIELD_VALUE ); - Stream resultStream = query.getResultStream(); + try (Stream resultStream = query.getResultStream()) { - List children = resultStream.collect( Collectors.toList() ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); - assertThat( children.size() ).isEqualTo( 1 ); + List children = resultStream.collect( Collectors.toList() ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); + assertThat( children.size() ).isEqualTo( 1 ); - Parent parent = children.get( 0 ).getParent(); - assertThat( parent ).isNotNull(); + Parent parent = children.get( 0 ).getParent(); + assertThat( parent ).isNotNull(); - assertThat( Hibernate.isInitialized( parent ) ).isFalse(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); + assertThat( Hibernate.isInitialized( parent ) ).isFalse(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); - assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + } } ); } @@ -106,29 +107,30 @@ public class LazyManyToOneStreamTest { session -> { QueryImplementor query = session .createQuery( "select c from Child as c order by id", Child.class ); - Stream resultStream = query.getResultStream(); + try (Stream resultStream = query.getResultStream()) { - List children = resultStream.collect( Collectors.toList() ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); + List children = resultStream.collect( Collectors.toList() ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); - assertThat( children.size() ).isEqualTo( 2 ); + assertThat( children.size() ).isEqualTo( 2 ); - Parent parent = children.get( 0 ).getParent(); - assertThat( parent ).isNotNull(); - assertThat( Hibernate.isInitialized( parent ) ).isFalse(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); + Parent parent = children.get( 0 ).getParent(); + assertThat( parent ).isNotNull(); + assertThat( Hibernate.isInitialized( parent ) ).isFalse(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); - assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); - Parent parent1 = children.get( 1 ).getParent(); - assertThat( parent1 ).isNotNull(); - // parent2 has been batch loaded - assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + Parent parent1 = children.get( 1 ).getParent(); + assertThat( parent1 ).isNotNull(); + // parent2 has been batch loaded + assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); - assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); - assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); + assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); + } } ); } @@ -140,15 +142,16 @@ public class LazyManyToOneStreamTest { QueryImplementor query = session .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .setParameter( "someField", FIELD_VALUE ); - Stream resultStream = query.getResultStream(); + try (Stream resultStream = query.getResultStream()) { - Optional child = resultStream.findFirst(); - assertThat( child.isEmpty() ).isFalse(); + Optional child = resultStream.findFirst(); + assertThat( child.isEmpty() ).isFalse(); - Parent parent = child.get().getParent(); - assertThat( parent ).isNotNull(); + Parent parent = child.get().getParent(); + assertThat( parent ).isNotNull(); - assertThat( Hibernate.isInitialized( parent ) ).isFalse(); + assertThat( Hibernate.isInitialized( parent ) ).isFalse(); + } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java index c34fd104c1..801c7a0812 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java @@ -138,13 +138,14 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { final String qry = "select d from D d"; final Query query = session.createQuery( qry ); - final ScrollableResults scrollableResults = getScrollableResults( query ); - while ( scrollableResults.next() ) { - final DEntity dEntity = (DEntity) scrollableResults.get(); - assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); - assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) ); - assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) ); - assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); + try (ScrollableResults scrollableResults = getScrollableResults( query )) { + while ( scrollableResults.next() ) { + final DEntity dEntity = (DEntity) scrollableResults.get(); + assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); + assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) ); + assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) ); + assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); + } } } ); @@ -231,12 +232,13 @@ 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 ); - while ( scrollableResults.next() ) { - final EEntity eEntity = (EEntity) scrollableResults.get(); - final DEntity dEntity = eEntity.getD(); - assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); - assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); + try (ScrollableResults scrollableResults = getScrollableResults( query )) { + while ( scrollableResults.next() ) { + final EEntity eEntity = (EEntity) scrollableResults.get(); + final DEntity dEntity = eEntity.getD(); + assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); + assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); + } } } ); @@ -251,14 +253,15 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { "join fetch d.g"; final Query query = session.createQuery( qry ); - final ScrollableResults scrollableResults = getScrollableResults( query ); - int i = 0; - while ( scrollableResults.next() ) { - i++; - final DEntity dEntity = (DEntity) scrollableResults.get(); - assertThat( dEntity.getBs().size(), is( 2 ) ); + try (ScrollableResults scrollableResults = getScrollableResults( query )) { + int i = 0; + while ( scrollableResults.next() ) { + i++; + final DEntity dEntity = (DEntity) scrollableResults.get(); + assertThat( dEntity.getBs().size(), is( 2 ) ); + } + assertThat( i, is( 1 ) ); } - assertThat( i, is( 1 ) ); } ); @@ -267,9 +270,10 @@ 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 ); - while ( scrollableResults.next() ) { - final Object o = scrollableResults.get(); + try (ScrollableResults scrollableResults = getScrollableResults( query )) { + while ( scrollableResults.next() ) { + final Object o = scrollableResults.get(); + } } } ); @@ -296,16 +300,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { "join fetch d.g"; final Query query = session.createQuery( qry ); - final ScrollableResults scrollableResults = getScrollableResults( query ); - int i = 0; - while ( scrollableResults.next() ) { - i++; - final Object[] result = (Object[]) scrollableResults.get(); - final DEntity dEntity = (DEntity) result[0]; - assertThat( dEntity.getBs().size(), is( 2 ) ); - assertThat( result[1], is( "bla" ) ); + try (ScrollableResults scrollableResults = getScrollableResults( query )) { + int i = 0; + while ( scrollableResults.next() ) { + i++; + final Object[] result = (Object[]) scrollableResults.get(); + final DEntity dEntity = (DEntity) result[0]; + assertThat( dEntity.getBs().size(), is( 2 ) ); + assertThat( result[1], is( "bla" ) ); + } + assertThat( i, is( 1 ) ); } - assertThat( i, is( 1 ) ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/AggressiveReleaseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/AggressiveReleaseTest.java index 43d595206e..1a91c1fe20 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/AggressiveReleaseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/AggressiveReleaseTest.java @@ -111,24 +111,24 @@ 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(); - sr.next(); + 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" ); + try { + SerializationHelper.serialize( s ); + fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" ); + } + catch (IllegalStateException e) { + // expected behavior + } + + // getting the first row only because SybaseASE15Dialect throws NullPointerException + // if data is not read before closing the ResultSet + sr.next(); + + // Closing the ScrollableResults does currently force batching to + // aggressively release the connection } - catch( IllegalStateException e ) { - // expected behavior - } - - // getting the first row only because SybaseASE15Dialect throws NullPointerException - // if data is not read before closing the ResultSet - sr.next(); - - // 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(); - assertTrue( sr.next() ); - Silly silly2 = ( Silly ) sr.get(); - assertEquals( silly, silly2 ); - sr.close(); + try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) { + assertTrue( sr.next() ); + Silly silly2 = (Silly) sr.get(); + assertEquals( silly, silly2 ); + } - 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(); + 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() ); + } s.delete( silly ); s.flush(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java index 80aa8fee59..3ec70425fa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java @@ -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,19 +135,21 @@ 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 ); + try { + SerializationHelper.serialize( sessionUnderTest ); - fail( "Serialization of connected session allowed!" ); - } - catch( IllegalStateException e ) { - // expected behaviour - } - finally { - release( sessionUnderTest ); - done(); + fail( "Serialization of connected session allowed!" ); + } + catch (IllegalStateException e) { + // expected behaviour + } + finally { + release( sessionUnderTest ); + done(); + } } } @@ -247,18 +250,19 @@ 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 ); - checkSerializedState( sessionUnderTest ); + disconnect( sessionUnderTest ); + SerializationHelper.serialize( sessionUnderTest ); + checkSerializedState( sessionUnderTest ); - reconnect( sessionUnderTest ); - sessionUnderTest.delete( silly ); - sessionUnderTest.flush(); + reconnect( sessionUnderTest ); + sessionUnderTest.delete( silly ); + sessionUnderTest.flush(); - release( sessionUnderTest ); - done(); + release( sessionUnderTest ); + done(); + } } /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ASTParserLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ASTParserLoadingTest.java index fb0bc857a6..fd90ee916a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ASTParserLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ASTParserLoadingTest.java @@ -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(); - 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(); + 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 = 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(); + 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 ); + } // 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(); - - assertTrue( "Incorrect result size", sr.next() ); - assertTrue( "Incorrect return type", sr.get() instanceof Animal ); - assertFalse( session.contains( sr.get() ) ); - sr.close(); + 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() ) ); + } 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(); - - assertTrue( "Incorrect result size", sr.next() ); - assertTrue( "Incorrect return type", sr.get() instanceof Map ); - sr.close(); + 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 ); + } 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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/HQLTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/HQLTest.java index 19516462d6..26e7c4ce22 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/HQLTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/HQLTest.java @@ -958,20 +958,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase { .collect(Collectors.toList()); } //end::jpql-api-stream-example[] - - // tag::jpql-api-stream-terminal-operation[] - List 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[] }); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ScrollableCollectionFetchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ScrollableCollectionFetchingTest.java index 62659011ee..94283b5fc8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ScrollableCollectionFetchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/ScrollableCollectionFetchingTest.java @@ -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(); - assertTrue( results.next() ); - Object[] result = (Object[]) results.get(); - assertTrue(Hibernate.isInitialized(((Animal) result[0]).getOffspring())); - session.createMutationQuery("delete Mammal").executeUpdate(); + 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(); + } } ); } @@ -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,55 +108,56 @@ 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() ); - - assertFalse( results.next() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - - assertFalse( results.previous() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - - results.beforeFirst(); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.next() ); - - assertFalse( results.first() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.next() ); - - results.afterLast(); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.next() ); - - assertFalse( results.last() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.next() ); - - for ( int i = 1; i < 3; i++ ) { - assertFalse( results.scroll( i ) ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - assertFalse( results.scroll( -i ) ); + assertFalse( results.next() ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - assertFalse( results.setRowNumber( i ) ); + assertFalse( results.previous() ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - assertFalse( results.setRowNumber( -i ) ); + results.beforeFirst(); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); + assertFalse( results.next() ); + + assertFalse( results.first() ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.next() ); + + results.afterLast(); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.next() ); + + assertFalse( results.last() ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.next() ); + + for ( int i = 1; i < 3; i++ ) { + assertFalse( results.scroll( i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + + assertFalse( results.scroll( -i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + + assertFalse( results.setRowNumber( i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + + assertFalse( results.setRowNumber( -i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + } } } ); @@ -190,84 +193,85 @@ 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() ); - assertFalse( results.previous() ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.previous() ); - assertTrue( results.next() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - - assertFalse( results.next() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - - assertTrue( results.previous() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - - assertFalse( results.previous() ); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - - assertTrue( results.next() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - - results.beforeFirst(); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.previous() ); - - assertTrue( results.first() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - assertFalse( results.next() ); - - results.afterLast(); - assertFalse( results.isFirst() ); - assertFalse( results.isLast() ); - assertFalse( results.next() ); - - assertTrue( results.last() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - assertFalse( results.next() ); - - assertTrue( results.first() ); - assertTrue( results.isFirst() ); - assertTrue( results.isLast() ); - - for ( int i = 1; i < 3; i++ ) { - assertTrue( results.setRowNumber( 1 ) ); + assertTrue( results.next() ); assertTrue( results.isFirst() ); assertTrue( results.isLast() ); - assertFalse( results.scroll( i ) ); + assertFalse( results.next() ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - assertTrue( results.setRowNumber( 1 ) ); + assertTrue( results.previous() ); assertTrue( results.isFirst() ); assertTrue( results.isLast() ); - assertFalse( results.scroll( -i ) ); + assertFalse( results.previous() ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - if ( i != 1 ) { - assertFalse( results.setRowNumber( i ) ); + assertTrue( results.next() ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + + results.beforeFirst(); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.previous() ); + + assertTrue( results.first() ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + assertFalse( results.next() ); + + results.afterLast(); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + assertFalse( results.next() ); + + assertTrue( results.last() ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + assertFalse( results.next() ); + + assertTrue( results.first() ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + + for ( int i = 1; i < 3; i++ ) { + assertTrue( results.setRowNumber( 1 ) ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + + assertFalse( results.scroll( i ) ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); - assertFalse( results.setRowNumber( -i ) ); + assertTrue( results.setRowNumber( 1 ) ); + assertTrue( results.isFirst() ); + assertTrue( results.isLast() ); + + assertFalse( results.scroll( -i ) ); assertFalse( results.isFirst() ); assertFalse( results.isLast() ); + + if ( i != 1 ) { + assertFalse( results.setRowNumber( i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + + assertFalse( results.setRowNumber( -i ) ); + assertFalse( results.isFirst() ); + assertFalse( results.isLast() ); + } } } } @@ -292,20 +296,20 @@ 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() ) { - counter++; - Animal animal = (Animal) results.get(); - checkResult( animal ); + int counter = 0; + while ( results.next() ) { + counter++; + Animal animal = (Animal) results.get(); + checkResult( animal ); + } + assertEquals( 2, counter, "unexpected result count" ); } - assertEquals( 2, counter, "unexpected result count" ); - } ); } @@ -319,21 +323,21 @@ 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(); + results.afterLast(); - int counter = 0; - while ( results.previous() ) { - counter++; - Animal animal = (Animal) results.get(); - checkResult( animal ); + int counter = 0; + while ( results.previous() ) { + counter++; + Animal animal = (Animal) results.get(); + checkResult( animal ); + } + assertEquals( 2, counter, "unexpected result count" ); } - assertEquals( 2, counter, "unexpected result count" ); - } ); } @@ -347,31 +351,32 @@ 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(); - assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" ); + results.first(); + Animal animal = (Animal) results.get(); + assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" ); - results.scroll( 1 ); - animal = (Animal) results.get(); - assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" ); + results.scroll( 1 ); + animal = (Animal) results.get(); + assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" ); - results.scroll( -1 ); - animal = (Animal) results.get(); - assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" ); + results.scroll( -1 ); + animal = (Animal) results.get(); + assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" ); - results.setRowNumber( 1 ); - animal = (Animal) results.get(); - assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" ); + results.setRowNumber( 1 ); + animal = (Animal) results.get(); + assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" ); - results.setRowNumber( 2 ); - animal = (Animal) results.get(); - assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" ); + results.setRowNumber( 2 ); + animal = (Animal) results.get(); + assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" ); + } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/internal/util/ScrollableResultsObjectArrayCastingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/internal/util/ScrollableResultsObjectArrayCastingTest.java index 0ba06def79..31409ea73c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/internal/util/ScrollableResultsObjectArrayCastingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/internal/util/ScrollableResultsObjectArrayCastingTest.java @@ -55,9 +55,10 @@ public class ScrollableResultsObjectArrayCastingTest { scope.inTransaction( (entityManager) -> { TypedQuery typedQuery = entityManager.createQuery( "select p.binaryValue from Product p", byte[].class ); - Stream stream = typedQuery.getResultStream(); - //noinspection ResultOfMethodCallIgnored - stream.findFirst(); + try (Stream stream = typedQuery.getResultStream()) { + //noinspection ResultOfMethodCallIgnored + stream.findFirst(); + } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ScrollTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ScrollTest.java index 801e33c7ff..a6a844d1ad 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ScrollTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ScrollTest.java @@ -51,18 +51,19 @@ public class ScrollTest { scope.inTransaction( s -> { - ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll(); - assertTrue( sr.next() ); - Item i1 = (Item) sr.get(); - assertTrue( sr.next() ); - Item i2 = (Item) sr.get(); - assertTrue( Hibernate.isInitialized( i1 ) ); - assertTrue( Hibernate.isInitialized( i2 ) ); - assertThat( i1.getName(), is( "foo" ) ); - assertThat( i2.getName(), is( "bar" ) ); - assertFalse( sr.next() ); - s.delete( i1 ); - s.delete( i2 ); + try (ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll()) { + assertTrue( sr.next() ); + Item i1 = (Item) sr.get(); + assertTrue( sr.next() ); + Item i2 = (Item) sr.get(); + assertTrue( Hibernate.isInitialized( i1 ) ); + assertTrue( Hibernate.isInitialized( i2 ) ); + assertThat( i1.getName(), is( "foo" ) ); + assertThat( i2.getName(), is( "bar" ) ); + assertFalse( sr.next() ); + s.delete( i1 ); + s.delete( i2 ); + } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/IdentityJoinedSubclassBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/IdentityJoinedSubclassBatchingTest.java index 91e1b70577..cdd68e0bab 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/IdentityJoinedSubclassBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/IdentityJoinedSubclassBatchingTest.java @@ -113,24 +113,26 @@ 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" ); + 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); } @@ -158,13 +160,14 @@ 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); @@ -191,13 +194,14 @@ 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/JoinedSubclassBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/JoinedSubclassBatchingTest.java index 53df385a15..1cace2ad69 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/JoinedSubclassBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/JoinedSubclassBatchingTest.java @@ -106,24 +106,26 @@ 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" ); + 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/SequenceJoinedSubclassBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/SequenceJoinedSubclassBatchingTest.java index 688b156188..2aba25489e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/SequenceJoinedSubclassBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclassbatch/SequenceJoinedSubclassBatchingTest.java @@ -111,24 +111,26 @@ 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" ); + 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); } @@ -153,13 +155,14 @@ 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 ); + while ( sr.next() ) { + Employee e = (Employee) sr.get(); + s.delete( e ); + } } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaCompilingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaCompilingTest.java index 835c73005b..bff46d479d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaCompilingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaCompilingTest.java @@ -218,9 +218,9 @@ public class CriteriaCompilingTest extends BaseEntityManagerFunctionalTestCase { TypedQuery 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 anotherQuery = session.createQuery( "select o from Order o where totalPrice in :totalPrices", diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/fetchscroll/CriteriaToScrollableResultsFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/fetchscroll/CriteriaToScrollableResultsFetchTest.java index bbd32acf36..940773a943 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/fetchscroll/CriteriaToScrollableResultsFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/fetchscroll/CriteriaToScrollableResultsFetchTest.java @@ -85,12 +85,13 @@ public class CriteriaToScrollableResultsFetchTest extends BaseEntityManagerFunct hibernateQuery.setCacheable( false ); List lines = new ArrayList<>(); - ScrollableResults scrollableResults = hibernateQuery.scroll(); - scrollableResults.last(); - int rows = scrollableResults.getRowNumber() + 1; - scrollableResults.beforeFirst(); - while ( scrollableResults.next() ) { - lines.add( (OrderLine) scrollableResults.get( ) ); + try (ScrollableResults scrollableResults = hibernateQuery.scroll()) { + scrollableResults.last(); + int rows = scrollableResults.getRowNumber() + 1; + scrollableResults.beforeFirst(); + while ( scrollableResults.next() ) { + lines.add( (OrderLine) scrollableResults.get() ); + } } assertNotNull( lines ); assertEquals( "Expected one order line", 1, lines.size() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/internal/ProcedureCallImplTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/internal/ProcedureCallImplTest.java index c5311be3f5..9e2ecd3b7a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/internal/ProcedureCallImplTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/internal/ProcedureCallImplTest.java @@ -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() ); + } } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/results/ImplicitSelectWithJoinTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/results/ImplicitSelectWithJoinTests.java index 0e684ab7b2..f73cda4ce4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/results/ImplicitSelectWithJoinTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/results/ImplicitSelectWithJoinTests.java @@ -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 results = query.scroll(); + try (ScrollableResults 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 results = query.scroll(); + try (ScrollableResults 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 results = query.scroll(); + try (final ScrollableResults 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 results = query.scroll(); + try (ScrollableResults 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 results = query.scroll(); + try (ScrollableResults results = query.scroll()) { assertThat( results.next() ).isTrue(); final Product result = results.get(); assertThat( result ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/returns/ScrollableResultsTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/returns/ScrollableResultsTests.java index 14b99d4346..d4a66a98ce 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/returns/ScrollableResultsTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/returns/ScrollableResultsTests.java @@ -68,54 +68,54 @@ public class ScrollableResultsTests { scope.inTransaction( session -> { final QueryImplementor query = session.createQuery( SINGLE_SELECTION_QUERY, String.class ); - final ScrollableResultsImplementor results = query.scroll( ScrollMode.SCROLL_INSENSITIVE ); + try (ScrollableResultsImplementor results = query.scroll( ScrollMode.SCROLL_INSENSITIVE )) { - // try to initially read in reverse - should be false - assertThat( results.previous(), is( false ) ); + // try to initially read in reverse - should be false + assertThat( results.previous(), is( false ) ); - // position at the first row - assertThat( results.next(), is( true ) ); - String data = results.get(); - assertThat( data, is( "other" ) ); + // position at the first row + assertThat( results.next(), is( true ) ); + String data = results.get(); + assertThat( data, is( "other" ) ); - // position at the second (last) row - assertThat( results.next(), is( true ) ); - data = results.get(); - assertThat( data, is( "value" ) ); + // position at the second (last) row + assertThat( results.next(), is( true ) ); + data = results.get(); + assertThat( data, is( "value" ) ); - // position after the second (last) row - assertThat( results.next(), is( false ) ); + // position after the second (last) row + assertThat( results.next(), is( false ) ); - // position back to the second row - assertThat( results.previous(), is( true ) ); - data = results.get(); - assertThat( data, is( "value" ) ); + // position back to the second row + assertThat( results.previous(), is( true ) ); + data = results.get(); + assertThat( data, is( "value" ) ); - // position back to the first row - assertThat( results.previous(), is( true ) ); - data = results.get(); - assertThat( data, is( "other" ) ); + // position back to the first row + assertThat( results.previous(), is( true ) ); + data = results.get(); + assertThat( data, is( "other" ) ); - // position before the first row - assertThat( results.previous(), is( false ) ); - assertThat( results.previous(), is( false ) ); + // position before the first row + assertThat( results.previous(), is( false ) ); + assertThat( results.previous(), is( false ) ); - assertThat( results.last(), is( true ) ); - data = results.get(); - assertThat( data, is( "value" ) ); + assertThat( results.last(), is( true ) ); + data = results.get(); + assertThat( data, is( "value" ) ); - assertThat( results.first(), is( true ) ); - data = results.get(); - assertThat( data, is( "other" ) ); + assertThat( results.first(), is( true ) ); + data = results.get(); + assertThat( data, is( "other" ) ); - assertThat( results.scroll( 1 ), is( true ) ); - data = results.get(); - assertThat( data, is( "value" ) ); - - assertThat( results.scroll( -1 ), is( true ) ); - data = results.get(); - assertThat( data, is( "other" ) ); + assertThat( results.scroll( 1 ), is( true ) ); + data = results.get(); + assertThat( data, is( "value" ) ); + assertThat( results.scroll( -1 ), is( true ) ); + data = results.get(); + assertThat( data, is( "other" ) ); + } } ); } @@ -262,7 +262,7 @@ public class ScrollableResultsTests { } private static void verifyScroll(Query query, Consumer validator) { - try ( final ScrollableResults results = query.scroll( ScrollMode.FORWARD_ONLY ) ) { + try (final ScrollableResults results = query.scroll( ScrollMode.FORWARD_ONLY ) ) { assertThat( results.next(), is( true ) ); validator.accept( results.get() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionTest.java index de51a1fdf0..59897eba3f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlySessionTest.java @@ -109,15 +109,16 @@ 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 ); - s.setDefaultReadOnly( false ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); + 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(); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -148,15 +149,16 @@ 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 ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if ( ++i == 50 ) { - s.setReadOnly( dp, true ); + .scroll( ScrollMode.FORWARD_ONLY )) { + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); + } + dp.setDescription( "done!" ); } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -187,15 +189,16 @@ 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 ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); + .scroll( ScrollMode.FORWARD_ONLY )) { + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -228,14 +231,15 @@ 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 ); - s.setDefaultReadOnly( false ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); + try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) { + s.setDefaultReadOnly( false ); + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -287,32 +291,33 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { assertTrue( query.isReadOnly() ); s.setDefaultReadOnly( false ); assertFalse( s.isDefaultReadOnly() ); - 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() ) { + try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) { assertFalse( s.isDefaultReadOnly() ); - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertFalse( s.isReadOnly( dp ) ); + assertTrue( query.isReadOnly() ); + DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); + assertFalse( s.isReadOnly( dpLast ) ); + query.setReadOnly( false ); + assertFalse( query.isReadOnly() ); + assertFalse( s.isDefaultReadOnly() ); + while ( sr.next() ) { + assertFalse( s.isDefaultReadOnly() ); + dp = (DataPoint) sr.get(); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertFalse( s.isReadOnly( dp ) ); + } + else { + assertTrue( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + nExpectedChanges = ( dp == dpLast ? 1 : 2 ); + } + dp.setDescription( "done!" ); } - else { - assertTrue( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); - nExpectedChanges = ( dp == dpLast ? 1 : 2 ); - } - dp.setDescription( "done!" ); + assertFalse( s.isDefaultReadOnly() ); } - assertFalse( s.isDefaultReadOnly() ); t.commit(); s.clear(); t = s.beginTransaction(); @@ -363,31 +368,32 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { assertFalse( query.isReadOnly() ); s.setDefaultReadOnly( true ); assertTrue( s.isDefaultReadOnly() ); - 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() ) { + 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() ); assertTrue( s.isDefaultReadOnly() ); - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertTrue( s.isReadOnly( dp ) ); + while ( sr.next() ) { + assertTrue( s.isDefaultReadOnly() ); + dp = (DataPoint) sr.get(); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertTrue( s.isReadOnly( dp ) ); + } + else { + assertFalse( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); + nExpectedChanges = ( dp == dpLast ? 99 : 98 ); + } + dp.setDescription( "done!" ); } - else { - assertFalse( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, true ); - nExpectedChanges = ( dp == dpLast ? 99 : 98 ); - } - dp.setDescription( "done!" ); + assertTrue( s.isDefaultReadOnly() ); } - assertTrue( s.isDefaultReadOnly() ); t.commit(); s.clear(); t = s.beginTransaction(); @@ -690,24 +696,25 @@ 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; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertFalse( s.isReadOnly( dp ) ); + 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() ) { + //dpLast existed in the session before executing the read-only query + assertFalse( s.isReadOnly( dp ) ); + } + else { + assertTrue( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + nExpectedChanges = ( dp == dpLast ? 1 : 2 ); + } + dp.setDescription( "done!" ); } - else { - assertTrue( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); - nExpectedChanges = ( dp == dpLast ? 1 : 2 ); - } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -741,24 +748,25 @@ 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; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertTrue( s.isReadOnly( dp ) ); + 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() ) { + //dpLast existed in the session before executing the read-only query + assertTrue( s.isReadOnly( dp ) ); + } + else { + assertFalse( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); + nExpectedChanges = ( dp == dpLast ? 99 : 98 ); + } + dp.setDescription( "done!" ); } - else { - assertFalse( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, true ); - nExpectedChanges = ( dp == dpLast ? 99 : 98 ); - } - dp.setDescription( "done!" ); } t.commit(); s.clear(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java index 49c116891c..26961e2d48 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyTest.java @@ -115,15 +115,16 @@ 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 ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - if ( ++i == 50 ) { - session.setReadOnly( dp, false ); + .scroll( ScrollMode.FORWARD_ONLY )) { + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + if ( ++i == 50 ) { + session.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); } - dp.setDescription( "done!" ); } session.getTransaction().commit(); @@ -172,18 +173,19 @@ 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 ); - clearCounts( scope ); + assertInsertCount( 100, scope ); + assertUpdateCount( 0, scope ); + clearCounts( scope ); - while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(); - assertFalse( session.isReadOnly( dp ) ); - session.delete( dp ); + while ( sr.next() ) { + DataPoint dp = (DataPoint) sr.get(); + assertFalse( session.isReadOnly( dp ) ); + session.delete( dp ); + } } } ); @@ -462,24 +464,25 @@ 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; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertFalse( s.isReadOnly( dp ) ); + 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() ) { + //dpLast existed in the session before executing the read-only query + assertFalse( s.isReadOnly( dp ) ); + } + else { + assertTrue( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + nExpectedChanges = ( dp == dpLast ? 1 : 2 ); + } + dp.setDescription( "done!" ); } - else { - assertTrue( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, false ); - nExpectedChanges = ( dp == dpLast ? 1 : 2 ); - } - dp.setDescription( "done!" ); } t.commit(); s.clear(); @@ -529,24 +532,25 @@ 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; - while ( sr.next() ) { - dp = (DataPoint) sr.get(); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertTrue( s.isReadOnly( dp ) ); + 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() ) { + //dpLast existed in the session before executing the read-only query + assertTrue( s.isReadOnly( dp ) ); + } + else { + assertFalse( s.isReadOnly( dp ) ); + } + if ( ++i == 50 ) { + s.setReadOnly( dp, true ); + nExpectedChanges = ( dp == dpLast ? 99 : 98 ); + } + dp.setDescription( "done!" ); } - else { - assertFalse( s.isReadOnly( dp ) ); - } - if ( ++i == 50 ) { - s.setReadOnly( dp, true ); - nExpectedChanges = ( dp == dpLast ? 99 : 98 ); - } - dp.setDescription( "done!" ); } t.commit(); s.clear(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/resulttransformer/ResultTransformerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/resulttransformer/ResultTransformerTest.java index cefa292dff..e886086166 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/resulttransformer/ResultTransformerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/resulttransformer/ResultTransformerTest.java @@ -60,21 +60,21 @@ public class ResultTransformerTest { } ); - 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 ) { - sr.next(); - } - else { - sr.first(); - } - - Object obj = sr.get(); - assertTrue(obj instanceof PartnerA); - PartnerA obj2 = (PartnerA) obj; - assertEquals("Partner A", obj2.getName()); + 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 ) { + sr.next(); + } + else { + sr.first(); + } + Object obj = sr.get(); + assertTrue( obj instanceof PartnerA ); + PartnerA obj2 = (PartnerA) obj; + assertEquals( "Partner A", obj2.getName() ); + } } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java index 1d32f5a0f2..b86813a830 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java @@ -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 ); - sr.next(); - doc2 = (Document) sr.get(); - sr.close(); + try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" ) + .scroll( ScrollMode.FORWARD_ONLY )) { + sr.next(); + doc2 = (Document) sr.get(); + } 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 ); - sr.next(); - doc2 = (Document) sr.get(); - sr.close(); + try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) { + sr.next(); + doc2 = (Document) sr.get(); + } assertEquals( "Blahs", doc2.getName() ); assertEquals( doc.getText(), doc2.getText() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/fetching/StatelessSessionFetchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/fetching/StatelessSessionFetchingTest.java index 96c0d68134..86ee6f363d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/fetching/StatelessSessionFetchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/fetching/StatelessSessionFetchingTest.java @@ -105,13 +105,14 @@ 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 ); - while ( scrollableResults.next() ) { - Task taskRef = (Task) scrollableResults.get(); - assertTrue( Hibernate.isInitialized( taskRef ) ); - assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); - assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); - assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); + try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) { + while ( scrollableResults.next() ) { + Task taskRef = (Task) scrollableResults.get(); + assertTrue( Hibernate.isInitialized( taskRef ) ); + assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); + assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); + assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); + } } } ); @@ -148,13 +149,14 @@ 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 ); - while ( scrollableResults.next() ) { - Task taskRef = (Task) scrollableResults.get(); - assertTrue( Hibernate.isInitialized( taskRef ) ); - assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); - assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); - assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); + try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) { + while ( scrollableResults.next() ) { + Task taskRef = (Task) scrollableResults.get(); + assertTrue( Hibernate.isInitialized( taskRef ) ); + assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); + assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); + assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); + } } } ); @@ -189,21 +191,22 @@ 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(); - assertTrue( Hibernate.isInitialized( producer ) ); - assertTrue( Hibernate.isInitialized( producer.getProducts() ) ); + while ( scrollableResults.next() ) { + Producer producer = (Producer) scrollableResults.get(); + assertTrue( Hibernate.isInitialized( producer ) ); + assertTrue( Hibernate.isInitialized( producer.getProducts() ) ); - for ( Product product : producer.getProducts() ) { - assertTrue( Hibernate.isInitialized( product ) ); - assertFalse( Hibernate.isInitialized( product.getVendor() ) ); + for ( Product product : producer.getProducts() ) { + assertTrue( Hibernate.isInitialized( product ) ); + assertFalse( Hibernate.isInitialized( product.getVendor() ) ); + } } } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stream/basic/JpaStreamTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/basic/JpaStreamTest.java index 9aba33120f..67a38f4c46 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/stream/basic/JpaStreamTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/basic/JpaStreamTest.java @@ -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 -> { - assertTyping( MyEntity.class, i ); - } ); + try (Stream stream = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream()) { + stream.forEach( i -> { + assertTyping( MyEntity.class, i ); + } ); + } - Stream 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] ); - } ); + try (Stream 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] ); + } ); + } } ); }