4 Replies Latest reply on May 21, 2008 3:57 PM by sankul123

    Atomic process in Local SSB -Urgent !!!!!!!

    sankul123

      Hi ALL,

      I have a following process, to be done by (Local)SSB[Stateless Session Bean]-
      <atomic operations in a method in SSL>

      1. Persist [update] some values in database.
      2. Some file operations , write some data to file.
      </atomic operations>
      I want both operations to be atomic. My Local SSL EJB is a CMT and its method 'transaction' attribute is marked for new transaction.

      void myAtomicProcess(String a, String b)throws MyException{
      
      //1. update db...
      
      //2. update file ...
      
      }


      If there is any exception while comitting[at the end of method] , I want to rolback my changes done to file i,e restore the file as it was.

      Please help me to achieve this.

        • 1. Re: Atomic process in Local SSB -Urgent !!!!!!!
          kahzoo

          Can you switch to stateful session bean? If yes, you can try the following:

          1. Have the stateful sesion bean class implements javax.ejb.SessionSynchronization interface.

          2. Make the database operation transactional, so that in the case of JTA rollback the data is not updated in the DB.

          2. Within the afterCompletion() notification method, check the transaction status (which is passed as argument). If it is true (meaning transaction is committed), write your data into the file. Otherwise (meaning rollback), do not write your data into the file.

          Please note that this is NOT a perfect solution, because the file operation within the afterCompltion() can fail.

          You need to provide good error logging, so that the system admin can later fix the inconsistency manually.

          Please check the EJB spec for details.

          Hope this helps.

          • 2. Re: Atomic process in Local SSB -Urgent !!!!!!!
            sankul123

             

            "kahzoo" wrote:
            Can you switch to stateful session bean?


            Yes I can , but after assessing there is not much hit on performance.
            One thing that worries me with Synchronization is that Synch is on whole bean and not the methods :).

            Now I need to have more than one Stateful session bean.

            This is what I am planning to do , have a SSL and call the Stateful session beans which implement SessionSynchronization.

            please let me know , if this is correct ...

            Regards,
            Sandeep Kulkarni

            • 3. Re: Atomic process in Local SSB -Urgent !!!!!!!
              kahzoo

               

              One thing that worries me with Synchronization is that Synch is on whole bean and not the methods :).

              Now I need to have more than one Stateful session bean.


              I'm sorry, but I'm failing to understand what your concern is.

              Are you saying that you have to split the logic in two different beans?

              That is not the case, and you can implement the logic in one stateful session bean(, in which you will have both your business method and the synchronization methods). You just call the business method and the synchronization methods will be automatically called at appropriate points of the CMT transaction.

              If StatefulSessionBean/SessionSynchronization is not satisfactory, the other option would be to switch from CMT to BMT (bean-managed transaction).

              With BMT, you need to do the transaction demarcation (begin, commit, rolback) yourself, but since your bean will know the outcome of the transaction, it can decide to write/do-not-write data in the file accordingly.


              • 4. Re: Atomic process in Local SSB -Urgent !!!!!!!
                sankul123

                Thank you Khazoo , for your support.

                "kahzoo" wrote:


                With BMT, you need to do the transaction demarcation (begin, commit, rolback) yourself, but since your bean will know the outcome of the transaction, it can decide to write/do-not-write data in the file accordingly.



                This is how I have implemented ,
                Used SSB with BMT-
                void myAtomicProcess(String a, String b)throws MyException{
                 try{
                 userTx.begin()
                 //2. update file ...
                 fileOperFlag = true
                 //1. update db...
                 userTx.commit()
                 DBoperFlag = true
                
                 }catch(){}
                 finally{
                 if(fileOperFlag && !DBoperFlag){
                 //Restore the file to its original state
                 }
                
                 }
                
                }
                


                Please let me know if you see any flaw in this.

                Regards,
                Sandeep Kulkarni