Repository navigation
Conversation
…t completes The open session in view interceptor closed the sessions it opened as soon as a request went asynchronous. A task that the action returned then failed to lazily load an association of an entity the action had loaded: with a LazyInitializationException, or with a NullPointerException from Hibernate when it got there while the session was being closed. The sessions are still taken off the thread when the request goes asynchronous, so that the thread's next request does not use them. They are now closed when the asynchronous request completes, from an AsyncListener on the request's asynchronous context, which the container also calls after a timeout or an error. Without an asynchronous context, they are closed at once, as before.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #17093 +/- ##
==================================================
+ Coverage 62.4963% 62.5724% +0.0761%
- Complexity 27472 27541 +69
==================================================
Files 2249 2249
Lines 111693 111725 +32
Branches 20478 20486 +8
==================================================
+ Hits 69804 69909 +105
+ Misses 32537 32456 -81
- Partials 9352 9360 +8 🚀 New features to boost your workflow:
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A session kept open for an asynchronous request also keeps its database connection.
| if (sessions.isEmpty()) { | ||
| return; | ||
| } | ||
| if (AsynchronousCompletion.onComplete(request, () -> closeSessions(sessions))) { |
There was a problem hiding this comment.
An action has already queried, then returns a task that needs its own connection. This keeps the first session, and that session keeps the only connection. The task cannot get one. Release the idle connection before keeping the session.
There was a problem hiding this comment.
Yes, that happens. In the hibernate7/datasources example application, with the default datasource limited to one connection, an action that reads outside a transaction keeps the connection on its session. A task that then opens a session of its own waits for the pool's timeout:
| 8.0.x (0518f48) | This pull request | |
|---|---|---|
| A task that opens a session of its own, after a read outside a transaction | Gets the connection | Waits 3 s for the pool, then fails |
The same after a read in withTransaction |
Gets the connection | Gets the connection |
| A lazy load in the task | Fails | Works |
It's what the releases do, though. Before the sessions were closed at the handoff, the default session was never closed after the request went asynchronous, so it kept the connection for good. I measured that with Hibernate 5 on 7.0.x before the same change (00f51d4): the task fails there as it does with this pull request, and the default session is still open after the response.
Releasing the connection at the handoff and keeping the session isn't safe. The task may already be using the session on another thread when the request thread gets there. When the sessions were closed at the handoff, a task that loaded at once got there during the handoff in 18 of 20 requests. A connection given back to the pool under a statement that is still running is worse than one held until the request completes.
A read in a transaction doesn't hold the connection, because the transaction manager disconnects the session when the transaction ends. So an action can avoid it that way. I'd keep the change as it is.
On 8.0.x the pool's timeout then surfaces as a NullPointerException from GrailsHibernateTemplate. convertJdbcAccessException returns the translator's null for an SQLException it can't translate. That's a separate bug.
jdaugherty
left a comment
There was a problem hiding this comment.
Reviewed cf107f7. The completion-based cleanup looks sound; I found no additional must-fix issues beyond the connection-pool behavior already discussed.
The interceptor still unbinds its sessions at handoff, preserves sessions it is only participating in, and closes immediately when there is no asynchronous context. Re-registering the listener in onStartAsync preserves cleanup across another asynchronous cycle.
Local validation on Java 21:
- Hibernate 7 plugin: 45 tests passed, plus 2 CLI tests.
- OsivRequestSessionSpec: all 4 existing HTTP scenarios passed, including lazy loading on both datasources and timeout cleanup.
- Two temporary HTTP probes passed: cleanup after a failed promise, and cleanup after dispatching to a servlet that starts a second asynchronous cycle.
The connection-retention case in the existing discussion remains a limitation: a nontransactional read can retain a pooled connection while the task needs another. I agree that an unconditional disconnect at handoff is unsafe because WebPromises.task can already be using that session on its worker thread. The transaction manager disconnects a pre-bound session after a completed transaction, so finishing the read transaction before submitting the task is a viable mitigation. I would keep this lifecycle fix focused and address automatic connection release separately, with coordination of session ownership and pool-exhaustion regression coverage.
|
@jdaugherty thanks for the review. I agree to keep this one on the lifecycle and to handle the connection separately. #17099 describes it, with the measurements from James's thread, the One thing about CI here: 8.0.x has the recycled-request bug that failed the CI of #17092 (explained there). The lazy-loading feature can fail here the same way, by timing, until #17095 is merged up. It passed on this run. If you're fine with the change as it is, could you approve it? |
codeconsole
left a comment
There was a problem hiding this comment.
Approving. Closing the sessions from the AsyncListener fixes the lazy load in a returned task, and the fallback covers each case I checked:
- With no asynchronous context, or when
addListenerthrowsIllegalStateException, the sessions are closed at once, as before. onStartAsyncregisters the listener again, so a second asynchronous cycle still closes them.- A task that calls
complete()before the request thread registers the listener is fine too. The container defers the completion until the original dispatch returns, so the listener is still called.
I agree that connection retention belongs in #17099.
The two inline comments don't block the merge. Two small things outside the diff:
- The class Javadoc of
GrailsOpenSessionInViewInterceptorstill says "a request that goes asynchronous closes the sessions it opened, so that they do not stay bound to the thread". It now unbinds them at the handoff and closes them when the asynchronous request completes. - The existing features of
MultiDataSourceSessionSpecthat callafterConcurrentHandlingStartedonwebRequest()("a request that goes asynchronous closes the sessions it opened…", "a request that Grails makes asynchronous closes its default session too", and so on) now cover only the request without an asynchronous context. Their names could say so, or the closing could be one feature, next to the new one that completes the context.
| List<Session> released = new ArrayList<>(); | ||
| try { | ||
| closeAdditionalSessions(request, true); | ||
| releaseAdditionalSessions(request, true, released); |
There was a problem hiding this comment.
Non-blocking. Now that the task is meant to keep using these sessions, this flush is the one place where the request thread still touches a session the task may be using. WebPromises.prepareAsyncRequest starts the asynchronous cycle, and the promise factory submits the task, before the action returns. So under AUTO with hibernate.allow_update_outside_transaction, the request thread can flush the secondary session here while the task is initializing a lazy collection in the same persistence context. The sleep(500) in the lazy* actions keeps the integration tests clear of that overlap.
The default COMMIT flush mode never gets here, and the base commit already flushed (and then closed) in the same place, so this isn't new. It's the same question as #17099, who owns the session between the request thread and the task, so I'd add the handoff flush to that issue rather than change it here.
There was a problem hiding this comment.
Agreed. I've added the handoff flush to #17099 (#17099 (comment)) and left it as it is here.
| (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); | ||
| released.add(sessionHolder.getSession()); | ||
| } else { | ||
| super.afterConcurrentHandlingStarted(request); |
There was a problem hiding this comment.
This changes the Spring path too, and the description doesn't mention it. For a Callable or DeferredResult, WebAsyncManager has already called startAsync() when this runs, so the other datasources' sessions released above now stay open until the asynchronous request completes, where they used to be closed at the handoff. That seems right, since the Callable can then lazily load through them as well. But no feature covers it: "a request that Spring makes asynchronous leaves its default session open for Spring to close" uses a request without an asynchronous context. Could you add a feature like the new one, without org.grails.ASYNC_STARTED? It would check that the secondary session stays open until asyncContext.complete(), and that the default session is still left to Spring.
There was a problem hiding this comment.
You're right. I've kept the change and added it to the description. 85f877e adds "a request that Spring makes asynchronous closes the session of another datasource when the asynchronous request completes, and leaves its default session to Spring". It uses a ServletWebRequest that calls startAsync() without org.grails.ASYNC_STARTED. The secondary session stays open until asyncContext.complete(), and the default one is still open after it. The feature fails on 0518f48.
…re a timeout For a Callable or DeferredResult, Spring starts the asynchronous request before the open session in view interceptor runs, so the sessions of the other datasources are closed when the request completes there too. A new MultiDataSourceSessionSpec feature covers that path. The timeout feature of OsivRequestSessionSpec now checks that the sessions stay open while the request waits, so that it fails without the change. The class Javadoc of GrailsOpenSessionInViewInterceptor says when the sessions are closed, and the features that run without an asynchronous context say so.
|
@codeconsole thanks for the review. 85f877e is the same as 0043798 on #17092:
The Hibernate 7 plugin passes 46 tests and The push dismissed your approval. Could you approve again? |
✅ All tests passed ✅🏷️ Commit: 85f877e Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A session kept open for an asynchronous request also keeps its database connection.
| } | ||
| } | ||
| TransactionSynchronizationManager.unbindResource(sf); | ||
| if (released != null) { |
There was a problem hiding this comment.
An action has already used a second datasource, then returns a task that waits. This keeps that session, and the session keeps the only connection. The task cannot get one. Release the idle connection before keeping the session.
The Hibernate 7 counterpart of #17092.
Since 0518f48, the open session in view interceptor of Hibernate 7 closes the sessions it opened as soon as a request goes asynchronous. A task that the action returns can then no longer lazily load an association of an entity the action loaded:
The task fails with a
LazyInitializationException. Without the wait that the example in the tests adds, it fails most of the time with aNullPointerExceptionfrom Hibernate instead ("this.session" is null, inAbstractPersistentCollection.withTemporarySessionIfNeeded), because it gets there while the session is being closed.The change
The same as in #17092:
AsyncListeneron the request's asynchronous context, which the container also calls after a timeout or an error.CallableorDeferredResult, Spring has already started the asynchronous request when the interceptor runs, so the other datasources' sessions are now closed when it completes too, instead of at the handoff. TheCallablecan then lazily load through them, as it already could through the default session. The default session is still left to the interceptors Spring registers for theCallableorDeferredResult.The Hibernate 5 module gets the change through the merge-up of #17092.
Tests
The same as in #17092, in
MultiDataSourceSessionSpecand inOsivRequestSessionSpecof thehibernate7/datasourcesexample. Without the change, both newMultiDataSourceSessionSpecfeatures fail, the one where Grails makes the request asynchronous and the one where Spring does, and so do the lazy-load, timeout and existing asynchronous features ofOsivRequestSessionSpec.With it, the Hibernate 7 plugin passes 46 tests and
hibernate7/datasources36.codeStylepasses for the plugin.On 8.0.x,
WebPromisesmarks the request as started by Grails. So an action that starts a task and doesn't return it also has its default session closed after the container's timeout, which isn't the case on 7.0.x.