# FetchingScrollableResultsImpl.last infinitely looping

**URL:** <https://discourse.hibernate.org/t/fetchingscrollableresultsimpl-last-infinitely-looping/9713>\
**Category:** Hibernate ORM\
**Created:** [June 7, 2024, 7:21pm UTC](https://discourse.hibernate.org/t/fetchingscrollableresultsimpl-last-infinitely-looping/9713 "2024-06-07T19:21:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EB55](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@EB55](https://discourse.hibernate.org/u/EB55)\
**Post date:** [June 7, 2024, 7:21pm UTC](https://discourse.hibernate.org/t/fetchingscrollableresultsimpl-last-infinitely-looping/9713/1 "2024-06-07T19:21:09Z")

</div>

I’m upgrading code that worked in Hibernate 5 to hibernate 6, and found that one of our queries results in an infinitely looping call - locking up the SQL table.

The querystring contains a “fetch” keyword, and we use query.Scroll() to get a scrollableResults (allegedly for performance purposes?) - we then immediately call scrollableResults.last() - which never completes nor crashes.

By calling query.list() I was able to determine that the result list is empty (which is expected in this case)

I debugged the FetchingScrollableResultsImpl.last() call, and saw that it loops infinitely within these lines:

```java
while ( !afterLast ) {
    more = next();
}

```

Curiously, a few lines before this there’s a check for ‘isResultSetEmpty()’ that returns true, despite the previously mentioned query.list() showing there are no results.

I’ve tried this on 6.4.0 and 6.5.0, same issue.

---

<div class="post-metadata">

**Author:** ![EB55](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@EB55](https://discourse.hibernate.org/u/EB55)\
**Post date:** [June 10, 2024, 3:27pm UTC](https://discourse.hibernate.org/t/fetchingscrollableresultsimpl-last-infinitely-looping/9713/2 "2024-06-10T15:27:57Z")

</div>

> [@EB55](#):
>
> Curiously, a few lines before this there’s a check for ‘isResultSetEmpty()’ that returns true, despite the previously mentioned query.list() showing there are no results.

This should say “Curiously, a few lines before this there’s a check for ‘isResultSetEmpty()’ that returns **false** , despite the previously mentioned query.list() showing there are no results.”

---

<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:** [June 11, 2024, 11:13pm UTC](https://discourse.hibernate.org/t/fetchingscrollableresultsimpl-last-infinitely-looping/9713/3 "2024-06-11T23:13:08Z")

</div>

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.
