Hello everybody.
I've just started implementing Hibernate 3 (JPA) in a legacy project, and have a question about transactions.
I've already read this:
http://hibernate.org/42.html, but haven't found an answer there.
I've started reading the "Java Persistence with Hibernate" book, and the author there creates new entity manager for every transaction:
Code:
// Start EntityManagerFactory
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
// First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Message message = new Message("Hello World");
em.persist(message);
tx.commit();
em.close();
Is this the right approach, or can i reuse the entityManager as a singleton?
I have a singleton service object, which has an entityManager, and it is reused across all the service methods (CRUD).
What happens if, for example, two threads start using this singleton service simultaneously, i.e. they'll be using that one entityManager object simultaneously - won't it explode or something?