11 Replies Latest reply on May 8, 2007 4:01 PM by christian.bauer

    What is the difference between EJB and POJO transactions?

    grettke_spdr

      Reading the excellent Yuan/Heute book, he talks about the fact that transactions are enabled by default for all EJB 3 session beans. The transaction begins when the conversation begins, and ends when the conversation ends. It goes on to talk about POJO transactions, this is where it gets a bit confusing.

      POJOs have two transactions, the first spanning the beginning of the update model values, the second spans the render phase. Does this reflect the fact that the default conversation goes from the request to the full rendering of the next page?

      Using POJOs, is it correct to say that if you start a conversation, the transaction won't finish until that conversation is ended, just like with EJBs?

        • 1. Re: What is the difference between EJB and POJO transactions
          christian.bauer

          I haven't read the book, but the three basic transaction models are

          - EJB/CMT: You have EJB components (session beans, MDBs) and you call these EJB components from whatever other code. By default, a system transaction will begin when an EJB method is called and it will be committed when that method returns. The transaction propagates into other calls, e.g. if your EJB calls another EJB. You can use annotations or XML metadata to change this default transaction assembly. Seam can emulate this behavior with the Seam-specific and proprietary @Transactional annotation on POJOs, then Seam acts as the container that manages transaction boundaries.

          - JTA: You can call the JTA UserTransaction API in your application code somewhere and control the begin and end of a transaction programmatically.

          - Seam-managed JTA: Seam will call the JTA API for you automatically during the JSF lifecycle, meaning it will wrap two transactions around a JSF request. One transaction starts when the request hits the server and is committed when INVOKE APPLICATION is complete. The second transaction spans only the RENDER RESPONSE phase. This is an optimized and recommended pattern in a Seam/JSF application. All EJBs and indeed any POJOs you call during that request are participating in that transaction. To enable it, configure JSF with the TransactionalSeamPhaseListener.

          I recommend the last option, since it works with whatever components you execute and it is good from a scalability perspective. Also, you don't need to write any code or use any annotations with it.

          • 2. Re: What is the difference between EJB and POJO transactions
            christian.bauer

            Also note that system transactions and conversations are not related in a Seam application. These are independent contexts with a quite different scope. System transactions are short, conversations are potentially long-running and spanning several requests.

            • 3. Re: What is the difference between EJB and POJO transactions
              grettke_spdr

               

              "christian.bauer@jboss.com" wrote:
              Also note that system transactions and conversations are not related in a Seam application. These are independent contexts with a quite different scope. System transactions are short, conversations are potentially long-running and spanning several requests.


              Suppose that you have a conversation and you don't want to persist your changes until that conversation has ended. I believe this is the default behavior.

              How is this behavior related to transactions?

              • 4. Re: What is the difference between EJB and POJO transactions
                christian.bauer

                It's not the default behavior. The default behavior is to flush a persistence context (translation: dirty check objects, generate and execute SQL DML) when a transaction commits.

                If you use Hibernate as your JPA provider, you can enable FlushModeType.MANUAL when you begin a conversation, so that the persistence context bound to that conversation is only flushed when you call entityManager.flush().

                Please also read the Seam documentation, I'm basically quoting verbatim.

                • 5. Re: What is the difference between EJB and POJO transactions
                  statelessbean

                   

                  "christian.bauer@jboss.com" wrote:
                  It's not the default behavior. The default behavior is to flush a persistence context (translation: dirty check objects, generate and execute SQL DML) when a transaction commits.

                  If you use Hibernate as your JPA provider, you can enable FlushModeType.MANUAL when you begin a conversation, so that the persistence context bound to that conversation is only flushed when you call entityManager.flush().

                  Please also read the Seam documentation, I'm basically quoting verbatim.


                  What U mean Christian, in EJB U can also disable auto flush and set to manual.



                  in persistence.xml

                  • 6. Re: What is the difference between EJB and POJO transactions
                    christian.bauer

                    EJB 3.0 does not support disabling automatic flushing of the persistence context. This was one of the questionable changes forced into the specification by certain experts. The PC is always flushed automatically if there is a commit, the only way to prevent it is complicated auto commit mode.

                    Or the Hibernate setting that is also available in Seam. As soon as other JPA providers also add this essential feature, Seam will support that too.

                    • 7. Re: What is the difference between EJB and POJO transactions

                       

                      "christian.bauer@jboss.com" wrote:
                      I recommend the last option, since it works with whatever components you execute and it is good from a scalability perspective. Also, you don't need to write any code or use any annotations with it.


                      Now that I've read the bold part, I'm a bit confused about the relationship between @Transactional and TransactionalSeamPhaseListener (I thought that I need both). Do you mean, that if I use the TransactionalSeamPhaseListener, I don't need to mark my POJOs with @Transactional at all?

                      Thanks,

                      Alex

                      • 8. Re: What is the difference between EJB and POJO transactions
                        christian.bauer

                        Javadoc of @Transactional:

                        /**
                        * Specifies that the transaction propagation for a JavaBean
                        * component or method of a JavaBean component. JavaBean
                        * components have @Transactional(SUPPORTS) behavior
                        * is no @Transactional annotation is specified.
                        *
                        * @author Gavin King
                        */

                        SUPPORTS means that they will happily join whatever transaction is going on already, someone else started it. Which is the job of the TransactionalPhaseListener.

                        • 9. Re: What is the difference between EJB and POJO transactions
                          christian.bauer

                          Maybe what is also interesting is this:

                          /**
                          * The transaction propagation type.
                          *
                          * @return REQUIRED by default
                          */
                          TransactionPropagationType value() default TransactionPropagationType.REQUIRED;

                          So if you just put @Transactional() on a bean or a method, it means Seam will actually start a transaction if none is active. That wouldn't happen if you have the right phase listener.

                          • 10. Re: What is the difference between EJB and POJO transactions
                            grettke_spdr

                            I have found that one ought to have already read 'Java Persistence with Hibernate' before reading about Seam. One needs to understand Hibernate/JPA before reading the Seam book, or documentation.

                            • 11. Re: What is the difference between EJB and POJO transactions
                              christian.bauer

                              Actually, you only need to know a bit about EJB/CMT (EJB 2.x is good enough) and about JTA. Unfortunately, these days many developers think that "Spring transactions" are more important than the standards.