-
1. Re: EntityManager closed in Application scoped EntityQuery component
swd847 Feb 20, 2009 5:43 AM (in response to ztiringer)EntityQueries are not really designed to be put in the application scope. If no parameters have changed they will keep returning the same list of entities, which will be detached as soon as the creating conversation has ended (and will be a thread safety issue before that).
Async tasks are generally better off using a transaction scoped entityManager. Just issue the query directly in whatever component is doing the scan and it will cause far less problems.
Setting the entityManager to null will not do anything. The seam injection logic happens after the constructor call, and after a method invocation all injected fields are set to null to prevent resource leaks.
-
2. Re: EntityManager closed in Application scoped EntityQuery component
ztiringer Feb 20, 2009 3:18 PM (in response to ztiringer)Hi Stuart,
Thanks for your quick reply. In my case the same query might return a different list of entities, as other applications access the same DB and might add new records or modify existing ones.
Actually, I did start out with just injecting the entityManager (no EntityQuery), but it resulted in a NPE (I use an Observer on postInitialization to kick off after startup, and put create=true on the injection - and still), that's why I looked for other ways to resolve the issue and found that with EntityQuery it does connect to the DB.
After your reply, I went back to investigate more the injection of the entityManager (and this time with more frequent Quartz invocations) and I realized that even though the first call to the em results in NPE, the subsequent calls succeed.
So now I have plain em injection that fails the first time and works later, and EntityQuery that works correctly only the first time. Can someone please advise to find a solution that works all the time?
-
3. Re: EntityManager closed in Application scoped EntityQuery component
swd847 Feb 20, 2009 8:45 PM (in response to ztiringer)Post your code.
-
4. Re: EntityManager closed in Application scoped EntityQuery component
fridgebuzz Feb 20, 2009 9:30 PM (in response to ztiringer)I may be speaking completely out of turn here, but... I had a sort of similar problem with an application scoped component using a Quartz Timer and having trouble with the EntityManager.
I posted about it and received the answer I needed. Perhaps there is something in this thread that will aid you.
For what it's worth: The thread...
Cheers,
Vanessa
-
5. Re: EntityManager closed in Application scoped EntityQuery component
israeldl Feb 20, 2009 10:02 PM (in response to ztiringer)Use a stateless scoped component with a entityManager.
Example
@Name("blaBla)
@Scope(APPLICATION)
public class myApplicationComp{
@In(create = true)
private EntityManagerDAO myEn;
}
@Name("myEn")
@Scope(STATELESS)
public class EntityManagerDAO{
@In(create = true)
private EntityManager em;
}
Maybe it's works. -
6. Re: EntityManager closed in Application scoped EntityQuery component
ztiringer Feb 20, 2009 10:06 PM (in response to ztiringer)Thanks for your help. I figured it out in the meantime. I was making the first call to the DB from the constructor (so no em injection took place), that's why it was failing. Thanks again.