As long as getEmFromFactory(); returns a fresh em (ie: emf.createEntityManager), that code should be safe. Since both threads will have different em references, calling em.find(B.class, 123) will also return two separate instances. More so, EntityManagers aren't supposed to be shared by threads so this scenario isn't valid if getEmFromFactory() returns the same instance to two different threads. -- Thanks, Rick
On Mon, Apr 5, 2010 at 3:28 PM, Daryl Stultz <[email protected]> wrote: > On Mon, Apr 5, 2010 at 4:05 PM, Rick Curtis <[email protected]> wrote: > > > Daryl I'm not entirely sure I follow your last email. My understanding of > > the problem is that we are trying to merge a detached entity that has a > > relationship to an entity which is in a different persistence context. > > > I don't think I have a good enough understanding of JPA to explain what I > mean. Maybe something like this: > > em = getEmFromFactory(); > EntityB b = getBFromQuery(em); > EntityA a = new EntityA(); > a.setB(b); > Thread.sleep(1000); > em.persist(a); > > If two threads enter this code overlapping by a few millis, could "b" fail > due to it being managed by the other thread's em? I figure if the problem > with my code resulted in an em being left open and this error being thrown, > then there might be a race condition present even after fixing the problem > in my code, just with a smaller window. > > -- > Daryl Stultz > _____________________________________ > 6 Degrees Software and Consulting, Inc. > http://www.6degrees.com > mailto:[email protected] >
