8 Replies Latest reply on Nov 29, 2005 4:33 AM by jacekolszak

    Silly (maybe not ;) ) question about rollback CMT

    jacekolszak

      Hi everyone... I have a silly (maybe) question about how to roolback container managed transaction ? Now I'm using flush to prevent automaticaly send data to db after commit (so I can commit transaction after mulitple request from my web application). But how can I rollback this transaction ?

      Regards
      Jacek

        • 1. Re: Silly (maybe not ;) ) question about rollback CMT
          martinganserer

          Hello,

          you have to throw a new RuntimeException() to rollback a CM transaction!

          • 2. Re: Silly (maybe not ;) ) question about rollback CMT
            jacekolszak

             

            "martin ganserer" wrote:
            Hello,

            you have to throw a new RuntimeException() to rollback a CM transaction!


            Hm... yes.. I know... But is this only one possible way ? When i throw RuntimeException my CMB will be removed from pool :(

            Look at this... :

            @Statefull
            class Abc {
            public void method1(){
            // some operations on db
            }
            public void rollback(){
            throw new RuntimeException()
            }
            }

            I have a client which want to rollback last update for example. So it executes rollback method and then throw runtime exception. Do you know other possible ways ?

            Regards
            Jacek

            • 3. Re: Silly (maybe not ;) ) question about rollback CMT
              martinganserer

              Hi,

              I am not sure if it helps but what's about UserTransactions?
              By using them you don't have to throw a RuntimeException. You can or must use commit and rollback.

              • 4. Re: Silly (maybe not ;) ) question about rollback CMT
                jacekolszak

                 

                "martin ganserer" wrote:
                Hi,

                I am not sure if it helps but what's about UserTransactions?
                By using them you don't have to throw a RuntimeException. You can or must use commit and rollback.


                Thanks a lot. Now it working:
                @TransactionManagement(TransactionManagementType.BEAN)
                @Stateful
                public class TranBean implements TranLocal {
                
                 @PersistenceContext(type = PersistenceContextType.EXTENDED)
                 protected EntityManager em;
                
                 @Resource
                 protected SessionContext ctx;
                
                 public void test() {
                
                 em.getTransaction().begin();
                 TranEntity e = new TranEntity("Jasd");
                
                 em.persist(e);
                
                 }
                
                 public void test34() {
                 em.getTransaction().rollback();
                 }
                }
                



                • 5. Re: Silly (maybe not ;) ) question about rollback CMT
                  kabirkhan

                  You could keep using container managed transactions, and call SessionContext.setRollbackOnly()

                  • 6. Re: Silly (maybe not ;) ) question about rollback CMT
                    jacekolszak

                     

                    "kabir.khan@jboss.com" wrote:
                    You could keep using container managed transactions, and call SessionContext.setRollbackOnly()


                    Ok... so using this how can I rollback transaction? (only be exception ?)

                    One more question - is it possible to create save points in EJB 3?

                    Thanks for replies
                    Jacek


                    • 7. Re: Silly (maybe not ;) ) question about rollback CMT
                      bill.burke

                      save points? Sort of. You have to use extended persistence context and flushmode.NEVER. Basically, updates/inserts/deletes are held up until you call flush() manually.

                      setRollbackOnly just marks the transaction to be rolled back. YOu can also throw an Runtime, Remote, or an exception marked with @ApplicationException(rollback=true) to automatically rollback the tx.

                      • 8. Re: Silly (maybe not ;) ) question about rollback CMT
                        jacekolszak

                        Ok... I've created bean which can begin, commit and rollback transaction. But now I'm trying to use other CMB from it.

                        em.getTransaction().begin();
                        TypDzial typ=new TypDzial();
                        typ.setNazwa("zooo");
                        tlocal.add(typ);
                        em.getTransaction().commit();
                        


                        p.s. TypDzial is a CMB

                        But this code gives me an exception:
                        java.sql.SQLException: You cannot commit with autocommit set!
                        


                        So.. I've set up autocommit property in persistence.xml to true - but this gives me another error -
                        autocommit breaks ejb 3


                        Is there any way to use stateless CMB inside from BMP and not allow CMB to commit the transaction automatically ? Or i must use statefull CMB and FlushMode.NEVER ?