Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to the <a href="http://db.apache.org/jdo/pmf.html" rel="nofollow">JDO Documentation</a> you create one <code>PersistenceManagerFactory</code> per datastore. If you are using JDO to access databases via SQL and you have more than one database, then you will need one <code>PersistenceManagerFactory</code> per database (since you need to specify the JDBC URL, user name and password when you create the <code>PersistenceManagerFactory</code>).</p> <p>For simple use cases, you can just create a <code>PersistenceManager</code> when you need it and close it in a <code>finally</code> clause (see <a href="http://db.apache.org/jdo/pm.html" rel="nofollow">the persistence manager documentation</a>).</p> <p>If you use transactions, and the code for updating entities can be spread across multiple methods or objects, I recommend creating the <code>PersistenceManager</code> on demand and storing it in a <code>ThreadLocal</code> (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 <code>PersistenceManager</code> at the end of the request.</p> <p>If you only need one persistence manager factory, you can do:</p> <pre><code>public class Datastore { private static PersistenceManagerFactory PMF; private static final ThreadLocal&lt;PersistenceManager&gt; PER_THREAD_PM = new ThreadLocal&lt;PersistenceManager&gt;(); 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(); } } } </code></pre> <p>Any code that needs a persistence manager can call <code>Datastore.getPersistenceManager()</code></p> <p>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 <code>Datastore</code> as a Singleton.</p> <p>You could call <code>finishRequest</code> in a Servlet Filter:</p> <pre><code>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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload