-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Persistence context propagation: documentation not clear?
PostPosted: Fri Aug 13, 2010 4:43 am 
Newbie

Joined: Mon Dec 28, 2009 10:56 pm
Posts: 5
Hi guys,

I'm struggling to understand a key part of the Hibernate documentation. Specifically 1.2.4...

http://docs.jboss.org/hibernate/stable/ ... ropagation

...says "In a transaction-scoped container managed entity manager (common case in a Java EE environment), the JTA transaction propagation is the same as the persistence context resource propagation".

During my HTTP request/response cycle, my app seems to be giving a new EntityManager to each @PersistenceContext-injected EJB. I assume therefore I am not correctly establishing a 'transaction-scoped container managed entity manager'? But the doc isn't clear how you actually establish the transaction scope?

I tried putting @TransactionAttribute at the class level, and at the method level, but my EJBs still receive different EntityManagers. If I were using a non-container-managed EntityManager I guess I could do something in a ServletFilter, but what is the correct way for container managed?

Thanks in advance,

Richard.


Top
 Profile  
 
 Post subject: Re: Persistence context propagation: documentation not clear?
PostPosted: Fri Aug 27, 2010 6:32 am 
Newbie

Joined: Mon Dec 28, 2009 10:56 pm
Posts: 5
An answer for those that come this way after me :)

The documentation is a bit confusing. If you look here:

http://docs.jboss.org/hibernate/stable/ ... ml#d0e2823

There are some great examples of how to do it outside of a Java EE container. Documentation about 'em.createEntityManager' and 'em.getTransaction' and the JTA variant 'utx.commit()' and so forth. There is also discussion of how to look after your own EntityManager through the use of Filters or ThreadLocals or whatnot. Then there is a bit that says:

"all you have to do in a managed environment is to inject the EntityManager, do your data access work, and leave the rest to the container. Transaction boundaries are set declaratively in the annotations or deployment descriptors of your session beans. The lifecycle of the entity manager and persistence context is completely managed by the container."

And there are no further examples of JTA management. But this is misleading if you are trying to implement the (recommended) EntityManager-per-request pattern in a container-managed environment. When they say 'Transaction boundaries are set declaratively in the annotations... of your session beans' that's actually pretty useless, because it means you will start and end a transaction with each bean call during your HTTP request.

What you really need is to start and end the transaction around the entire HTTP request, and to do that you still need to use 'utx.commit()' and Filters and whatnot. Even if you are using container-managed persistence contexts, you'll still need a ServletFilter around your HTTP Request something like:

Code:
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
   throws ServletException {

   UserTransaction tx = new InitialContext().lookup( "java:comp/UserTransaction" );
   tx.beginCurrent();

   try {
      chain.doFilter( request, response );

      tx.commitCurrent();

   } catch ( Throwable e ) {

      tx.rollbackCurrent();
      throw new ServletException( e );
   }
}

Alternatively, you can use Seam or CDI or something like that.

Hope that helps!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.