persistence@glassfish.java.net

thread-safety

From: Daniel Cavalcanti <dhcavalcanti_at_gmail.com>
Date: Fri, 1 Sep 2006 11:13:09 -0400

Hello,

Quick question: Is the EntityManager thread safe?

For example,

class Foo {

    private EntityManagerFactory emf =
Persistence.createEntityManagerFactory("my-persistence-unit");
    private EntityManager manager = emf.createEntityManager();

    // method accessed by several concurrent threads
    public void foo() {

        try {
            manager.getTransaction().begin();
            // TODO: do somethinf
            manager.getTransaction().commit();
        } catch(Exception e) {
            manager.getTransaction().rollback();
        }

    }

}

thanks,
Daniel.