|EntityManager and Multi-threading design question [message #899437]
||Tue, 31 July 2012 15:38
| Amit B
Registered: July 2012
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.
Powered by FUDForum
. Page generated in 0.01481 seconds