tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@gmail.com>
Subject Re: can't persist value in TransactionSynchronizationRegistry
Date Thu, 12 Apr 2012 19:39:44 GMT
This kind of EntityManager tracking is provided out of the box for you and is a standard part
of JavaEE.  In fact it's one of the main benefits you get.  EntityManagers are tracked in
a transaction and flushed or discarded on commit or rollback, all automatically.

This doc talks about how it works:  

 - http://openejb.apache.org/jpa-concepts.html

Maybe also see the "JPA Integration" section of this answer which describes the tracking more
tersely:

 - http://stackoverflow.com/questions/269186/besides-ear-and-ejb-what-do-i-get-from-a-j2ee-app-server-that-i-dont-get-in-a/9199893#9199893

So the question at hand, if your mechanism for tracking and the container's mechanism for
tracking are the same, then why doesn't it work?  The answer applies to both.

The TransactionSynchronizationRegistry (and therefore the EntityManager tracked in it) only
live as long as the transaction lives.  That will be for the duration of the one method call
if the EJB is being called from someplace that doesn't already have a transaction in progress.

The only way to keep the TransactionSynchronizationRegistry around is to keep the transaction
open longer.  There are some things to keep in mind:

   - Transactions are not multi-threaded
   - The transaction must end in the same thread in which it was began

As an FYI, those restrictions are not artificial.  Multi-threaded transactions are hard and
there aren't any TransactionManagers that support it -- read, there's no way to get around
it.

Your only option for truly holding an EntityManager for a long period of time is to track
it between transactions.  The JavaEE standard term for this kind of tracking is called an
EXTENDED PersistenceContext.  This example isn't really documented, but the code works and
uses EXTENDED persistence contexts.

  http://openejb.apache.org/examples-trunk/injection-of-entitymanager/README.html

EXTENDED persistence contexts provide the same transactional exclusivity and tracking that
a TRANSACTION scoped persistence context provides, but with the added benefit that the EntityManager
itself will live as long as the @Stateful bean that created it.  You can track the @Stateful
bean however you want, such as by putting it into the HttpSession.

Hope this helps!


-David


On Apr 12, 2012, at 9:15 AM, Alexis KRIER wrote:

> 
> Hello all I have an entityManagerProvider wich creates the entityManager if it does not
exist already.
> 
> To do this I store the EntityManager created in TransactionSynchronizationRegistry 
> TransactionSynchronizationRegistry registry = (TransactionSynchronizationRegistry)getContext().lookup(
>          "java:comp/TransactionSynchronizationRegistry");
> (works also with java:openejb)
> 
> then when an other call is done to the EMProvider I look in the registry to get my manager
if it exists:
> TransactionSynchronizationRegistry registry = (TransactionSynchronizationRegistry)getContext().lookup(
>          "java:comp/TransactionSynchronizationRegistry");
> registry.getesources("The key")
> 
> The problem is that on each call from a stateless resource, it seem that the registry
is always empty...
> 
> By the way the registry.put("My entity") is working well because when I do immediately
after a get I can see My value stored.
> 
> Is there a trick to keep the TransactionSynchronizationRegistry Statefull?
> 
> thank you
> 
> Alexis
> 		 	   		  


Mime
View raw message