[jboss-user] [JBoss Seam] - Re: When to use @PersistenceContext vrs @In EntityManager (S

kasim do-not-reply at jboss.com
Thu Feb 7 11:45:59 EST 2008

"msystems" wrote : Use @PersistenceContext if you are using EJB3 and if you need a new transaction (@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) ) - Seam ref:
  | anonymous wrote : 
  |   | If you are using EJB3 and mark your class or method @TransactionAttribute(REQUIRES_NEW) then the trans- 
  |   | action and persistence context shouldn't be propagated to method calls on this object. However as the Seam- 
  |   | managed persistence context is propagated to any component within the conversation, it will be propagated to 
  |   | methods marked REQUIRES_NEW. Therefore, if you mark a method REQUIRES_NEW then you should access the en- 
  |   | tity manager using @PersistenceContext. 
  |   | 
  | otherwise use SMPC.

What you are saying at the top part is true.

However i don't believe the doco is saying use SMPC all the time.

Take a look at the first paragraph of the section. It basically refers to if you are using the component outside of a Java EE5 environment or if you have many loosly coupled components. Which as long as they are all SB the PersisetnceCOntext should transfer. However the doco is 100% right if you are going to have a mix of SB and POJOS .... then yeah doing the EntityManager injection is the only way to go.

So you can stick with the @PersistenceContext in most situations. Its going to be a situation where you are wanting to transact against non-EJB components you will want to use the @IN EntityManger. 

Of course that being said i am not sure the harm it would cause using the @In EntityManager more often.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4127483#4127483

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4127483

More information about the jboss-user mailing list