1 2 3 Previous Next 33 Replies Latest reply on Dec 12, 2006 2:31 PM by gavin.king Go to original post
      • 15. Re: EntityManager: For newbies or to close gaps in your know
        christian.bauer

        About EntityManager.getDelegate(): http://jira.jboss.com/jira/browse/EJBTHREE-687

        This means that this code will soon work everywhere:

        Session s = (Session) em.getDelegate();

        • 16. Re: EntityManager: For newbies or to close gaps in your know
          gavin.king

          You can't flush outside of a transaction. That would be Bad :-)

          So enable transactions at least around the method that flushes.

          • 17. Re: EntityManager: For newbies or to close gaps in your know
            y_zl

            Hi! bfo81,

            great!

            I have one question as following:

            Suppose I have two tables

            user(id, name)
            userlog(id,userid,content)
            


            I'd like to create a new user and write the log at the same time.

            Suppose I use extended persistent manager.

            I have the following code:

            @In @Out user;
            
            @PersistenceContext(type=EXTENDED)
            private EntityManager em;
            
            public void new_user()
            {
            
            user.setName("user");
            Userlog userlog=new Userlog();
            userlog.setContent("register");
            userlog.setUser(user);
            
            -----
            
            }
            


            the question is:
            1)Do I need to em.persist to save user and userlog?
            2)If it is needed. Which one is first? user or userlog

            thanks in advance

            • 18. Re: EntityManager: For newbies or to close gaps in your know
              y_zl

              Anybody can help to answer the above question?

              • 19. Re: EntityManager: For newbies or to close gaps in your know
                pmuir

                This is really a jpa/hibernate question (and explained in the manuals/examples). Anyway, you would need to post your entities not the tables!!

                • 20. Re: EntityManager: For newbies or to close gaps in your know
                  toni

                  Hi bfo81,

                  I enjoyed reading your post - well done. You asked about how to run into a LIE:


                  Seam Managed Persistence Context
                  I must confess that I never ran into LazyInitializationExceptions (abbr. LIE). I use an Extended Persistence Context in a Conversation scoped Stateful Session Bean. And when accessing related entities the EntityManager lazily fetches them without complaint. Don't ask me why. Maybe you can add a case where the LIE occurs.


                  I just wrote a post, which seems to be closely connected to the issues which touch upon and exposes a LIE.

                  I think you will enjoy reading it, even though its a bit lengthy.

                  http://www.jboss.com/index.html?module=bb&op=viewtopic&t=96021

                  • 21. Re: EntityManager: For newbies or to close gaps in your know
                    toni

                    @bf081

                    I found another thing you forgot to mention:

                    You even don't have to call the merge() method since the entities never get DETACHED. So you might ask what is the "normal" (transaction scoped) persistence context good for?


                    One of the disatvantages of using an extended persistent context is that the transaction lasts for a long time, which is maybe not what you want.

                    If you go into the extreme case, then it might span until the user finally logs out. If during this time an error occurs, then all other actions will be undone.

                    Which makes me wonder, when and how a transaction finally is commited in an extended persistent context? I mean which of the statefull session beans do we have to destroy, if many are involved in the transaction.

                    I guess by ending the conversation using @End, wer surely destroy all SFSB and the current extended persistent manager, thereby commiting the current transaction. Is that correct? Maybe somebody can contribute to what I have said?


                    @justinwalsh

                    I have a question regarding:

                    The SMPC takes the concept of an EPC a little further by integrating the JSF lifecycle with that of the stateful component. Effectively what the SMPC achieves is the removal of LIE when traversing objects outside the scope of your session bean (there's more to it than that - but for now that will do). So in your JSF, if you traversed the customer --> addresses relationship (after outjecting a customer object) you'll avoid the LIE issue.


                    Does that mean that the the Entity will still be attached to the persistent manager or will it be unattached, but modified in such a way that calls to uninitialzed values will not generate LIE's?

                    • 22. Re: EntityManager: For newbies or to close gaps in your know
                      gavin.king

                       

                      One of the disatvantages of using an extended persistent context is that the transaction lasts for a long time, which is maybe not what you want.


                      This is DEFINITELY not true.

                      Persistence context != transaction context. That is why there are two different things.

                      • 23. Re: EntityManager: For newbies or to close gaps in your know
                        toni

                         

                        Persistence context != transaction context. That is why there are two different things.


                        Ok, so objects may be attached to the Entity Manager and not participate in a transaction.

                        But how come that changes made to attached objects are reflected in the database?

                        And when does a transaction start and end when we are using a persistent entity manager?

                        • 24. Re: EntityManager: For newbies or to close gaps in your know
                          gavin.king

                          What I am saying is that the extended persistence context spans multiple transactions. Typically one transaction per request.

                          • 25. Re: EntityManager: For newbies or to close gaps in your know
                            toni

                            Are those transactions nested or are they idenpendet of each other?

                            • 26. Transactions in Perstistence Context
                              bfo81

                              Sorry for my late answer :).

                              Those transactions are independent from each other. But step by step:

                              During the existence of an Extended Persistence Context there can be multiple Java Transactions (JTA).

                              And each Java Transaction will encapsulate write accesses to the database in a database transaction, if necessary (but I'm not sure about that). Don't know what about read accesses ;).

                              Usually (i.e. in a Stateless or Stateful Bean with default transaction stuff) changes to entities get written to the database at the end of a method. And those changes are encapsulated in a transaction (which is created by the EJB framework).

                              You might even write changes explicitely to the database with em.flush() before a method ends. Then the SQL-Statements are created immediately and submitted to the database, so you can react to exceptions if necessary (e.g. when violating db constraints).

                              But I must confess, I am not 100% sure at which point in time a transaction starts and ends. My imagination is like this:
                              - before methode starts: em.getTransaction().start().
                              - after method ends: em.getTransaction().commit() (or rollback() if setRollbackOnly() was called or a RuntimeException occured).
                              - and whenever there are Java transactions running, a database transaction is started before the first db access (e.g. set autocommit=0 in MySQL or begin tran in MS SQL). And after writing was finished, a database commit (or rollback) is called.

                              GAVIN? Could you take as on a ride into the deep misterys of the Java Transaction API and its interactions with the EntityManager here? Only a short comment wether my findings are correct :)

                              Plus some more questions I have:

                              - If a rollback was set (setRollbackOnly() or RuntimeException) there won't be any SQL statements sent to the database at the end of a method. If you called em.flush() before, then statements were already created and only then you need a database rollback, right?

                              - Ist there a SQL commit called at the end of em.flush()? If yes, and if there are e.g. constraints violated, the database rolls back the changed stuff automatically and throws an exception, right?

                              - How does the EntityManager know which entities changed? Does he hold a copy of every managed entity in memory and compare it to the actual entities, field by field?!? Or are there some interceptors doing some observer stuff on the entity?

                              • 27. Re: EntityManager: For newbies or to close gaps in your know
                                toni

                                So you and gavin are basically saying that the outcome of any one of those transactions does not determine the success or failure of any of the other transactions? I just want make sure I get this right.

                                But what do I do if I want to span the transaction over several requests? Now I'm getting confused. Isn't it that only in a non extended persistent context the transaction starts and ends with the mehtod invocation, whereas in an extended context the transaction lasts for the livetime of the SFSB?

                                • 28. Re: EntityManager: For newbies or to close gaps in your know
                                  bfo81

                                  The Extended Persistence Context only says, that the entities "live" in the managed state for multiple method invocations, in opposition to the "normal" Persistence Context, in which the entities get detached after a method has finished.

                                  Transactions are completely indepent from that. And always remember that transactions are an isolated piece of work. Just think of it as football (soccer, not american football, please *g*) matches. Losing one match doesn't mean you can't win the next ;).

                                  There's just one thing you should bare in mind: after a rollback the involved entities might be in a dirty state.

                                  Now, to your question: If you want to have a transaction span multiple method invocations I'd say that you should switch to user managed transactions (instead of container managed ones - there's a certain EJB annotation for that, but at the moment I don't know which one). Then you can start a transaction and finish it later, in another method. But I never did this so don't trust me without testing that yourself ;).

                                  But, I can hear Gavin screaming, "children, don't do this at home" ;). Whenever you have a transaction opened for a longer period, you might have serious problems with scalability. Multiple open transactions may block each other. In a clean software architecture transactions are lasting only a short time.

                                  • 29. Re: EntityManager: For newbies or to close gaps in your know
                                    gavin.king

                                     

                                    So you and gavin are basically saying that the outcome of any one of those transactions does not determine the success or failure of any of the other transactions?


                                    Yes.


                                    But what do I do if I want to span the transaction over several requests?


                                    What you are really asking is "how can I have an atomic conversation". Use @Begin(flushMode=MANUAL), and explicitly flush() when you want the changes to be persistent.

                                    Isn't it that only in a non extended persistent context the transaction starts and ends with the mehtod invocation, whereas in an extended context the transaction lasts for the livetime of the SFSB?


                                    Definitely not. Transactions held open across a user interaction are almost always a really bad idea.