Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] EntityManager and thread safety

If you have multiple threads, each thread should have its own EntityManager,
this isolates each threads changes and gives each thread its own transaction
context.  Using multiple threads for a single EntityManager would be very
odd, why are you doing this?

If the threads use a blocking mechanism such that only one thread accesses
the EntityManager at one time, you should not have any concurrency issue,
except if your threads use transactions, if you want each thread to have its
own transactions, then you must block for the whole duration of the
transaction.  But in general you should be creating an EntityManager per
thread.


Ben Horowitz-3 wrote:
> 
> Hi,
> 
> I have some questions about thread safety and EclipseLink's EntityManager.
> 
> I understand that the spec says it's not safe to access an entity manager 
> concurrently from multiple threads.
> 
> But, supposing that I use synchronized blocks or java.util.concurrent
> locks as a 
> mutual exclusion mechanism to guarantee that no two threads will
> concurrently 
> access the same entity manager or any of the entities it manages: in this
> case, 
> is it safe to access the entity manager and the entities it manages from 
> different threads?
> 
> I understand that the behavior may be different in EclipseLink vs. other
> JPA 
> implementations.  I'm only asking about EclipseLink.
> 
> Thanks a lot,
> Ben
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/EntityManager-and-thread-safety-tp21670722p21684665.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top