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

    how to get Container Managed Transactions in session bean?

    gort Newbie

      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.