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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:41:58+00:00 2026-05-26T07:41:58+00:00

What is the best approach to get EntityManagerFactory in web app(jsp/servlets)? Is this a

  • 0

What is the best approach to get EntityManagerFactory in web app(jsp/servlets)?
Is this a good way When should EntityManagerFactory instance be created/opened?,
or is it better to get it from JNDI, or something else?

  • 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-05-26T07:41:59+00:00Added an answer on May 26, 2026 at 7:41 am

    They’re heavyweight and they’re supposed to be in the application scope. So, you need to open them on application startup and close them on application shutdown.

    How to do that depends on your target container. Does it support EJB 3.x (Glassfish, JBoss AS, etc)? If so, then you don’t need to worry about opening/closing them (neither about transactions) at all if you just do the JPA job in EJBs with @PersistenceContext the usual way:

    @Stateless
    public class FooService {
    
        @PersistenceContext
        private EntityManager em;
    
        public Foo find(Long id) {
            return em.find(Foo.class, id);
        }
    
        // ...
    }
    

    If your target container doesn’t support EJBs (e.g. Tomcat, Jetty, etc) and an EJB add-on like OpenEJB is also not an option for some reason, and you’re thus manually fiddling with creating EntityManagers (and transactions) yourself, then your best bet is a ServletContextListener. Here’s a basic kickoff example:

    @WebListener
    public class EMF implements ServletContextListener {
    
        private static EntityManagerFactory emf;
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            emf = Persistence.createEntityManagerFactory("unitname");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            emf.close();
        }
    
        public static EntityManager createEntityManager() {
            if (emf == null) {
                throw new IllegalStateException("Context is not initialized yet.");
            }
    
            return emf.createEntityManager();
        }
    
    }
    

    (note: before Servlet 3.0, this class needs to be registered by <listener> in web.xml instead of @WebListener)

    Which can be used as:

    EntityManager em = EMF.createEntityManager();
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best approach for defining page width of a web app? Most
Trying to get some good advice on the best approach to using arrays for
What is the best approach to releasing a new version of a hosted web
What is the best approach to implementing authorisation/authentication for a Windows Forms app talking
What is a best approach to get rid of a boilerplate code that serializes/deserializes
This issue is simple, but I am not sure what is the best approach
I don't know if this is the best approach so that's why I'm asking
Need advice on what is the best approach to get the auto incremented id
Could someone suggest what’s the best approach to go about this: What I like
Given this page , what will be the best approach to the layout? How

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.