2 Replies Latest reply on Oct 31, 2011 8:38 AM by Richard Clarke

    Help Required - Synchronisation problem with JMS, JPA and MDB

    Richard Clarke Newbie



      I am struggling with a synchronisation problem when using hibernate and JMS 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


        • 1. Re: Help Required - Synchronisation problem with JMS, JPA and MDB
          Mark Paluch Master

          Hi Richard,

          the best way to deal with this is using at first a transacted JMSSession (JMSConnection.createSession(true, SESSION_TRANSACTED). The second would be using the XA on JMS (Beware: There are two JMS XA ConnectionFactories, JmsXA and XAConnectionFactory, only one of these two work properly, but currently I don't know why...).


          Usually, when you do this within a managed transaction, the container commits the transaction to the resources at the end of the transaction, so everything should work fine. Manual flushing should be only an exception.


          Best regards,


          • 2. Re: Help Required - Synchronisation problem with JMS, JPA and MDB
            Richard Clarke Newbie


            Thanks for the reply.

            You were correct, making the JMS session transacted did the trick.


            I changed my connection factory to the XA one;


                           connectionFactory = (ConnectionFactory) namingContact.lookup("java:JmsXA");

                           connection = connectionFactory.createConnection();


            Then used a transacted session;


                           session = connection.createSession(true, Session.SESSION_TRANSACTED);


            Now in my onMessage method, when I look up the the component in the database, it exists and has the changes made in the sending class.


            Thanks for your help, and I hope this is useful to others.