4 Replies Latest reply on Apr 25, 2004 3:05 AM by Michael Forster

    Rolback Problem

    Steven Harris Newbie

      This is the second time I've posted this message on this forum. The first time it disappeared.

      A simple transaction rollback was working with hypersonic, but seems not to be working when we switch to mysql.

      Running mysql-connector-java-3.0.11 and mysql 4.0.18-0 (client and server) with JBoss 3.2.3 java 1.4.2_04 on both linux and windows.

      We have 2 stateless session beans FooService (remote) and BarService (local). FooService has a createFoo() method which, after creating an Foo entity bean (local with CMP & CMR data), calls validate() on BarService. Validate() does some data validation and if invalid data, throws a RemoteException which is caught in createFoo() and transformed into another remote exception to be sent back to the client (in this case a junit test). Both FooService and BarService are declared to have container managed transactions, and both createFoo() and validate() have the required transaction attribute. There is no client transaction.


      createFoo( ) {
      try {
      fooHome.create(); //create entity bean
      bar.validate(); // get a RemoteException
      catch (Exception e) {
      throw RemoteException; //entity bean creation should rollback
      }

      This code works with hypersonic but not with mysql. I do see the TransactionRollbackException on the jboss console, and a remote exception is thrown back to the client, but the Foo entity bean is in the database. Is there a configuration of msql that is causing this?






        • 1. Re: Rolback Problem
          Kabir Khan Master

          You are throwing the wrong exception.

          You should generally not be creating your own RemoteExceptions (they are meant for use by Java when something goes wrong with the remote communication). This is the case in EJB 2.0 at least, maybe this was different in EJB 1.1? I can't remember.

          For rollback to take place you need to do one of:

          a) If you want to rollback the transaction as a result of throwing an exception, that exception needs to inherit from RuntimeException, and the "standard" exception you would thrown in your case is javax.ejb.EJBException which inherits from RuntimeException.

          b) If you throw an exception that does not extend RuntimeException (such as RemoteException, or any application exceptions defined by you etc.) you need to call setRollbackOnly() on the EJBContext of your EJB for rollback to take place.

          Why this worked with hsqldb I don't know.

          So your code should be (for a)):

          createFoo( ) {
          try {
          fooHome.create(); //create entity bean
          bar.validate(); // get a RemoteException
          catch (Exception e) {
          throw EJBException; //entity bean creation should rollback
          }


          Cheers,

          Kab

          • 2. Re: Rolback Problem
            Steven Harris Newbie

            I tried both of these suggestions - throwing an EJBException and calling setRollbackOnly on the EJBContext at the end of createFoo(). In both cases a TransactionRolledbackLocalException is generated, but the creation of the Foo entity bean is NOT rolled back in the db.

            So then I figured it might be something in the way that our mysql was configured. When I ran the mysql admin tool, I found that the table_type (ie., storage engine) comes up myisam which does not support transaction rollbacks. Remember that 4.X versions of mysql ship with innodb and bdb starage engines which do support transactions. Can this be the problem or does JBoss reset the storage engine? I suppose what I'm asking is whether there is some required or preferred mysql configuration for transactions or whether the JBoss just handles this.

            • 3. Re: Rolback Problem
              Steven Harris Newbie

              I'm still wondering about how JBoss handles setting mysql storage engines (if it does). By the way, using hypersonic, throwing a RemoteException from BarService.validate() causes the transaction initiated by validate's caller (FooService) to be marked for rollback (and it is rolled back). I can see it in the debugger when I look at the transaction in the EJBContext. It goes from having a status of 0 (ACTIVE) to 1 (MARKED_FOR_ROLLBACK). I realize this rollback should happen only if a RuntimeExceptions like EJBException is thrown, but what I see is what I see.

              • 4. Re: Rolback Problem
                Michael Forster Newbie

                What Database and Table system are you using,

                I had problems with a transaction not rolling back on MySQL using MyISAM tables, I switched to InnoDB and it works, Apparently MyISAM doesnt suppot ACID transactions but innoDB does.