@Asynchronous events do not have access to the conversation or session scope, which makes it very hard to get access to the extended persistence context.
In practice even if you did manage to get access to it through some hack you have to consider that EntityManagers are not thread safe, so you would have to serialize all your asynchronous requests anyway (and all normal http requests in the same conversation), which defeats the purpose of running them asynchronously and would result in way less manageable code than the alternative.
EJB3 specification says you cannot use extended persistent context in SLSB nor in MDB.
Apart from extended context from EJB3 there is Seam Managed Persistence Context, which you may access from all components.
Your problem might be that the async method is not surrounded with transaction, which does not distrub you from retriving entities via EntityManager but they become detatched right after that.
Do not try and use the seam managed persistence context in an async method for the following reasons:
- You do not have access to the conversation context, you would have to pass it in as a method parameter
- If you do get access to it then what happens if the user ends the conversation before the async method has finished (extremely likely if there is not long running conversation)? The entityManager will be closed and everthing will come to a halt.
- As I said before it is not thread safe, and will probably cause all kinds of fun bugs (you can get thread safe entitymanagers by wrapping them in a proxy that serialises all requests, but this still just feels like a bad idea).
async methods should almost always use a transaction scoped entityManager
Got it -- thanks Gentlemen!