Skip to content

fix(hibernate7): close the sessions of an asynchronous request when it completes - #17093

Open
matrei wants to merge 2 commits into
8.0.xfrom
fix/close-sessions-when-async-request-completes-8.0.x
Open

matrei wants to merge 2 commits into
8.0.xfrom
fix/close-sessions-when-async-request-completes-8.0.x

Conversation

@matrei

@matrei matrei commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

import static grails.async.web.WebPromises.task

class ShelfController {

    def show(Long id) {
        Shelf shelf = Shelf.get(id)
        task {
            render(text: shelf.volumes*.title.join(','))
        }
    }
}

The task fails with a LazyInitializationException. Without the wait that the example in the tests adds, it fails most of the time with a NullPointerException from Hibernate instead ("this.session" is null, in AbstractPersistentCollection.withTemporarySessionIfNeeded), because it gets there while the session is being closed.

The change

The same as in #17092:

  • When the request goes asynchronous, the interceptor still takes its sessions off the thread.
  • It now closes them 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, the sessions are closed at once, as before.
  • For a Callable or DeferredResult, 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. The Callable can 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 the Callable or DeferredResult.

The Hibernate 5 module gets the change through the merge-up of #17092.

Tests

The same as in #17092, in MultiDataSourceSessionSpec and in OsivRequestSessionSpec of the hibernate7/datasources example. Without the change, both new MultiDataSourceSessionSpec features 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 of OsivRequestSessionSpec.

With it, the Hibernate 7 plugin passes 46 tests and hibernate7/datasources 36. codeStyle passes for the plugin.

On 8.0.x, WebPromises marks 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.

…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

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.5724%. Comparing base (0518f48) to head (85f877e).
⚠️ Report is 14 commits behind head on 8.0.x.

Additional details and impacted files

Impacted file tree graph

@@                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     

see 14 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matrei

matrei commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@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 withTransaction mitigation and what a fix needs: a rule for who owns the session between the request thread and the task, and a test that exhausts a small pool.

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
codeconsole previously approved these changes Nov 20, 2026

@codeconsole codeconsole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 addListener throws IllegalStateException, the sessions are closed at once, as before.
  • onStartAsync registers 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 GrailsOpenSessionInViewInterceptor still 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 MultiDataSourceSessionSpec that call afterConcurrentHandlingStarted on webRequest() ("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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@github-actions
github-actions Bot dismissed codeconsole’s stale review November 21, 2026 07:36

Dismissed automatically: the pull request changed after this approval, from cf107f7 to 85f877e.

@matrei

matrei commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@codeconsole thanks for the review. 85f877e is the same as 0043798 on #17092:

The Hibernate 7 plugin passes 46 tests and hibernate7/datasources 36. I've updated the description.

The push dismissed your approval. Could you approve again?

@testlens-app

testlens-app Bot commented Nov 21, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 85f877e
▶️ Tests: 106740 executed
⚪️ Checks: 99/99 completed


Learn more about TestLens at testlens.app/docs.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A session kept open for an asynchronous request also keeps its database connection.

}
}
TransactionSynchronizationManager.unbindResource(sf);
if (released != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants