Announcement Announcement Module
No announcement yet.
Spring transactions + CMT Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring transactions + CMT


    I have a POJO "handler" which is used basically as a session bean, ie it encapsulates business logic and needs some type of transaction scope for a certain method.

    My question is, can Spring's transcation management join the transactions of CMP beans operations (CMT) inside my handler which executes the business logic.

    For all i know it is not possible to join CMT and JTA java:comp/UserTransaction transactions....


  • #2
    Take a look at this post on the dev. mailing list.


    • #3
      Effectively, joining EJB CMT transactions is hardly ever a problem; for example, synchronizing O/R mapper resources with EJB CMT transactions. In the case of Hibernate, this even works without Spring transaction demarcation: HibernateTemplate will auto-detect the current JTA transaction, as long as a Hibernate TransactionManagerLookup is configured.

      The problems start to arise when you use EJB CMT at the facade level but Spring transaction demarcation for finer-grained components. Joining in via PROPAGATION_REQUIRED is usually not a problem, but suspending transactions via PROPAGATION_REQUIRES_NEW or PROPAGATION_NOT_SUPPORTED might be, as it implies suspending a transaction that has been started by EJB CMT.



      • #4

        I'm planning to use CMT.

        Is there any way to just use the rollback functionality ( based on Exception) provided by the Spring TransactionProxy (without propagation, isolation level and timeout) in the case of CMT?

        I'm thinking of creating a simple proxy with throws advise that uses configuration with a list of exceptions, Exception1, Exception2 etc..When any of these exceptions are thrown, i want to call "setRollbackOnly" method on the EJB context.

        The issue is the EJB context....Is there any easy way for the proxy to get access to the context ?



        • #5
          You could use a Spring TransactionProxyFactoryBean with PROPAGATION_REQUIRED (or PROPAGATION_MANDATORY to enforce an existing transaction) and JtaTransactionManager as backend: This will always join into existing EJB CMT transactions, marking the EJB transaction rollback-only according to the rollback rules.

          Spring will work with the JTA UserTransaction here, calling setRollbackOnly on it when necessary. While this is not directly covered by the J2EE spec, it works nicely on all known J2EE servers. Essentially, if your server allows JTA UserTransaction access within EJB CMT, then Spring transaction demarcation will work even with EJB transactions.

          If you prefer to use a custom AOP interceptor, I recommend to fetch the JTA UserTransaction from JNDI ("java:comp/UserTransaction") and call setRollbackOnly on it when necessary. Don't bother with getting access to the EJBContext; the UserTransaction will work too on all known J2EE servers, as indicated above.



          • #6
            The EJB spec explicitly mentions (in section 17.3.4)

            The enterprise beanís business methods, message listener methods, or ejbTimeout method must not
            attempt to obtain or use the javax.transaction.UserTransaction interface.
            Its good to hear that it works with all AppServers....I'm just worried that this may change with the next release of the AppServer (I'm sure Spring will figure out a solution if that happens.. ).

            Thanks for your quick response.