!--a11y-->
Using JDO with Message-Driven
Beans 
The life cycle of a message-driven bean (MDB) is similar to that of a stateless session bean. When the MDB instance is created, the container invokes its setMessageDrivenContext() method, and after that the ejbCreate() method. The instance is then ready for an invocation of the onMessage() method, which is the only business method of an MDB. When the instance is not needed any more, the ejbRemove() method is called to dissociate the bean from the particular destination.
For more information, see Developing Message-Driven Beans.
Just like when you use JDO with a session bean, you must look up the PersistenceManagerFactory (PMF) in the setMessageDrivenContext() method:

public class MyMessageDrivenBean implements MessageDrivenBean { MessageDrivenContext mdc; PersistenceManagerFactory pmf; public void setMessageDrivenContext(MessageDrivenContext mdc) { this.mdc = mdc; try{ Context ctx = new InitialContext(); pmf = (PersistenceManagerFactory)ctx.lookup("java:comp/env/jdo/defaultPMF"); }catch (Exception e){ } } ... |
The PersistenceManager instance is obtained in the onMessage() method. The PM must be closed before the method returns:

public void onMessage(Message msg){ PersistenceManager pm = null; try{ pm = pmf.getPersistenceManager(); // do work ... } finally { if (pm!=null && !pm.isClosed()) pm.close(); } } |
See also:
Working with Persistent Objects
