javaspringhibernatetransactionsjta

javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional


I don't understand what is the actual difference between annotations javax.transaction.Transactional and org.springframework.transaction.annotation.Transactional?

Is org.springframework.transaction.annotation.Transactional an extension of javax.transaction.Transactional or they have totally different meaning? When should each of them be used? Spring @Transactional in service layer and javax in DAO?

Thanks for answering.


Solution

  • Spring has defined its own Transactional annotation to make Spring bean methods transactional, years ago.

    Java EE 7 has finally done the same thing and now allows CDI bean methods to be transactional, in addition to EJB methods. So since Java EE 7, it also defines its own Transactional annotation (it obviously can't reuse the Spring one).

    In a Java EE 7 application, you'll use the Java EE annotation.

    In a Spring application, you'll use the Spring annotation.

    Their use is the same: informing the container (Java EE or Spring) that a method is transactional.