7 Replies Latest reply on Feb 11, 2004 10:20 AM by gort

    how to get Container Managed Transactions in session bean?

    gort

      Hi,

      I am trying to use container managed transactions but only bean-managed work.
      I am using Jboss 3.2.1 with Jetty. I do not have any entity beans. I have a connection that i got from a DataSource in a session bean which calls various methods.

      Pseudo-code (includes bean-managed things i commented out -- my goal is to make them go away):



      //javax.transaction.UserTransaction ut = sessionContext.getUserTransaction();
      Connection connx = null;

      try {
      // ut.begin();

      connx = getDataConnection (DEFAULT_DB);
      // connx.setAutoCommit(false);

      sqlInsertOne( connx );

      if (true) throw new NullPointerException("transaction roll back, darn it!");

      sqlInsertAnotherOne( connx );

      // ut.commit();
      // connx.commit();

      } catch (Exception e) {
      // connx.rollback ();
      // ut.rollback();
      throw e;
      }


      The goal is to made the first statement, sqlInsertOne(), not happen when the second statement (or I) throws an exception.

      If i use bean-managed transactions and *iether* the transactions provided by UserTransaction, or just the commit() and rollback() provided by the Connection, then it is fine. My exception gets thrown and sqlInsertOne() does not happen.

      But the second i try to use Container managed transactions, everything goes awry. sqlInserOne() inserts! how do i make it stop? am i missing a configuration file? are session beans meant to manage transactions?

      I change ejb-jar.xml. To make it container managed i have

      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      and

      <container-transaction>
      < method >
      <ejb-name>TradeEngine</ejb-name>
      <method-name>*</method-name>
      </ method >
      <trans-attribute>Required</trans-attribute>
      </container-transaction>

      am i supposed to edit some other config file besides ejb-jar.xml?

      thanks to anyone who can help.

        • 1. Re: how to get Container Managed Transactions in session bea
          perseus

          Hi gort,

          I have the same problem, like some other guys too.

          I found a brand new 70 euros problem ticket at www.infutura.com dealing with exactly the same question.

          I think we have to wait until it is solved there.
          Solutions are free. To view solution proposals you have to be a member. But membership is free.

          Regards
          Jack

          • 2. Re: how to get Container Managed Transactions in session bea

            setRollbackOnly before throw your exception.

            • 3. Re: how to get Container Managed Transactions in session bea
              gort

              thanks, but i don't need to set setRollbackOnly, it rolls back correctly when i use bean-managed (my own) transactions as shown commented out.
              i'm trying to avoid writing tx.commit() and tx.rollback() and tx.setRollbackOnly() in the first place. by setting my session bean to container-managed transactions.

              • 4. Re: how to get Container Managed Transactions in session bea
                pilhuhn

                What do you want to achieve?
                If you want a CMT to be rolled back you also need to tell it so.
                If an exception occours, fine. If your business logic decides to roll back - how should the container know this?

                • 5. Re: how to get Container Managed Transactions in session bea
                  gort

                  i thought the point of a container-managed transaction is that iether the entire thing commits, or the entire thing fails. Like that whole ATM example, getting $ and subtracting it from the account is one transaction. if error happens the whole thing does not happen -- it doesn't subtract money from your account without giving it to you -- it rolls back.

                  bascially what you're saying is that there is no such thing as a container managed transaction, even when ejb-jar.xml has a <trans-attribute>Required</trans-attribute>, i still need to manage transactions myself -- telling them when to start, commit, and roll back?

                  • 6. Re: how to get Container Managed Transactions in session bea
                    gort

                    I tried to get a usertransaction in a contaniner-managed bean and Jboss told me "CMT beans are not allowed to get a UserTransaction"

                    so i using setRollBackOnly() or even rollBack() is not the solution to the container-managed transactions problem, because Jboss will not even let me access it in Container Managed Transactions.

                    my question is not about using UserTransaction (UserTransaction works in Bean-managed session beans but not, as shown, CMT) but about getting a container-managed transaction to roll back when it fails!

                    • 7. Re: how to get Container Managed Transactions in session bea
                      gort

                      ok i get it...
                      i need to set setRollbackOnly () on the *SessionContext* when i catch exception.
                      i didn't see that method there...
                      it works now