2 Replies Latest reply on Oct 26, 2011 7:38 AM by Richard Clarke

    Help Required - JMS/Database Synchronisation issue

    Richard Clarke Newbie

      Hi,

       

      I am struggling with a synchronisation problem when using hibernate and hornetq under JBOSS AS 6.0.0.Final.

      Forgive me if I get my terminology wrong, I am far from an expert in this!

       

      I use a database facade class to perform all of my DB operations, which includes my entity manager persistence context. This 'dbFacade' class is injected into both my sending and listening beans.

       

      In my sending class, I am using teh injected dbFacade (and it's entity manager) to retrieve and modify the property of a component, which I then merge and flush the entity manager.

       

      I subsequently use JMS to send a message to an MDB to perform some asynchronous operations (so I can return the UI to the user).

      The JMS message includes a reference to the component id in my database (MySQL).

       

      My MDB injects the dbFacade and the 'onMessage' method retrieves the component form the database using the component id.

      The problem is that the retrieved component does not include the modifications made before the message was sent.

       

      For example I set the component status to a value, but the component retrieved in the onMessage method has a different value (the prior value).

       

      I am able to get around this problem by forcing the MDB to sleep a few seconds before retrieving the component, which suggests this is a timing issue, where the modifications in the sending class have not been written back to the database before they are read by the MDB.

       

      Is there a better way to handle this scenarion? Some Ideas I had were...

       

      1/ Do I need to make my dbFacade a singleton - is this an issue with the sending class and the MDB using different entity managers?

      2/ Is there a way to force the entity manager to write the changes back to the db before sending the message (Apart from calling flush() on the entity manager which I am already doing)

      3/ Should I look to use @Asynch rather than MDB's

       

      I have also posted this to the hibernate forum.

       

      Any other ideas???

      Thanks in advance

      Rich