Stateless session bean and container managed transactions

Discussions

EJB design: Stateless session bean and container managed transactions

  1. Hi all,
    i'm an ejb newbie and am wondering what's the proper way to do things in this case:
    Let's say i have a stateless session bean. It has methodA which is declared with the xactional attribute of 'Required'. It finds a datasource, gets a connection, updates a bunch of tables and the container commits if we return normally, or rollsback if we setRollbackOnly.
    Similarly, i have methodB in the same SSB which does the same thing except that it updates a different bunch of tables (in the same database).
    Now, it is forseeable in the future that we will be asked to make both sets of updates in one transaction, although the ability to invoke the individual transaction will still be required.
    So what is the best way to handle this?
    Can i just introduce methodC (marked as 'Required') into my aab which calls methodA then methodB? Will this work? Or do i have to remotely call the same ejb from my methodC?
    At the moment i am just using a 'normal' datasource. Should i use an 'XA' datasource to make it work?
    If i decided not to introduce methodC into the ssb, can i let the client (probably some servlet) take care of creating the transaction (somehow... not to sure about this)?
    Basically, i need to leave methodA and methodB in place, but if i also want the ability to make them both operate as a transactional unit what are my options?
    Thanks in advance,
    - Richard.
  2. Richard:

    You should be OK with Method C initiating a TXN and covering A & B. This is because it is the resp. of the container to make sure that it returns the "SAME" connection to Method B - when it requests for a connection since it is in a larger TXN scope.

    Normally TXNs get associated with a THread ID and as long as your call is within the same thread - even normal data source connections should be able to do this.

    Hope I got your problem

    -Sanjay
  3. Hi all,i'm an ejb newbie and am wondering what's the proper way to do things in this case:Let's say i have a stateless session bean. It has methodA which is declared with the xactional attribute of 'Required'. It finds a datasource, gets a connection, updates a bunch of tables and the container commits if we return normally, or rollsback if we setRollbackOnly.Similarly, i have methodB in the same SSB which does the same thing except that it updates a different bunch of tables (in the same database).Now, it is forseeable in the future that we will be asked to make both sets of updates in one transaction, although the ability to invoke the individual transaction will still be required.So what is the best way to handle this?Can i just introduce methodC (marked as 'Required') into my aab which calls methodA then methodB? Will this work? Or do i have to remotely call the same ejb from my methodC?At the moment i am just using a 'normal' datasource. Should i use an 'XA' datasource to make it work?If i decided not to introduce methodC into the ssb, can i let the client (probably some servlet) take care of creating the transaction (somehow... not to sure about this)?Basically, i need to leave methodA and methodB in place, but if i also want the ability to make them both operate as a transactional unit what are my options?Thanks in advance,- Richard.

    >Can i just introduce methodC (marked as 'Required') into my aab which calls methodA then methodB? Will this work?

    Yes it will work.

    >At the moment i am just using a 'normal' datasource...

    If both the updates are happening on the same database (I understand that is the case) then you do NOT require XA connection.


    > If i decided not to introduce methodC into the ssb, can i > let the client (probably some servlet) take care of
    > creating the transaction (somehow... not to sure about this)?

    Yes by using a javax.jts.UserTransaction object and then programmatically starting the transaction. The bean methods will then pariticipate in the transaction initiated by your servlet. How to obtain a UserTransaction in the servlet depends on your server. Read this answer here to get an idea - http://www.jguru.com/faq/view.jsp?EID=2566http://www.jguru.com/faq/view.jsp?EID=2566

    Programmatic transaction demarcation this way is usually not a good idea - from what I have been told :-) So maybe introducing method C is a better option.

    Regards
    Kingshuk
  4. Sorry, the link in the earlier message was not clear. Here is it again -

    http://www.jguru.com/faq/view.jsp?EID=2566