# ResultSet closed while scrolling results

**URL:** <https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146>\
**Category:** Hibernate ORM\
**Created:** [March 6, 2024, 3:53pm UTC](https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146 "2024-03-06T15:53:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pela](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@pela](https://discourse.hibernate.org/u/pela)\
**Post date:** [March 6, 2024, 3:53pm UTC](https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146/1 "2024-03-06T15:53:32Z")

</div>

Hi,

We have some code pattern that used to work in Hibernate 5 but fails on Hibernate 6.  
The code iterates a ScrollableResults in the usual way by calling `next`/`get` in a while loop. Now the second call to `next` fails because the ResultSet has been closed in the meantime, even if the loop itself does nothing.

We notice it happening mainly if the entity has some entity linked to it, like in a `@OneToMany` association.

It looks like I’d need to wrap the loop within `beginTransaction`/`commit` to make it work.

Is it expected that any usage of scrollable results should now be within a transaction, or is there any other solution? Was there some change in Hibernate 6 in this respect, while in Hibernate 5 it was not required? I could not find a statement in a migration document.

Thanks!

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [March 7, 2024, 11:12am UTC](https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146/2 "2024-03-07T11:12:01Z")

</div>

Hibernate 6 has 5 different minor versions so far, so you have to be a bit more specific about the version you use. Lots of bugs were fixed, so please try again with 6.4.4.Final. If you still have a problem, please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.

---

<div class="post-metadata">

**Author:** ![pela](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@pela](https://discourse.hibernate.org/u/pela)\
**Post date:** [March 8, 2024, 10:37am UTC](https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146/3 "2024-03-08T10:37:18Z")

</div>

Sorry I wasn’t specific, this is happening with 6.4.4.Final as well. I didn’t raise a jira as I wasn’t really sure if this was a bug or rather an intended change which ended up in a different behavior in my case.

Looking more in depth, it seems this behavior actually changed somewhere along Hibernate 5 history. It worked fine on 5.4.x, stopped working later in 5.5/5.6 series, and it is not working now in 6.4.4.

I have put reproducers here [GitHub - mpela81/hibernate-test-case-templates at scrollableresult-issue](https://github.com/mpela81/hibernate-test-case-templates/tree/scrollableresult-issue). I can raise it on the issue tracker as well if helpful to keep track of it.

Thanks

---

<div class="post-metadata">

**Author:** ![pela](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@pela](https://discourse.hibernate.org/u/pela)\
**Post date:** [March 8, 2024, 1:27pm UTC](https://discourse.hibernate.org/t/resultset-closed-while-scrolling-results/9146/4 "2024-03-08T13:27:30Z")

</div>

Raised [[HHH-17826] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-17826)
