-
1. Re: PersistenceContext propagation
wdfink Jun 29, 2011 8:23 AM (in response to rotapa)I'm not sure whether the EM works without transaction context.
Add
@TransactionManagement(TMType.CONTAINER)
@TransactionAttribute(REQUIRED)
to your class definition and check if it works.
Also it might happen that the two beans run in a different transaction context.
Than your code might work if you call Service2 from the client, also the User should be in the database after the test is executed.
-
2. Re: PersistenceContext propagation
rotapa Jun 29, 2011 11:01 AM (in response to wdfink)I think I got it.
@TransactionManagement(TMType.CONTAINER) and @TransactionAttribute(REQUIRED) are the default so it shouldnt be a problem.
I didnt write the whole class for simplicity. I have a stateful bean, with extended pc. I wanted to have an exception because of the pc collision.
but it seems quite strange with jboss:
so I have a stateful bean:
@Stateful
public class Service3Bean implements Service3 {
@PersistenceContext(type=...EXTENDED)
private EntityManager em;
@Override
public void doStuff(Long id) {
User u = em.find(User.class, id);
System.out.println("user for id " + id + " is: " + u);
}
}
so service3 is the stateful, service2 is the stateless
and in the service1's doStuff method:
@Override
public void doStuff() {
User u = new User();
user.setName("Name");
em.persist(u);
service3.doStuff(u.getId());
service2.doStuff(u.getId());
}
in that case I get:
user for id 13345 is: null
user for id 13345 is: null
If I change the order of invocations:
@Override
public void doStuff() {
User u = new User();
user.setName("Name");
em.persist(u);
service2.doStuff(u.getId());
service3.doStuff(u.getId());
}
user for id 13345 is: pkg.User@aaaaaa
user for id 13345 is: pkg.User@bbbbbb
so if we call stateless before the stateful the pc is propagated, and it work for the extended pc as well.
if we call the stateful before the stateless the pc doesnt get propagated.
another interesting:
if I call the stateless before stateful, and I modify the username in the stateless, (the pc is propagated to the stateless so we can do),
and from the service1Bean we call the stateful the pc doesnt get propagated there, so the user there is null.
It's quite extreme example, but as the hibernate reference jpa spec say, an exception should be thrown if we try to call a stateful (with REQUIRED trancaction attribute) from stateless.
It seems to be a bug/strange feature
btw, Thank you for the fast reply