You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
grails-data-hibernate5: request-session connection closes after a lazy load inside withNewSession #16783
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 sessionBook.withNewSession {
Book.withTransaction {
book.author.name // lazy load through the request session borrows inner connectionnewAuthor(name: 'Tenar').save()
}
}
Book.count() // fails: connection is closed
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.
Activity
jdaugherty commentedon Oct 27, 2026
On 7.0.x today, a
SUPPORTSscope that only callswithNewSessionworks: 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. TheSUPPORTScase that fails on 7.0.x is the lazy load from the steps above, run inside aSUPPORTSscope that doesn't query the outer session. It ends the same way, withPooledConnection has already been closed.