Bean Managed Transaction Questions

Discussions

EJB programming & troubleshooting: Bean Managed Transaction Questions

  1. Bean Managed Transaction Questions (2 messages)

    1) When exactly to get the java.sql.Connection in a BMT? After you get the UserTransaction, can you get the java.sql.Connection before you call begin transaction or must you wait and get the java.sql.Connection after you begin the transaction? Does the sequence matter at all?

    2) I have two stateless session beans. The first bean uses BMT. It calls methods on the second bean. My question is in regards to what the correct way is for setting the transaction type in the ejb-jar.xml file for the second bean. I want the the second bean to have a trans-attribute of "Mandatory" but of course this is an attribute for container managed transactions. Is it correct to make the secondary bean use container managed transactions when it will always be called by a bean managed transaction bean? I'm simply wanting the container to verify that the secondary bean is involved in a transaction already. Since it will be declared "Mandatory" it will never actually create a transaction buy will throw back an error if it's not involved in one already.
  2. Bean Managed Transaction Questions[ Go to top ]

    1) You may use any order you want - either create/close the resource within the transaction or outside it. The rule to remeber is:
    All updates to a resource performed within a transaction (i.e, between UserTransaction.begin() and UserTransaction.commit/rollback()) belong to the same transaction.
    For instance, you can create a connection, start a transaction, update the connection, commit, start another transaction, update again, commit again.
    The convention, AFAIK, is to create the resource connections outside the transaction, but it is not mandatory.
    For a normative description see subsection 17.3.3 of the EJB2.0 spec.

    2) You should do what you suggested: use CMT for the second bean, and declare the transaction attribute to be "mandatory". If you declared the second bean to use BMT the second bean would create a seperate transaction for itself, not use the invoking transaction. Only use BMT when your bean actually needs to "manage" it's own transactions.

    Hope that helps
    Gal
  3. Bean Managed Transaction Questions[ Go to top ]

    Thanks for your help Gal.