Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:58:26+00:00 2026-06-13T12:58:26+00:00

We’re trying to enable Servlet’s 3.0 async-supported in our Spring application: Added in web.xml

  • 0

We’re trying to enable Servlet’s 3.0 “async-supported” in our Spring application:

  • Added in web.xml (async-supported)true(/async-supported) for all servlets and filters.

  • Rewrote the request handling code as

**

request.getAsyncContext().start(new Runnable() {
  @Override
  public void run() {
    handleRequest(servlet, request, response);
  }
});

**

This causes two problems:

  1. Spring Security authentication is lost when running the asynchronous code.

  2. There is no EntityManager/session/… any more either.

Of course both problems are due to the fact the handler code is not executed in the same thread as the thread that created the Runnable. Ideally the worker thread should “inherit” the Spring contexts from the invoking thread.

I could get around the Spring Security problem by saving the authentication at construction time and setting it at execution time.

private final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

try {
  SecurityContextHolder.clearContext();
  SecurityContextHolder.getContext().setAuthentication(authentication);

  ..run code here..

} finally {
  SecurityContextHolder.clearContext();
} 

However, for the second problem I have no clue how to “transfer” the session to the executing thread. For servlet 2.x we were using OpenEntityManagerInViewFilter to stick a session to the thread.

<filter>
  <filter-name>openEntityManagerInViewFilter</filter-name>
  <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  <async-supported>true</async-supported>
  <init-param>
    <param-name>entityManagerFactoryBeanName</param-name>
    <param-value>entityManagerFactory</param-value>
  </init-param>
</filter>

But now of course this does not make sense any more because the thread it sticks it to is not the same as the one which executed the request.

I’ve tried various versions of Spring 3.1 and 3.2. But to no effect.

Anybody who knows how to get around this ? Preferably with hacking. 🙂

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

  <servlet>
    <servlet-name>Foo</servlet-name>
    <servlet-class>com.foo.Main</servlet-class>
    <async-supported>true</async-supported>
  </servlet>

  <servlet-mapping>
    <servlet-name>Foo</servlet-name>
    <url-pattern>/restricted/*</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>entityManagerFactoryBeanName</param-name>
      <param-value>entityManagerFactory</param-value>
    </init-param>
  </filter>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
  </filter>

  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <listener>
    <listener-class>org.bar.ServletInit</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.foo.SpringCtxInitializer</param-value>
  </context-param>

  <filter-mapping>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <url-pattern>/restricted/*</url-pattern>
  </filter-mapping>

</web-app>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:58:27+00:00Added an answer on June 13, 2026 at 12:58 pm

    You need to open a new session/entitymanager for that async thread (sessions can’t be shared between threads). For this purpose you might want to use OpenSessionInViewInterceptor (or I guess for JPA it’s called OpenEntityManagerInViewInterceptor), but you’ll need to manually invoke its methods or write your own aspect for this purpose and use proxies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.