On Mar 11, 2009, at 7:10 PM, Kaayan wrote:


Hi all, I'm using openejb 3.1 + Tomcat 6.0.18.

My question is can I use @PersistenceContext to inject EntityManager and
into Singleton beans?

I have configured the persistence-unit with type="TRANSACTION" and
@PersistenceContext worked fine on Stateful and Stateless beans.

But when I use the same way to inject EntityManager into a Singleton(also
annotated @Startup) and tried to persist() something, openejb logged
TransactionRequiredException and failed to deploy the app.

Am I mistakenly understanding the documents? I've searched over the openejb
official site but couldn't find any examples about this. Many thanks!

Currently, the @PostConstruct methods are not invoked in a container transaction -- identically to stateful and stateless. This is one change to the Singleton API that was made after we shipped our OpenEJB 3.1 release. I suspect that is where you're running into issues as you mention the use of @Startup. All business methods will be invoked in a transaction by default and there should be no issues using a TRANSACTION PersistenceContext from a Singleton.

A temporary workaround could be to have a, possibly new, singleton call another bean (stateless, stateful, or singleton) in its @PostConstruct method and let that other bean setup the state you need in it's business method.

-David

Reply via email to