An Oracle blog about Java Technology

JTA 1.2 - It's not your Grandfather's Transactions anymore!

David Delabassee
Software Evangelist
Java Transaction API 1.2 aka JSR 907 (*), which is part of Java EE 7, adds 2 important new features : @Transactional and @TransactionScoped

Prior to JTA 1.2, transactions were delegated to EJBs. By doing that, the developer was shielded from doing low-level JTA/JTS transaction handling as all of this was transparently handeld by the EJB container. With CMT (Container-managed transaction), the default behaviour, the EJB container intercept calls so that it can interpose the required transaction plumbings. So this was, on one hand, handy because transactions were basically managed transparently but it also required, on the other hand, to use EJBs.

The new @Transactional annotation solve this by basically bringing the semantics of those EJB transaction attributes in managed beans without any dependencies on the EJB container. So the @Transactional annotation provides the ability to declaratively control transaction boundaries on managed beans. Under the hood, this capability is provided by a CDI interceptor that handle the necessary transaction plumbings (suspend, resume, commit, ...). In a nutshell, @Transactional bring the ease of CMT transactions to various Java EE Components (Servlet, JAX-RS, etc.) without requiring to use EJBs. 

The new @TransactionScoped annotation provides the ability to specify a standard CDI scope to define bean instances whose lifecycle is scoped to the currently active JTA transaction. This simple code driven post explain and demonstrate such a behaviour in action.

(*) For the curious who might wonder why some JSRs start in the 9xx range, there's a simple explanation for that. Those specifications are referred, in JCP parlance, as "Grandfathered JSRs" because those specifications have originally been developed outside of the JCP.

Join the discussion

Comments ( 4 )
  • guest Monday, February 17, 2014

    Would this be the first of several steps to incorporate the features provided by EJBs into CDI and move away from EJBs?

  • Reza Rahman Tuesday, February 18, 2014

    Yep - that's the basic idea. Do you have opinions on this? Is so, please share!

  • Deepu Roy Saturday, February 22, 2014

    Since Java EE 5, EJBs have become quite easy. But I still see people's eyes glazing over when I talk of EJBs. Anything to simplify it will be great. I'm not sure if everything that is done by EJBs can all be moved to CDI. I'm thinking of activation of MDBs , remote calls and QoS features(like failover) provided by the EJB containers.

    If we can bring the features available only in EJBs, to the rest of Java EE, that will be great. I think it will be interesting to see how this turns out.

    Personally, I would also like to see a standard way to bring CDI to Java SE. Now we either use implementation specific ways or use something like Spring.

  • Reza Rahman Monday, February 24, 2014

    I definitely agree with you and have forwarded your comments to the interested parties. Let's see what we can do in Java EE 8...

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.