java - Running multiple JPA transactions in parallel -
i have 2 (or more) java threads creating, updating , deleting entities mysql database using jpa. achieve have persistencelayer class creating entitymanager , providing save, update , delete methods entities looking like:
public void saveentity(entity entity) { manager.gettransaction().begin(); manager.persist(entity); manager.gettransaction().commit(); } public void saveentity2(entity2 entity) { manager.gettransaction().begin(); manager.persist(entity); manager.gettransaction().commit(); }
if 1 thread enters operation saveentity , other saveentity2 @ same time, both try transaction entitymanager wich fail. thought underlying database able manage multiple transactions, if both working on different rows or different tables. of course synchronize blocks, mean 1 db connection possible @ time not scale multiple users creating several threads.
what wrong assumption doing here? possible submit multiple transactions database via jpa , let db handle concurrency issues?
entitymanager
not intended used multiple threads. need obtain separate instances of entitymanager
each thread.
actually, if use ejb or spring can use transaction-scoped entitymanager
, can used multiple threads (it's proxy delegates actual work separate thread-bound instances of entitymanager
), think it's not case.
Comments
Post a Comment