playframework/playframework — pull request #3388 by ssokolenko (2014-09-09 13:23:38+00:00)
Title: Add support for nested JPA.withTransaction calls

Hi,

we're facing issues with nested JPA.withTransaction calls which result in well known RuntimeException("No EntityManager bound to this thread...").
Here is simplified use-case:

@Transactional
public static Result index() {

JPA.em().isOpen(); // true

// 2nd call to JPA.withTransaction binds new EntityManager to current thread (erasing previously bound EntityManager)
JPA.withTransaction(() -> {
JPA.em().isOpen(); // true
});
// after JPA.withTransaction call there is no EntityManager bound anymore

JPA.em().isOpen(); // RuntimeException: No EntityManager bound to this thread.

return ok();
}

The obvious solution is to keep current EntityManager while new EntityManager is being bound and then rebind it when new EM is no longer used (i.e. at the end of JPA.withTransaction).

Are there any potential or known issues with this solution?

--

insign - simply e-business
http://www.insign.ch