gwt-guice JPA, inject EntityManager thread safe?

198 views
Skip to first unread message

Yan

unread,
Feb 8, 2013, 8:39:06 PM2/8/13
to google-we...@googlegroups.com
Hi there, 

I need to deploy the app. to a J2EE container using JTA. So the annotation @Transactional does not work because it is using EntityTransaction, not compatible with JTA. 

To work around it, I inject UserTransaction which I acquire by JNDI lookup.   Is the following code thread-safe?  Since both EntityManager and UserTransaction are instance variables but the methods are run by multiple threads.

How can I make it thread-safe but still be able to use Injection?

Thanks,
Yan

import com.google.inject.persist.Transactional;
import javax.persistence.EntityManager;
 
public class MyService {
       
@Inject EntityManager em;
@Inject UserTransaction // self-defined by JNDI look up 

 
      public void createNewPerson() {
                em
.persist(new Person(...));
       
} 
        public void createB() {
                em.persist(,,,)
        }

}

Thomas Broyer

unread,
Feb 9, 2013, 6:38:32 AM2/9/13
to google-we...@googlegroups.com
Because a service is a quasi-singleton and you want your EntityManager request-scoped (session-per-request), you should inject a Provider<EntityManager> (I can't tell for UserTransaction as I don't know how it works)
Reply all
Reply to author
Forward
0 new messages