Monday, July 4, 2011

JPA joinTransaction Problem

When using JPA as your ORM in Java Web applications, you likely use transactions. As transaction-type you specify "RESOURCE_LOCAL" in your persistence.xml.

Suppose we have these two methods:
 public Object create(Object o){
em.getTransaction().begin();
em.persist(o);
em.getTransaction().commit();
return o;
}
and

public void delete(Object entity){
em.getTransaction().begin();
em.remove(entity);
em.getTransaction().commit();
}




The problem which occured when calling multiple times a new transaction was:

"joinTransaction has been called on a resource-local EntityManager"

That means, somehow a joined Transaction should be used, although we didn't configure JTA.
The source of this problem is, that we would call multiple times new transactions, which won't be executed after each other, but in parallel.

The solution was to set all methods in which a transaction is used to synchronized.
This leads to exact processing of the transactions in correct order.

 public synchronized Object create(Object o){
em.getTransaction().begin();
em.persist(o);
em.getTransaction().commit();
return o;
}
and

public synchronized void delete(Object entity){
em.getTransaction().begin();
em.remove(entity);
em.getTransaction().commit();
}

4 comments:

  1. In that case, sheet metal fabrication is an easy and sturdy different for small to medium-sized sheet metal enclosures to giant sheet metal cupboards over panel benders and folding machines. Specifying gap sizes, Vanity Fair Panties for Women alignment, and places in a sheet metal design is vital. Hole diameters lesser than the thickness of sheet metal could end in lengthy burnish, high punch loading, and extreme burr. Therefore, gap diameters must be greater than the sheet metal’s thickness. Holes and slots’ diameter ought to minimal of|no much less than} be as giant because the thickness of the sheet metal.

    ReplyDelete