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 8858033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:44:02+00:00 2026-06-14T14:44:02+00:00

Trying to figure out some possible situations, when a EntityManagerFactory could be useful in

  • 0

Trying to figure out some possible situations, when a EntityManagerFactory could be useful in EJB. Of course usually one needs just an transaction-scoped EntityManager (JTA scoped), so that all injected EntityManagers share the same PersistenceContext.
What happens in the following situations:

  1. When @PersistenceContext(type=PersistenceContextType.EXTENDED) is used: is this JTA-enabled? will the requests of such an EntityManager be executed in the context of the JTA? If not, that in which one? (Note: of course it works only with @Stateful EJBs). Of course it is clear, that in this case the EntityManager will have its own/special PersistenceContext.

  2. When one uses @PersistenceUnit to get a EntityManagerFactory in an EJB (I suppose it works in all type of EJBs, correct?), is the obtained Entitymanager JTA-enabled (of course Entitymanager.joinTransaction() is necessary)? How can one get a transaction-scoped (JTA enabled) or Extended EntityManager from the Factory? When would be useful to use the Factory, instead of a Entitymanager. (Of course it is clear that an EntityManagerFactory is the only interface for a Java SE application to the JPA, but what about EJB?).

  • 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-14T14:44:04+00:00Added an answer on June 14, 2026 at 2:44 pm

    Your questions revolve around the assumption that you are put in a situation were you are obliged to inject @PersistenceUnit to get a EntityManagerFactory in an EJB. Well… let me narrate some missing background here:

    In servlets

    We had to follow these steps in order to instantiate an EntityManager:

    1. Inject @PersistenceUnit to obtain an EntityManagerFactory
    2. Use the line: entityManager = entityManagerFactory.createEntityManager();

    The above 2 steps quarantees the EntityManager is local to the current thread thus thread-safe. This scenario was a workaround for servlets being re-entrant with respect to the container injection. Meaning the servlet will accept already existing entityManagers to be injected by the container.

    In EJBs

    By definition, EJBs are non-reentrant with respect to the container injection, thus we know that the container will instantiate a fresh entityManager proxy local for the EJB, thus thread-safe.

    That said we can take the discussion into how/when to use Extended PersistenceContext

    Quoting the JSR-317 of the JPA specifications page 294:

    A container-managed extended persistence context can only be initiated within the scope of a stateful session bean. It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound to the stateful session bean…
    The persistence context is closed by the container when the @Remove method of the stateful session bean completes (or the stateful session bean instance is otherwise destroyed).

    Thus the non-reentrancy feature can be broke using the attribute PersistenceContextType.EXTENDED only to serve the stateful session bean semantics. It is mandatory for the stateful session bean to maintain the very same EntityManager as long as it exists.

    To conclude

    In your first question:

    when a EntityManagerFactory could be useful in EJB?

    the answer is: It is not useful.

    As for your second question I am sure the first answer and the above background narration discards the need to ask it in the first place!

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

Sidebar

Related Questions

I am trying to figure out some way to get actual time via JavaScript,
I have been trying to figure this out for some time. It seems lots
I'm trying to figure out how to extract some data from a string according
I'm having some problems trying to figure out how to generate a Google Map
I am having some trouble trying to figure out, how to accomplish the following.
I am having some trouble trying to figure out how to parse information collected
I'm trying to figure out a way to serialize some Django model object to
I spent some time trying to figure out why this query isn't pulling the
I'm trying to figure out to get rid of some spaces that Visual studio
I've been trying to figure this out for quite some time, and it seems

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.