14 Replies Latest reply on Aug 17, 2007 7:36 PM by Carlo de Wolf

    need help for Transaction Attribute Types

    paata lominadze Master

      good day,
      i need something like that :

      @Stateful
      @Remote(TestFasade.class)
      public class TestFasadeBean implements TestFasade {
      
       @PersistenceContext(unitName = "Oracle")
       private EntityManager oracleManager;
      
       @TransactionAttribute(??????????????)
       public void create(Test test) {
       try {
       System.out.println("start persist");
       oracleManager.persist(test);
       System.out.println("end persist");
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
       @TransactionAttribute(??????????????)
       public void comit() {
       try {
       System.out.println("start comit");
       oracleManager.flush();
       System.out.println("end comit");
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
      }
      

      i want that first method "create" must not be flushed into database before i call commit.
      is it possible, and i don't want class variable initialization.
      is it possible by transaction types or something like that ?


      any idea will be appreciated.



      _______________________
      Regards
      Paata,

        • 1. Re: need help for Transaction Attribute Types
          Andrew Rubinger Master

          Looks like you'd just like an Extended Persistence Context, so that your EM isn't flushed to the DB automatically at JTA Commit.

          http://docs.jboss.org/ejb3/app-server/tutorial/extended_pc/extended.html

          S,
          ALR

          • 2. Re: need help for Transaction Attribute Types
            paata lominadze Master

             

            "ALRubinger" wrote:
            Looks like you'd just like an Extended Persistence Context, so that your EM isn't flushed to the DB automatically at JTA Commit.

            http://docs.jboss.org/ejb3/app-server/tutorial/extended_pc/extended.html

            S,
            ALR


            Great Thanks for your posts,
            i did what you say but problems is not resolved, my example now looks like :
            @Stateful
            @Remote(TestFasade.class)
            public class TestFasadeBean implements TestFasade {
            
             @PersistenceContext(type = PersistenceContextType.EXTENDED)
             EntityManager oracleManager;
            
             Test test;
            
             public Long create() {
             try {
             System.out.println("start persist");
             test = new Test();
             test.setAge(21L);
             test.setName("adsdasd");
             test.setSurname("asdasd");
             oracleManager.persist(test);
             System.out.println("end persist");
             Long ret = test.getId();
             return ret;
             } catch (Exception e) {
             e.printStackTrace();
             return 0L;
             }
             }
             public void comit() {
             try {
             System.out.println("start comit");
             oracleManager.flush();
             System.out.println("start end");
             } catch (Exception e) {
             e.printStackTrace();
             }
             }
            }
            


            and after call create method information is flushed into database :( did i something incorrect ?
            or maybe i have some bad versions of enviroment :
            AS : JBoss AS 4.2.1.GA with ejb 3 included



            • 3. Re: need help for Transaction Attribute Types
              paata lominadze Master

              any idea???


              i need it so much :(.

              did i something incorrect ? :(


              Thanks In Advance.

              • 4. Re: need help for Transaction Attribute Types
                J-C jc Apprentice

                Maybe you can try to use a bean manage transaction:

                @TransactionManagement(TransactionManagementType.BEAN)
                


                Then you will have to begin and commit transaction by yourself.

                • 5. Re: need help for Transaction Attribute Types
                  paata lominadze Master

                   

                  "jc7442" wrote:
                  Maybe you can try to use a bean manage transaction:
                  @TransactionManagement(TransactionManagementType.BEAN)
                  


                  Then you will have to begin and commit transaction by yourself.


                  • 6. Re: need help for Transaction Attribute Types
                    paata lominadze Master

                    yes it is possible to do this, but i don't want bean managed transactions.

                    is it possible what i did ?
                    i think that extended persistence context disables automatic flash on database,

                    maybe i will use bean managed transaction if i will not resolve this problem.


                    Thanks jc7442 anyway.


                    • 7. Re: need help for Transaction Attribute Types
                      Oskar Carlstedt Novice

                      Hi All!

                      I haven't read everything in this thread but a good idea might be to take a look in the Java EE tutorial. It has a whole chapter discussing transactions.

                      Whole tutorial: http://java.sun.com/javaee/5/docs/tutorial/doc/
                      Transactions: http://java.sun.com/javaee/5/docs/tutorial/doc/Transaction.html#wp79663


                      Kind regards
                      /Oskar

                      • 8. Re: need help for Transaction Attribute Types
                        Andrew Rubinger Master

                        Sorry everyone for the delayed response, I must not have set this Thread to send emails when updated.

                        The Extended Persistence Context is 1/2 the equation, because if your EM is flushing automatically on any select/update/insert/delete events or when the Tx is committed, you're still going to have the same problems you're seeing.

                        @PersistenceContext(type=PersistenceContextType.EXTENDED, properties=@PersistenceProperty(name="org.hibernate.flushMode", value="MANUAL"))
                        EntityManager oracleManager;


                        ...and then flush the EM only when you want to; give that a spin?

                        Disclaimer: Haven't personally done this in a couple months; the code samples may be off but the theory should be spot-on. Use an Extended Persistence Context, and ensure that you are manually flushing it, and it's not getting flushed automatically by the container.

                        Mind reporting back your results? Curious to see if this path will work for you, and if I'm correct in assuming that the container is still flushing your EM for you.

                        S,
                        ALR

                        • 9. Re: need help for Transaction Attribute Types
                          paata lominadze Master

                          Hello Ta
                          Great Thanks for your post, i tried to this for couple of moth and could not resolve this problem.
                          and ok, i did what you say i got another problem when i tried to persist in one method whats ok, there is no information persisted in database, but when i tried to flush on other method i got an error : javax.persistence.TransactionRequiredException.
                          my example now looks like :

                          package com.magti.businesslayer.ejb3Fasade;
                          
                          import javax.ejb.EJB;
                          import javax.ejb.Remote;
                          import javax.ejb.Remove;
                          import javax.ejb.Stateful;
                          import javax.ejb.Stateless;
                          import javax.ejb.TransactionAttribute;
                          import javax.ejb.TransactionAttributeType;
                          import javax.ejb.TransactionManagement;
                          import javax.ejb.TransactionManagementType;
                          import javax.persistence.EntityManager;
                          import javax.persistence.PersistenceContext;
                          import javax.persistence.PersistenceContextType;
                          import javax.persistence.PersistenceProperty;
                          
                          import com.magti.businesslayer.ejb3Fasade.TestFasade;
                          import com.magti.businesslayer.ejb3entity.oracle.Test;
                          
                          @Stateful
                          @Remote(TestFasade.class)
                          @TransactionManagement(TransactionManagementType.BEAN)
                          public class TestFasadeBean implements TestFasade {
                          
                           @PersistenceContext(
                           type = PersistenceContextType.EXTENDED,
                           properties = @PersistenceProperty(
                           name = "org.hibernate.flushMode",
                           value = "MANUAL"))
                           EntityManager oracleManager;
                           Test test;
                          
                           public Long create() {
                           try {
                           test = new Test();
                           test.setAge(21L);
                           test.setName("adsdasd");
                           test.setSurname("asdasd");
                           oracleManager.persist(test);
                           return test.getId();
                           } catch (Exception e) {
                           e.printStackTrace();
                           return 0L;
                           }
                           }
                          
                           public void comit() {
                           try {
                           oracleManager.flush();
                           } catch (Exception e) {
                           e.printStackTrace();
                           }
                           }
                          }
                          


                          when i tried to flush, i got this exception : javax.persistence.TransactionRequiredException.
                          i tried to use @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
                          and
                          @TransactionAttribute(TransactionAttributeType.SUPPORTS)
                          but it could not resolve problem :(

                          have any idea ?

                          Thanks again ALRubinger In advance.

                          • 10. Re: need help for Transaction Attribute Types
                            Carlo de Wolf Master

                            Please try the following:

                            @Stateful
                            @Remote(TestFasade.class)
                            public class TestFasadeBean implements TestFasade {
                            
                             @PersistenceContext(type = PersistenceContextType.EXTENDED)
                             EntityManager oracleManager;
                             Test test;
                            
                             @TransactionAttribute(NEVER)
                             public Long create() {
                             try {
                             test = new Test();
                             test.setAge(21L);
                             test.setName("adsdasd");
                             test.setSurname("asdasd");
                             oracleManager.persist(test);
                             return test.getId();
                             } catch (Exception e) {
                             e.printStackTrace();
                             return 0L;
                             }
                             }
                            
                             @TransactionAttribute(REQUIRED)
                             public void comit() {
                             try {
                             oracleManager.flush();
                             } catch (Exception e) {
                             e.printStackTrace();
                             }
                             }
                            }


                            • 11. Re: need help for Transaction Attribute Types
                              paata lominadze Master

                              i could not resolve this problem yet:(


                              • 12. Re: need help for Transaction Attribute Types
                                J-C jc Apprentice

                                When you use bean manage transaction, you have to begin and commit transaction by yourself:

                                @Resource UserTransaction ut;
                                ...
                                ut.begin();
                                ...
                                ut.commit();


                                • 13. Re: need help for Transaction Attribute Types
                                  paata lominadze Master

                                   

                                  "wolfc" wrote:
                                  Please try the following:


                                  hello wolfc, i did what you say, but on flush i got this error :
                                  10:08:19,946 ERROR [STDERR] javax.persistence.TransactionRequiredException: no transaction is in progress
                                  10:08:19,947 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:293)
                                  10:08:19,947 ERROR [STDERR] at org.jboss.ejb3.entity.ExtendedEntityManager.flush(ExtendedEntityManager.java:121)
                                  10:08:19,947 ERROR [STDERR] at com.magti.businesslayer.ejb3Fasade.TestFasadeBean.comit(TestFasadeBean.java:47)
                                  10:08:19,947 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                                  10:08:19,947 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                                  10:08:19,947 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                                  10:08:19,947 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
                                  


                                  i don't know what to do any more.


                                  hi jc7442,

                                  @Resource UserTransaction ut;
                                  ...
                                  ut.begin();
                                  ...
                                  ut.commit();

                                  i know bean managed transaction, i don't want it, if it is possible.

                                  if i will not resolve this problem by container managed transaction i will choose bean managed.



                                  Thanks all in advance.

                                  • 14. Re: need help for Transaction Attribute Types
                                    Carlo de Wolf Master

                                    Post the entire stack trace please.