[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openejb-user
Subject:    Re: Cannot persist() in @Singletons?
From:       David Blevins <david.blevins () visi ! com>
Date:       2009-03-13 16:59:24
Message-ID: A6E8C9EA-596A-4496-9AA2-97C819F84C46 () visi ! com
[Download RAW message or body]


On Mar 13, 2009, at 2:52 AM, Kaayan wrote:

> 
> Thanks for your reply! Your suggestion works!
> 
> I found another solution to this problem - define another  
> RESOURCE_LOCAL
> unit in persistence.xml. But the drawback is that I need to re- 
> define all
> @PersistenceContext in my current beans as there are multiple  
> persistence
> unit, and I have to call getTransaction().begin() in my @Singleton's.

You shouldn't need to switch to RESOURCE_LOCAL everywhere.  You really  
only need it for the PostConstruct method of the singleton, after that  
you would be better of with TRANSACTION scoped.  For the singletons  
that need to manipulate data in the PostConstruct, you could have both  
a @PersistenceContext reference and a @PersistenceUnit reference.

-David


> 
> 
> 
> 
> David Blevins wrote:
> > 
> > 
> > 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
> > 
> > 
> > 
> 
> -- 
> View this message in context: \
> http://www.nabble.com/Cannot-persist%28%29-in-%40Singletons--tp22468590p22493227.html
>  Sent from the OpenEJB User mailing list archive at Nabble.com.
> 
> 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic