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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:46:25+00:00 2026-06-18T10:46:25+00:00

I am building a simple web application for which I would like to use

  • 0

I am building a simple web application for which I would like to use JPA. Although I expect to be deploying on Glassfish I was think that it may be beneficial to manage entity persistence within the application rather than through JTA datasource. I acknowledge that this may not be a very bright idea?

Some time ago I put the following together (possibly from a now lost web reference):

public class PersistenceManager {
    private static final PersistenceManager instance = new PersistenceManager();
    protected EntityManagerFactory emf;

    public static PersistenceManager getInstance() {
        return instance;
    }

    private PersistenceManager() {
    }

    public EntityManagerFactory getEntityManagerFactory() {
        if (emf == null) {
            createEntityManagerFactory();
        }
        return emf;
    }

    public void closeEntityManagerFactory() {
        if (emf != null) {
            emf.close();
            emf = null;
        }
    }

    protected void createEntityManagerFactory() {
        this.emf = Persistence.createEntityManagerFactory("Met");
    }
}

So, my questions; Is this a reasonable approach – are there any pitfalls here?

  • 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-18T10:46:26+00:00Added an answer on June 18, 2026 at 10:46 am

    I’ve always deployed JPA without container managed persistence. I’ve even used it with Atomikos JTA transaction manager. The simplest way is to not use JNDI based lookup. I would recomment using Spring’s JPATransactionManager and to configure everything either with persistence.xml or spring-xml.

    Persistence manager in Spring-xml:

    <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="dataSource" />
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath:META-INF/etf-persistence.xml</value>
            </list>
        </property>
    </bean>
    
    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="pum" />
        <property name="persistenceUnitName" value="my-pu" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.cfgfile">my-hibernate.cfg.xml</prop>
    
                <prop key="hibernate.session_factory_name">mySessionFactory</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.archive.autodetection">none</prop>
    
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>       
        </property>
    </bean>
    

    Don’t specify non-jta or jta-data-source tags in persistence.xml. The rest of the setup is standard-spring (EntityManagerFactory, DataSource etc.).

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

Sidebar

Related Questions

I'm building a simple web application that handles a simple database using struts and
I'm building a web application for a University project which requires the use of
I'm building a simple web application in tornado.web using mongodb as the backend. 90%
I'm building a simple web based forum application. I want to allow users to
I am currently building a very small/simple web application in ASP.NET MVC with ADO.NET
I have a web application that relies on a MySQL database, for which I
I'm building a simple live chat into a web application running on Django, but
I'm building a Zend-based Web app that will permit third-party extensions. Essentially, this application's
I'm building an ASP.NET 3.5 application and would like to expose an enum I
Good day, I'm developing a simple Java EE web application in NetBeans 7.2.1 which

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.