title: EJB - Commit and flush within a MDB
body: <p>I have a message driven bean which is communicating with a database via EntityManager. The EM is injected via @PersistenceContext, like normal. I want to flush changes to an Entity immediately without waiting for the MDB to fully complete its processing of the given Message. </p>
  
  <p>For example:</p>
  
  <pre><code>MDB's onMessage() {
    Foo f = em.find(Foo.class, 123);
    f.setNewStatus("Performing work!");
    em.merge(f);
    em.flush();
    ...
    // Continue doing a lot of work...
    ...
  
    f.setNewStatus("Done!");
    em.merge(f);
    em.flush();
  }
  </code></pre>
  
  <p>The problem is that I never see the "Performing Work!" status from outside the context of the MDB (e.g. by logging into the DB directly and checking the tuple's value).</p>
  
  <p>This appears to be related to transactions. From online material, it sounds like a transaction is started within the context of onMessage() and not committed until the method is complete. Hence, the intermediate status is never committed since we eventually write "Done!" which overwrites the Foo's value within the PersistentContext. </p>
  
  <p>Is there a solution to this type of problem? Some way to control the context of the transaction?</p>