0 Replies Latest reply on Jan 23, 2008 2:57 AM by mclu

    Use of TransactionLocal from a EJB3 SessionBean

    mclu

      Hi!

      I need to store data which is altered/touched within a transaction. At the end of that transaction (leaving my first SLSB) I need to create a report and send it to a JMS Queue.

      So ThreadLocal is problematic in managed environments, Context is not transaction based so I searched and find TransactionLocal!
      But how to use it right.

      I have a EJB3 SLSessionBean DataCollectorHelperBean which should collect and store the data. This is used by all session beans which starts a transaction (first layer in our BL).
      All data is collected in a UCData object which I want to store in a TransactionLocal.

      Complicated? So here is the flow:
      - Web calls BL Bean A
      - Bean A registers some data in the DataCollectorHelperBean
      - other beans register status informations using the DataCollectorHelperBean
      - before returning the call from Bean A (ending the transaction) I want to get the UCData object from the DataCollectorHelperBean and pass it to a JMS module.


      OK now the Question:
      What is the best /working design for it?

      A: Using a TransactionLocal in the Stateless Session Bean:

      @Stateless(name = "UseCaseDataCollector")
      public class UseCaseDataCollectorBean implements UseCaseDataCollector {
      
       private static final TransactionLocal singleton = new TransactionLocal();
      
       public void registerStuff(Object x){
       UCData myData = singleton.get();
       myData.addData(x);
       }
      }
      


      B: Using a Singleton Class:
      @Stateless(name = "UseCaseDataCollector")
      public class UseCaseDataCollectorBean implements UseCaseDataCollector {
       public void registerStuff(Object x){
       MySimpleSingleton.getInstance().registerStuff(x);
       }
      }
      
      public class MySimpleSingleton {
      
       private static final TransactionLocal singleton = new TransactionLocal();
       private static final MySimpleSingleton INSTANCE = new MySimpleSingleton ();
      
       public void registerStuff(Object x){
       UCData myData = singleton.get();
       myData.addData(x);
       }
       ....
      }
      


      Put you option C here!!

      Are both ways bullshit? (Then please tell me how to handle it)
      I am not using a clustered environment!


      Thx!
      Markus Lutum