users@glassfish.java.net

Two JTA persistance units does not see each other changes, why?

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Thu, 12 Jul 2007 09:49:24 +0200

Hi there,
I have a scenario: one JDBC data source is used by two persistence
units, each one in its own EJB module, both belongs to same enterprise
application. First EJB module has "Session1" session bean and
"method1" in it, second EJB module has "Session2" and "method2". Both
methods have default transaction attribute (REQUIRED).
Transaction begins in first EJB module:

@Stateless
public class Session1 implements Session1Local {
  @PersistenceContext EntityManager em;
  @EJB Session2Local session2;

  public void method1() {
    Person p = new Person();
    em.persist(p);
    em.flush();
    session2.method2();
  }
}

@Stateless
public class Session2 implements Session2Local {
  @PersistenceContext EntityManager em;

  public void method2() {
    List persons = em.createQuery("SELECT p FROM Person p")
      .getResultList();
    // persons list does not contain the person persisted
    // in #method1 even though both methods are in the same transaction
    // and flush was explicitly invoked to synchronize first em's context
  }
}

My question is: why entity manager from EJB2 cannot see the person I
just inserted into database? Both methods should be in THE SAME
transaction, because both persistence units are JTA aware and are
injected by application server itself.

Why this does not work? Is the transaction from method1 not correctly
propagated to method2?