Skip to main content

EntityTransaction vs UserTransaction

2 replies [Last post]
Joined: 2008-06-11

I am developing a Java SE project using JPA. Because I can't use any container-managed or bean-managed transaction (not sure about bean-managed here), anyways, I decided to client-controlled approach and I found both the EntityTransaction and UserTransaction would work in my codes. But what is the difference between them?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-06-11

here is something I found in the JPA faq

Q. Can I use a resource-local EntityManager in my web application?

Yes, you can use both resource-local as well as JTA EntityManager in your web application. The same applies to an EJB application as well. But, we recommend you use a JTA EntityManager in your web and EJB applications and [b]use a resource-local EntityManager in your Java SE or Java EE client application. [/b]

There is no reason given though.

Joined: 2009-02-22

Here is what "Enterprise JavaBeans, 3.0" by Bill Burke, Richard Monson-Haefel, Print ISBN-10: 0-596-00978-X says:


5.6. Resource Local Transactions

An entity manager's persistence context is usually managed by a JTA transaction in a Java EE environment. [b]When running in a non-Java EE environment, JTA is not available, so the Java Persistence API specification has provided a transaction-like API through
the EntityTransaction interface[/b]. You can obtain access to an Entity-Transaction through the EntityManager.getTransaction( ) operation:
public interface EntityTransaction {
public void begin( );
public void commit( );
public void rollback( );
public boolean isActive( );
The begin( ) method throws an IllegalStateException if there is already an active EntityTransaction. The commit( ) and rollback( )
methods throw an IllegalState-Exception if there is not an active transaction.

[b]It is possible for a vendor implementation to allow the use of the EntityTransaction API
Java EE environment, but the application developer is encouraged to use JTA.[/b]