Injecting an entity manager in a SFSB with Seam
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:
- You can inject the same persistence context in non-EJB Seam component
- Use of
<s:convertEntity>
in the view (assuming you are going to use JSF) - Use of EL in the Query Language
- You have the persistence context bound to the conversation context (not important for you, if understand correct)
If you choose @In
, thus Seam managed persistence context, then you can have declarative transaction demarcation using @Transactional
in non-EJB Seam component where @TransactionAttribute
make no sense.
For EJB session bean annotated as Seam component (@Name
) @TransactionAttribute
should be used with the same semantic as defined for EJB3.
Since @Transactional
doesn't have a REQUIRES_NEW
value 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_NEW
then you should access the entity manager using
@PersistenceContext
.
Chapter 9 of Dan Allen book Seam In Action will satisfy any doubt on this topic.
精彩评论