You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Laird Nelson <lj...@gmail.com> on 2009/07/24 22:17:10 UTC
JPA + EJB interaction question
If:
- I have one stateless session bean--S1--that calls another--S2--through
S2's *local *business interface...
- ...and if both S1 and S2 have EntityManagers injected into them (EM1
and EM2) with the same @PersistenceContext annotation (thus referring to the
same persistence unit)...
- ...and if S2 does something that results in an entity becoming managed
under EM2...
- ...and if S2 is marked as
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) (and if S1 is
marked as REQUIRED, though I suspect it doesn't matter)...
- ...and if S2 returns that managed object to S1 (by reference, as
guaranteed by the EJB specification)...
...is that EM2-managed object then guaranteed to be managed in EM1's
persistence context? May I call EM1.refresh() on it without getting an
exception?
Another way to ask this is: are EM1 and EM2 guaranteed to share a common
persistence context *by reference* in this case?
Thanks,
Laird
Re: JPA + EJB interaction question
Posted by David Blevins <da...@visi.com>.
On Jul 24, 2009, at 1:17 PM, Laird Nelson wrote:
> If:
>
> - I have one stateless session bean--S1--that calls another--S2--
> through
> S2's *local *business interface...
> - ...and if both S1 and S2 have EntityManagers injected into them
> (EM1
> and EM2) with the same @PersistenceContext annotation (thus
> referring to the
> same persistence unit)...
> - ...and if S2 does something that results in an entity becoming
> managed
> under EM2...
> - ...and if S2 is marked as
> @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) (and
> if S1 is
> marked as REQUIRED, though I suspect it doesn't matter)...
> - ...and if S2 returns that managed object to S1 (by reference, as
> guaranteed by the EJB specification)...
>
> ...is that EM2-managed object then guaranteed to be managed in EM1's
> persistence context? May I call EM1.refresh() on it without getting
> an
> exception?
>
> Another way to ask this is: are EM1 and EM2 guaranteed to share a
> common
> persistence context *by reference* in this case?
The long and short of it is that the persistence context is tied to
the transaction. So the REQUIRES_NEW tx attribute in a very real
sense implies "REQUIRES_NEW_PERSISTENCE_CONTEXT" as well. The end
result is that S1 and S2 will have different persistence contexts.
If you switch the tx attribute of S2 to REQUIRED then they would share
the same transaction and persistence context.
This doc sort of paints a high level picture:
http://openejb.apache.org/3.0/jpa-concepts.html
Hope this helps!
-David