3 Replies Latest reply on Dec 20, 2012 6:12 AM by Martin Kouba

    About using <transaction:ejb-transaction/> in components.xml

    M M Islam Chisty Newbie

      Hello,

       

      I have a confusion regarding the <transaction:ejb-transaction/> tag in compoenents.xml. Why and when is it necessary?

       

      So far after doing some goole search, I have understood the followings:

       

       

      Point 1


      According to Dan Allen (Author of Seam in Action),

       

      <<

      If your components rely on container-managed transactions, available in an EJB environment, Seam works alongside the UserTransaction on the EJBContext to capture transaction synchronization events. Since Seam isn’t in control of container-managed transactions, it’s necessary to register Seam to be notified of the transaction boundaries:

       

      <tx:ejb-transaction/>

       

      This configuration activates a stateful session bean that implements the Session-Synchronization interface to capture transaction events from the EJB container. This SFSB is packaged in jboss-seam.jar, which must be bundled in the EAR. From this SFSB, Seam passes on the following two events to other Seam components using its own internal event mechanism, the same events Seam raises for Seam-managed transactions:

      • org.jboss.seam.beforeTransactionCompletion
      • org.jboss.seam.afterTransactionCompletion (raised on commit or rollback)

      >>

       

       

      [Source: Seam in Action (copyright @2009) ; Chapter 9; Page 373]

       

       

       

      Point 2

       

      I looked into the API documentation of the following classes:

      • org.jboss.seam.transaction.EjbTransaction
      • org.jboss.seam.transaction.EjbSynchronizations
      • javax.ejb.SessionSynchronization

       

       

      About org.jboss.seam.transaction.EjbTransaction:

       

      <<

      Dummy component that lets us install the EjbSynchronizations via the tag transaction:ejb-transaction

      >>

      [Ref: http://docs.jboss.org/seam/2.0.0.GA/api/]

       

       

      About org.jboss.seam.transaction.EjbSynchronizations:

       

      <<

      Receives JTA transaction completion notifications from the EJB container, and passes them on to the registered Synchronizations. This implementation is fully aware of container managed transactions and is able to register Synchronizations for the container transaction.

      >>

      [Ref: http://docs.jboss.org/seam/2.0.0.GA/api/org/jboss/seam/transaction/EjbSynchronizations.html]

       

       

      About javax.ejb.SessionSynchronization (the super interface of org.jboss.seam.transaction.EjbSynchronizations):

       

      <<

      The SessionSynchronization interface allows a stateful session bean instance to be notified by its container of transaction boundaries.

       

      Only a stateful session bean with container-managed transaction demarcation can receive session synchronization notifications. Other bean types must not implement the SessionSynchronization interface or use the session synchronization annotations.

       

      A stateful session bean class is not required to implement this interface.

      >>

       

      [Ref: http://docs.oracle.com/javaee/5/api/javax/ejb/SessionSynchronization.html]

       

       

       

      From the above statements/docs as mentioned, is it okay to conclude that,

      - if we only use POJO (Seam compoents with @Name annotation) and/or stateless EJBs (beans with bean with @Stateless annotation)  and,

      - if we DO NOT use any SFSB (i.e. no bean with @Stateful annotation),

      then we do not need to use <transaction:ejb-transaction/> tag in the components.xml?

       

       

      Any feedback would be appreciable.

       

       

      With Thanks,

      ... Chisty

        • 1. Re: About using <transaction:ejb-transaction/> in components.xml
          Martin Kouba Master

          Hi,

          I don't think so. I'd say EjbSynchronizations component is required for stateless session beans (SLSB) as well. So if you work with EJB session beans and CMT, use the tag <tx:ejb-transaction/> in your components.xml.

           

          Note that the SessionSynchronization javadoc does not say anything about SFSBs (check the link you provide :-):

          "The SessionSynchronization interface allows a session Bean instance to be notified by its container of transaction boundaries."

          1 of 1 people found this helpful
          • 2. Re: About using <transaction:ejb-transaction/> in components.xml
            M M Islam Chisty Newbie

            Thanks for your feedback. Sorry about providing a wrong URL. Two versions are there: one for JEE5 and one for JEE6 (instead of referring to the latest URL, I pointed link to the older URL)

             

            Here are those:

            http://docs.oracle.com/javaee/5/api/javax/ejb/SessionSynchronization.html (JEE 5 API)

             

            which says,

            <<

            The SessionSynchronization interface allows a session Bean instance to be notified by its container of transaction boundaries.

             

            An session Bean class is not required to implement this interface. A session Bean class should implement this interface only if it wishes to synchronize its state with the transactions.

            >>

             

            And the other one:

            http://docs.oracle.com/javaee/6/api/javax/ejb/SessionSynchronization.html

             

            which says,

            <<

            The SessionSynchronization interface allows a stateful session bean instance to be notified by its container of transaction boundaries.

             

            Only a stateful session bean with container-managed transaction demarcation can receive session synchronization notifications. Other bean types must not implement the SessionSynchronization interface or use the session synchronization annotations.

             

            A stateful session bean class is not required to implement this interface.

            >>

             

            Intead of modifying the URL in the actual article, I'm providing the links here in this reply.

            • 3. Re: About using <transaction:ejb-transaction/> in components.xml
              Martin Kouba Master

              Yes, you're right, the EJB spec (both 3.0 and 3.1 versions) states:

              "Only a stateful session bean with container-managed transaction demarcation can receive SessionSynchronization notifications. Stateless session beans and Singleton session beans must not implement the SessionSynchronization interface or use the session synchronization annotations."

               

              However EjbSynchronizations is @Stateful so it's ok :-).

               

              I still think EjbSynchronizations is required for SLSBs as well, because it's used to sync non-EJB Seam components with CMT, e.g. to close Hibernate session afterCompletion in org.jboss.seam.persistence.ManagedHibernateSession. But I'm not 100% sure.