The following code runs whenever I want to save any object. Everything seems to be working fine, but I don’t understand how it works!
EntityManager em = getEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
em.persist( ent );
userTransaction.commit();
The EntityManager above is one instance common to the entire application. After the start of the transaction; I'm just saying em.persist (entity) .. How hibernate knows this belongs to which transaction!
Suppose that in my application there are 10 simultaneous users and all 10 threads executing the above code. Thus, 10 independent transactions are created and completed. But all 10 different organizations I do not associate them with their respective transactions; since JPA can fix it!
Based on the answers; we have below; are we saying that we should have an EntityManager instance for the thread? Will this be a kill on the server! Should we combine these instances? Would this be tantamount to repeating the implementation of the connection pool?
source
share