Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EntityManager and Multi-threading design question
EntityManager and Multi-threading design question [message #899437] Tue, 31 July 2012 19:38 Go to next message
Amit B is currently offline Amit BFriend
Messages: 14
Registered: July 2012
Junior Member
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 18:02 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

#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.

James : Wiki : Book : Blog : Twitter
Previous Topic:NPE during commit
Next Topic:Spring 3.0.6+Eclipselink2.4.0 persist/insert is not working
Goto Forum:

Current Time: Wed Sep 30 10:50:05 GMT 2020

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

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

Back to the top