spring-boothibernatescrollableresults

Hibernate ScrollableResult not working on Springboot 3.2


In a springboot application i'm using a scrollable result to make a query, in this way

Session session = entityManager.unwrap(Session.class); org.hibernate.query.Query query = buildParameterizedQuery(); /*this is some method that build a query*/ ScrollableResults result = query.setReadOnly(true).setCacheable(false).scroll(ScrollMode.FORWARD_ONLY);

This is working fine on springboot 3.1. I tried to migrate to springboot 3.2. Then my aplication crashed. When i called the first time result.next(); an exception is raised

 java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@4e212c5c is closed
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:142) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:54) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:153) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:183) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:155) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.exec.spi.JdbcSelectExecutor.lambda$scroll$1(JdbcSelectExecutor.java:102) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:231) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:167) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:218) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:98) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:19) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:66) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]
    at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:51) ~[hibernate-core-6.4.2.Final.jar!/:6.4.2.Final]

I tried to modify the query in many ways, adding a transaction, or a lockmode, but nothing worked. For some way, someone is closing the connection before I started to scroll my results.

Can someone help me?


Solution

  • We had same Problem. After many debug ant tests, we figured out, that our EntityManager was not joined to Transaction.

    em.isJoinedToTransaction() == false
    

    Until to version Spring-Boot 3.1.6 despite that ScrollableResults worked. But since 3.1.7 Hibernate Session was closed. After we properly configured @Transactional attribute, ScrollableResults works. I hope it helps