Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use CDI interceptors to create and tear down JDO PMs in our web apps.</p> <p>You can define an interceptor method relying on ThreadLocal in a helper class. I'm simplifying my code here (read: untested :) ) as you may have to deal with multiple data sources, re-entrances, etc., but as illustration:</p> <pre><code>public class MyJDO_Helper { private static ThreadLocal&lt;PersistenceManager&gt; localPM = new ThreadLocal&lt;&gt;(); @AroundInvoke public Object allocatePersistenceManager(InvocationContext ctx) throws Exception { PersistenceManager pm = localPM.get(); if (pm == null) { pm = getNewPersistenceManager(); localPM.set(pm); } try { return ctx.proceed(); } catch(Error err) { // to log them before Jersey blows casting them to Ex log.log(Level.SEVERE, "Caught " + err, err); throw err; } finally { try { if (pm.currentTransaction().isActive()) { pm.currentTransaction().rollback(); } pm.close(); } catch(Exception ex) { log.log(Level.SEVERE, "Error closing JDO PM: " + ex, ex); } } } public static PersistenceManager getPersistenceManager() { return localPM.get(); } </code></pre> <p>Then you can decalre this interceptor on Jersey resource methods needing JDO access:</p> <pre><code>@ManagedBean // for interceptors @Path("/my") public class MyResource { @GET @Interceptors({MyJDO_Helper.class}) public String get() { PersistenceManager pm = MyJDO_Helper.getPersistenceManager(); </code></pre> <p>At that point you can call MyJDO_Helper.getPersistenceManager() from anywhere in your code (even not Jersey classes) anytime and you'll always get the right PM without the need to pass it around. :)</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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