Skip to content

grails-data-hibernate5: request-session connection closes after a lazy load inside withNewSession #16783

Open

Description

@borinquenkid

Expected Behavior

In a Grails 7 application with Hibernate 5, after withNewSession { withTransaction { … } } completes, the OSIV-bound request session should remain usable for subsequent queries and operations.

Actual Behaviour

When an initial transaction ends on the request session, Spring disconnects the session's physical JDBC connection. If the application subsequently calls withNewSession { withTransaction { ... } } and touches a lazy association of an entity loaded by the outer request session, the outer session requests a connection from the DataSource and borrows the inner transaction's thread-bound connection.

When the inner transaction completes, it closes that connection, leaving the outer session holding a closed connection. Subsequent operations on the outer session fail with:

org.springframework.dao.DataAccessException: ... PooledConnection has already been closed

Additionally, under PROPAGATION_SUPPORTS transaction scopes containing nested withNewSession invocations without an in-scope query on the outer session, connection synchronization can close the outer session connection upon scope completion.

Steps To Reproduce

Book book = Book.first()
Author.withTransaction { Author.count() } // ends transaction, disconnects request session

Book.withNewSession {
    Book.withTransaction {
        book.author.name // lazy load through the request session borrows inner connection
        new Author(name: 'Tenar').save()
    }
}
Book.count() // fails: connection is closed

Environment Information

  • Grails 7.0.x
  • Hibernate ORM 5.6
  • Spring Framework 6.2
  • Groovy 4.0
  • JDK 17 / 21

Activity

  1. jdaugherty commented on Oct 27, 2026

    @jdaugherty
    Contributor

    On 7.0.x today, a SUPPORTS scope that only calls withNewSession works: the outer session is still usable after the scope. That case broke only in an intermediate commit of #16763, so it belongs in the H5 tests as a regression check, not as a reproduction. The SUPPORTS case that fails on 7.0.x is the lazy load from the steps above, run inside a SUPPORTS scope that doesn't query the outer session. It ends the same way, with PooledConnection has already been closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions