Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » EntityManager and Multi-threading design question
EntityManager and Multi-threading design question [message #899437] Tue, 31 July 2012 15:38 Go to next message
Eclipse UserFriend
I am fairly new to EclipseLink and JPA so I am just going to ask the as I understand it. I have some questions regarding multiple threads and Eclipselink. I read from the specs that EntityManagerFactory is thread-safe and Entity Manager are not. We have a client-server application using Eclipselink and JPA 2.0 talking to a database i.e an application managed EntityManager.
The server also connects to a device and constantly reads and write data to the device and also reads and updates to a database depending upon client requests. We are now adding another device so that the server connects to this second device by creating a second thread. I have two design choices -
1. We create two threads and use a single EntityManager. Then we synchronize the methods using synchronized blocks. The transaction are already being guarded against by using lock and mutual-exclusion. However, we seem to hit random null-pointer crashes when trying to execute select queries.

2. I read somewhere each thread should have its own EntityManager, this isolates each threads changes and gives each thread its own transaction context. Why is this a better choice then the first one. Also, does this mean that if I have 10 threads in an application I should have 10 EntityManagers. Can someone point me to some links on this topic.

Thanks for looking.
Re: EntityManager and Multi-threading design question [message #899670 is a reply to message #899437] Wed, 01 August 2012 14:02 Go to previous message
Eclipse UserFriend
#2 is correct,

Your should have one EntityManager per thread. Even if you synchronize access to the EntityManager, your two different threads transactions and contexts will still overlap.
Previous Topic:NPE during commit
Next Topic:Spring 3.0.6+Eclipselink2.4.0 persist/insert is not working
Goto Forum:
  


Current Time: Mon Jul 07 23:13:13 EDT 2025

Powered by FUDForum. Page generated in 0.02904 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top