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

  • SEARCH
  • Home
  • 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 7876759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:13:07+00:00 2026-06-03T03:13:07+00:00

EntityManager is not thread-safe by definition. Servlets specs says that in non-distributed environment and

  • 0

EntityManager is not thread-safe by definition.
Servlets specs says that in non-distributed environment and without implementing SingleThreadModel, there is only one servlet instance per definition.

Therefore, in Java EE when you inject an EntityManager through the @PersistenceContext into Servlet’s field – it’s not thread safe:

public class MyServlet extends HttpServlet {

    // Not thread-safe, should be using EMF instead.
    @PersistenceContext
    private EntityManager em;
}
  1. Is this correct to say that even though the default scope of Spring beans is singleton, the EntityManager is thread-safe as the Spring uses ThreadLocal to bind its transaction and EntityManager to it?

  2. Is the above Servlets example still valid in Spring? Is it still not thread-safe?

  3. Does the ThreadLocal approach works only for Spring managed beans and plain servlet is not one of those?

  4. As far as I remember, it’s the container responsibility to inject the EntityManager. In Glassfish Java EE implementation, it was the application server who discovers the @PersistenceContext as injection point.
    How does it look like in Spring? Is the Spring Framework responsible for discovering those annotations or it’s responsibility of the JPA implementor?

  • 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-03T03:13:08+00:00Added an answer on June 3, 2026 at 3:13 am

    Question 2, 3, and 4 — Spring does not pay attention to any class that is not a Spring Bean. Therefor Spring does not pay attention to you MyServlet class.
    Therefore the answer for

    • 2) is no
    • 3) only Spring managed Beans
    • 4) it is Springs responsibility, because Spring is the Container

    For Question 1). It works this way, so the usage of an Spring Injected Entity Manager is effective thread save.

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

Sidebar

Related Questions

We have some JavaEE5 stateless EJB bean that passes the injected EntityManager to its
I am not using Spring so I am creating an instance of EntityManager within
I have been struggling with not being able to have the EntityManager be instantiated
I've used JPA for a while. Having an entityManager that performs your SQL operations
I have the following query that works under Hibernate but not under eclipse: select
The documentation for EntityManager.persist() says it will Make an instance managed and persistent .
Is there a way to initialize the EntityManager without a persistence unit defined? Can
I'm new to JPA/OpenJPA and I noticed that if I don't call EntityManager.clear() after
I'm not that cocky to believe Doctrine 2 is broken, but I couldn't actually
EntityManager.merge() can insert new objects and update existing ones. Why would one want to

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.