[hibernate-issues] [JIRA] (HHH-14138) ScrollableResults closed unexpectedly when performing another query inside scroll loop

Jamie Strachan (JIRA) jira at hibernate.atlassian.net
Tue Aug 4 13:41:29 EDT 2020


Jamie Strachan ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f29960d5227f50014d306b2 ) *updated* an issue

Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HHH-14138?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 ) HHH-14138 ( https://hibernate.atlassian.net/browse/HHH-14138?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 ) ScrollableResults closed unexpectedly when performing another query inside scroll loop ( https://hibernate.atlassian.net/browse/HHH-14138?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 )

Change By: Jamie Strachan ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f29960d5227f50014d306b2 )

Using a second query while scrolling causes the ScrollableResults to be closed.

Minimal example:

{code:java}
public static void main(String[] args)
{
EntityManager em = getEntityManager();
// Query to be used while scrolling. Can be Native or not,
// it doesn't seem to matter what the query is. ( I just wanted to keep it simple)
Query qExtra = em.createNativeQuery("SELECT 1");

Query q = em.createQuery("SELECT row FROM User row");
q.setMaxResults(10);

@SuppressWarnings("rawtypes")
org.hibernate.query.Query hibernateQuery = q.unwrap(org.hibernate.query.Query.class);

ScrollableResults sr = hibernateQuery.scroll();

while(sr.next())
{
// Comment this query out and function finishes normally
qExtra.getSingleResult();
}

em.close();
}
{code}

Perhaps I am not supposed to unwrap the JPA2 query like that?
WIth Hibernate 5.1.0 and the following code to obtain a ScrollableResults, the example code works as expected:

{code:java}
org.hibernate.Query query = ((org.hibernate.jpa.internal.QueryImpl)q).getHibernateQuery();
ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
{code}

However, running 5.4.18, it does not appear to matter what query I issue inside the loop, it will close the result set.

The resulting exception from 5.4.18:

{noformat}
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not advance using next()
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.internal.ScrollableResultsImpl.convert(ScrollableResultsImpl.java:70)
at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:105)
at Test.main(Test.java:???)
Caused by: java.sql.SQLException: You can't operate on a closed ResultSet!!!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:691)
at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:100)
... 1 more
Caused by: java.lang.NullPointerException
at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:685)
... 2 more
{noformat}

Thanks for any help \ !

Addendum:
I have tested the following code using the JPA2 getResultStream(), and it also produces the same exception:

\{code:java}

{noformat}public static void main(String[] args)
{
EntityManager em = EntityManagerSingleton.getEntityManager();
Query qExtra = em.createNativeQuery("SELECT 1");
Query q = em.createQuery("SELECT row FROM User row");

q.getResultStream().forEach(row -> { qExtra.getSingleResult(); });
em.close();
}{noformat}

\{code}

The exception from using getResultStream():

\{noformat}

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not advance using next()

at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)

at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)

at org.hibernate.internal.ScrollableResultsImpl.convert(ScrollableResultsImpl.java:70)

at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:105)

at org.hibernate.query.internal.ScrollableResultsIterator.hasNext(ScrollableResultsIterator.java:33)

at java.util.Iterator.forEachRemaining(Iterator.java:115)

at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)

at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)

at org.hibernate.query.spi.StreamDecorator.forEach(StreamDecorator.java:155)

at Tester.main(Tester.java)

Caused by: java.sql.SQLException: You can't operate on a closed ResultSet\!\!\!

at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)

at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)

at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:691)

at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:100)

... 6 more

Caused by: java.lang.NullPointerException

at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:685)

... 7 more

\{noformat}

( https://hibernate.atlassian.net/browse/HHH-14138#add-comment?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-14138#add-comment?atlOrigin=eyJpIjoiYmNkMDhiMDQ1Mjg2NDEzOTg2OTkzNTliMTM3MTk5ZTkiLCJwIjoiaiJ9 )

Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100141- sha1:8f92423 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200804/9c35ab3c/attachment.html 


More information about the hibernate-issues mailing list