2 Replies Latest reply on Dec 13, 2005 3:40 AM by jacekolszak

    One more question about transactions

    jacekolszak

      Last few days I was thinking about transactions which can be manipulated by the client. But till now I've no good solution. I want to have a bean which transactions can be started, rollback and commit by the client. I want to have a solution, where client can rollback only last transaction (not everything).

      I've created simple stateful bean:

      @TransactionManagement(TransactionManagementType.CONTAINER)
      @FlushMode(FlushModeType.NEVER)
      @Stateful
      public class TransactionsStateful implements TransactionsStatefulLocal {
      
       @PersistenceContext(unitName = "ksiegi", type = PersistenceContextType.TRANSACTION)
       protected EntityManager em;
      
       @Resource
       protected SessionContext ctx;
      
      
       public void commit() {
       em.flush();
       }
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public void rollback() {
       throw new RamzesException("Rollback");
       }
      
      
       private short tmpId = 0;
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       public void a() {
       TypDzial typ = new TypDzial();
       typ.setNazwa("zooo32");
       em.persist(typ);
       tmpId = typ.getId();
       }
      }
      


      I run this code using:

      local.a();
      try {
      
       local.rollback();
      } catch (RamzesException e) {
      }
      local.commit();
      

      But the data was stored, even though I've thrown an exception using rollback method :( Yes, I know that I've catched the exception but client will be executing methods this way - he can rollback only last transaction, not all.

      Is there any chance to rollback only last transaction?

        • 1. Re: One more question about transactions
          bill.burke

          #1) I don't think @FlushMode on the class will work. Do EntityManager.setFlushMode(NEVER) instead.

          #2) Is the RamzesException a RuntimeException or RemoteException or annotated with @ApplicationException(rollback=true)?

          #3) FYI, FlushMode.NEVER will be removed in the final version of the specification. Instead, you can get the same behavior by doing EM operations outside of a JTA transaction. If you don't like this (I don't) please bitch directly to Mike Keith <michael.keith@oracle.com> please and also ejb3-feedback@sun.com.

          #4) @FlushMode is dissappearing I believe as well.

          • 2. Re: One more question about transactions
            jacekolszak

             

            "bill.burke@jboss.com" wrote:
            #1) I don't think @FlushMode on the class will work. Do EntityManager.setFlushMode(NEVER) instead.


            Ok.. I changed this. But my example still doesn't work :( I mean that data aren't rollback.


            #2) Is the RamzesException a RuntimeException or RemoteException or annotated with @ApplicationException(rollback=true)?

            Yes, it is annotated with @ApplicationException(rollback=true)