Skip to content

Hibernate: ResultSet closed when returning a stream #33177

@KyleAure

Description

@KyleAure

When using Hibernate as the Persistence provider for Jakarta Data the following error can occur:

org.hibernate.sql.exec.ExecutionException: A problem occurred in the SQL executor : Error advancing (next) ResultSet position [DSRA9110E: ResultSet is closed.]

Reason: When Hibernate returns a stream of data, the stream is backed by the JDBC ResultSet and a Spliterator. When the operation is executed outside a transaction, Hibernate closes the connection before returning the stream. Therefore, making streams unusable outside of a transaction.

Reference: https://github.com/hibernate/hibernate-orm/blob/23d12a6e1d7c02d6ff976a98773762c088833b89/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java#L219-L222

Documentation: https://jakarta.ee/specifications/persistence/3.2/apidocs/jakarta.persistence/jakarta/persistence/query#getResultStream()
Which allows implementations to have custom behavior

Resolution: Likely the best way to handle this would be to detect if we are in a transaction:

  • if so, request a stream, then return that stream
  • if not, request a list, then return the stream. (default behavior)

Testing:

  • I was able to work around this issue by surrounding the repository method call with tran.begin() and tran.commit()
  • I was unable to work around this issue by annotating the repository method call with @Transactional

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions