title: Why different persistence units with separated data sources query the same data source?
body: <p>I'm developing a webapp which needs access to two different database servers (H2 and Oracle).
  The container is an <a href="http://tomee.apache.org/apache-tomee.html">Apache Tomee 1.5.1</a> and I'm using the Java EE stack with libraries provided in it (JSF, JPA, CDI, EJB, etc.).</p>
  
  <p>I'm trying to use two entity managers inside an XA transaction to extract data from the Oracle database and persist it in the H2 after transforming it, BUT all the queries are executed against the H2 database no matter the entity manager I use. Any help? </p>
  
  <p><strong>EDIT</strong>: I found that if I try to access the entity managers in inverse order, they behavior is the same but accessing to Oracle. I.e.: the entity managers stay with the first database accessed.</p>
  
  <p>The EJB where this happens (calling <code>service.getFoo()</code> from JSF):</p>
  
  <pre><code>@Named
  @Stateless
  public class Service {
      @Inject
      @OracleDatabase
      private EntityManager emOracle;
  
      @Inject
      @H2Database
      private EntityManager emH2;
  
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public List&lt;Foo&gt; getFoo() {
          TypedQuery&lt;Foo&gt; q = emH2.createQuery(
                  "SELECT x FROM Foo f", Foo.class);
          List&lt;Foo&gt; l = q.getResultList();
          if (l == null || l.isEmpty()) {
              update();
          }
  
          return q.getResultList();
      }
  
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void update() {
          // FAIL: This query executes against H2 with Oracle entity manager!
          List&lt;Object[]&gt; l = emOracle.createNativeQuery("SELECT * FROM bar ").getResultList(); 
  
          //more stuff...
      }
  }
  </code></pre>
  
  <p>The resource producer (CDI) for the entity managers (where @H2Database and @OracleDatabase are <a href="http://docs.oracle.com/javaee/6/api/javax/inject/Qualifier.html">qualifiers</a>):</p>
  
  <pre><code>public class Resources {
      @Produces
      @PersistenceContext(unitName = "OraclePU")
      @OracleDatabase
      private EntityManager emOracle;
  
      @Produces
      @PersistenceContext(unitName = "H2PU")
      @H2Database
      private EntityManager emH2;
  }
  </code></pre>
  
  <p>My <em>peristence.xml</em> looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="H2PU"
          transaction-type="JTA"&gt;
          &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
          &lt;jta-data-source&gt;H2DS&lt;/jta-data-source&gt;
          &lt;class&gt;my.app.h2.Foo&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;/persistence-unit&gt;
  
      &lt;persistence-unit name="OraclePU" transaction-type="JTA"&gt;
          &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
          &lt;jta-data-source&gt;OracleDS&lt;/jta-data-source&gt;
          &lt;class&gt;my.app.oracle.Bar&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And finally, the data sources inside <em>tomee.xml</em> (there aren't any other data sources configured inside this file):</p>
  
  <pre><code>&lt;Resource id="OracleDS" type="javax.sql.DataSource"&gt;
      jdbcDriver = oracle.jdbc.xa.client.OracleXADataSource
      jdbcUrl = jdbc:oracle:thin:@server:port:instance
      jtaManaged = true
      password = abcde
      userName = user
  &lt;/Resource&gt;
  
  &lt;Resource id="H2DS" type="javax.sql.DataSource"&gt;
      jdbcDriver=org.h2.jdbcx.JdbcDataSource
      jdbcUrl=jdbc:h2:h2/db;AUTO_SERVER=TRUE
      jtaManaged = true
      password = edcba
      userName = user
  &lt;/Resource&gt;
  </code></pre>