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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:43:28+00:00 2026-05-18T03:43:28+00:00

I have two questions regarding how to create / use the JDO persistence manager

  • 0

I have two questions regarding how to create / use the JDO persistence manager (PM, hereafter).

Say, in a Java web application, if I have 10 entities, which can be logically grouped into 2 groups (for example, 5 user related entities and 5 business related entities)

  1. Should I need two different PMs to manage these 2 groups or only one PM is enough?
  2. Regarding the initialization, shall I use singleton instance of a PM (which will be shared by all the user’s using the app at a given point of time) or should I create a PM for each and every session?
  • 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-18T03:43:29+00:00Added an answer on May 18, 2026 at 3:43 am

    According to the JDO Documentation you create one PersistenceManagerFactory per datastore. If you are using JDO to access databases via SQL and you have more than one database, then you will need one PersistenceManagerFactory per database (since you need to specify the JDBC URL, user name and password when you create the PersistenceManagerFactory).

    For simple use cases, you can just create a PersistenceManager when you need it and close it in a finally clause (see the persistence manager documentation).

    If you use transactions, and the code for updating entities can be spread across multiple methods or objects, I recommend creating the PersistenceManager on demand and storing it in a ThreadLocal (or request-scoped object if you use Guice or Spring). This will make sure any code that does updates participates in the current transaction. Make sure to close the PersistenceManager at the end of the request.

    If you only need one persistence manager factory, you can do:

    public class Datastore {
      private static PersistenceManagerFactory PMF;
      private static final ThreadLocal<PersistenceManager> PER_THREAD_PM
          = new ThreadLocal<PersistenceManager>();
    
      public static void initialize() {
         if (PMF != null) {
           throw new IllegalStateException("initialize() already called");
         }
         PMF = JDOHelper.getPersistenceManagerFactory("jdo.properties");
      }
    
      public static PersistenceManager getPersistenceManager() {
        PersistenceManager pm = PER_THREAD_PM.get();
        if (pm == null) {
          pm = PMF.getPersistenceManager();
          PER_THREAD_PM.set(pm);
        }
        return pm;
      }
    
      public static void finishRequest() {
        PersistenceManager pm = PER_THREAD_PM.get();
        if (pm != null) {
          PER_THREAD_PM.remove();
          Transaction tx = pm.currentTransaction();
          if (tx.isActive()) {
             tx.rollback();
          }
          pm.close();
        }
      }
    }
    

    Any code that needs a persistence manager can call Datastore.getPersistenceManager()

    Note: I used all static methods to make it simple for the purposes of answering your question. If I was using a dependency-injection framework like Guice, I would make the methods non-static and bind Datastore as a Singleton.

    You could call finishRequest in a Servlet Filter:

    public class PersistenceManagerFilter implements javax.servlet.Filter {
    
      public init(FilterConfig filterConfig) {
        Datastore.initialize();
      }
    
      public void doFilter(ServletRequest request, 
          ServletResponse response, FilterChain chain)
          throws IOException, ServletException {
        try {
          chain.doFilter(request, response);
        } finally {
          Datastore.finishRequest();
        }    
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions regarding Java web application deployment and its impact on performance.
I actually have two questions regarding the same problem but I think it is
I have two questions regarding the '::' case class. :: can be used as
I have several questions regarding CI application design. Q. When creating a new form
I have two questions regarding RewriteRule, but they're both closely related so I hope
I have two questions regarding Erlang file i/o; what is the best way to
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have two questions: 1) How can I make an array which points to
I have two questions. Do realloc() and memcpy() copy the entries in an array
Say I have two tables, user and comment . They have table definitions that

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.