which transaction manager should I use (JTA vs JPA)?

grep picture grep · Oct 21, 2014 · Viewed 20k times · Source

I have spring 4 application. At the moment I use JpatransactionManager.

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Could you tell me how to choose transaction managers?

For instance, when should I use jta transaction manager and when jpa, and what benefit and disadvantages does they have?

And Is I know I have 2 way to work in Spring. First is JPA way and the second Hibernate way. first one includes java standard annotations and standard api and the second is hibenrate implementation. If i need to use JTA, I must use hibernate and not JPA, does not it?

Answer

Georgy Gobozov picture Georgy Gobozov · Oct 21, 2014

If you want to delegate managed transactions to your Application Server and handle complex transactions across multiple resources you need to use the JtaTransactionManager, but I believe it is not needed in most cases. Read this for more information http://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/html/transaction.html

Do you need an application server for transaction management?

The Spring Framework’s transaction management support changes traditional rules as to when an enterprise Java application requires an application server.

In particular, you do not need an application server simply for declarative transactions through EJBs. In fact, even if your application server has powerful JTA capabilities, you may decide that the Spring Framework’s declarative transactions offer more power and a more productive programming model than EJB CMT.

Typically you need an application server’s JTA capability only if your application needs to handle transactions across multiple resources, which is not a requirement for many applications. Many high-end applications use a single, highly scalable database (such as Oracle RAC) instead. Standalone transaction managers such as Atomikos Transactions and JOTM are other options. Of course, you may need other application server capabilities such as Java Message Service (JMS) and Java EE Connector Architecture (JCA).

The Spring Framework gives you the choice of when to scale your application to a fully loaded application server. Gone are the days when the only alternative to using EJB CMT or JTA was to write code with local transactions such as those on JDBC connections, and face a hefty rework if you need that code to run within global, container-managed transactions. With the Spring Framework, only some of the bean definitions in your configuration file, rather than your code, need to change.