I’m building a web app using Seam, using stateful session EJBs as business components (also annotated as Seam components). In this scenario what’s the best practice for injecting the entity manager, using @In or @PersistenceContext? will one of the two options cause me problems? (assume the duration of a conversation and its associated persistence context is not an issue)
If I choose to use @In and need to mark a method as non-transactional, should I use @Transactional(TransactionPropagationType.SUPPORTS) or @TransactionAttribute(TransactionAttributeType.SUPPORTS)?
You are not going to have any issue using Seam managed persistence context instead of the standard container managed persistence context, so in my opinion the best practice is using
@In.Among the advantages of using a Seam managed persistence context there are:
<s:convertEntity>in the view (assuming you are going to use JSF)If you choose
@In, thus Seam managed persistence context, then you can have declarative transaction demarcation using@Transactionalin non-EJB Seam component where@TransactionAttributemake no sense.For EJB session bean annotated as Seam component (
@Name)@TransactionAttributeshould be used with the same semantic as defined for EJB3.Since
@Transactionaldoesn’t have aREQUIRES_NEWvalue the following applies as explained in the reference documentation:If you are using EJB3 and mark your class or method
@TransactionAttribute(REQUIRES_NEW)then the transaction 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_NEWthen you should access the entity manager using@PersistenceContext.Chapter 9 of Dan Allen book Seam In Action will satisfy any doubt on this topic.