title: Unit Test code using wrong persistence unit
body: <p>In the midst of learning Maven created a simple JPA project (Java Persistence 1.0.2 with EclipseLink implementation 2.0.2). This is an Application Managed environment. So I manually control EntityManager's life cycle.</p>
  
  <p>The persistence.xml file used by the main source code is different from the one that unit test code uses (also suggested <a href="https://stackoverflow.com/questions/3862444/how-to-define-two-persistence-units-one-for-production-one-for-unit-testing">here</a>). Main code uses an Oracle DB and the test code uses an in-memory Derby.
  Running unit tests was updating the Oracle DB (!) and I eventually managed to fix that by using two different persistence-units in the XML files.</p>
  
  <p>However, I don't understand why that fixed the problem. I manually create and shut down the entity managers and they are not running concurrently. I'm pretty sure Maven (or the way I set it up) doesn't mess up the resources (XML files). In fact by looking at Maven's debug output I can see it's using the right XML file for unit tests. In fact I don't see why that should be a problem to begin with.</p>
  
  <p>Could someone enlighten me, please?</p>
  
  <p>-- Updated</p>
  
  <p>Here is the src/main/resources/META-INF/Persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence 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"
      version="2.0"&gt;
      &lt;persistence-unit name="MainPU" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;class&gt;org.domain.Book&lt;/class&gt;
          &lt;class&gt;org.domain.Tag&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="eclipselink.target-database" value="Oracle" /&gt;
              &lt;property name="eclipselink.logging.level" value="INFO" /&gt;
              &lt;property name="javax.persistence.jdbc.driver"     value="oracle.jdbc.OracleDriver" /&gt;
          &lt;property name="javax.persistence.jdbc.url"     value="jdbc:oracle:thin:@xxx:1526:XE" /&gt;
              &lt;property name="javax.persistence.jdbc.user" value="usr" /&gt;
              &lt;property name="javax.persistence.jdbc.password" value="pass" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>and here is src/test/resources/META-INF/Persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence 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"
      version="2.0"&gt;
      &lt;persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;class&gt;org.domain.Book&lt;/class&gt;
          &lt;class&gt;org.domain.Tag&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="eclipselink.target-database" value="DERBY" /&gt;
              &lt;property name="eclipselink.ddl-generation" value="drop-and-create-    tables" /&gt;
              &lt;property name="eclipselink.logging.level" value="FINE" /&gt;
              &lt;property name="javax.persistence.jdbc.driver"     value="org.apache.derby.jdbc.EmbeddedDriver" /&gt;
              &lt;property name="javax.persistence.jdbc.url"     value="jdbc:derby:memory:testDB;create=true" /&gt;
              &lt;property name="javax.persistence.jdbc.user" value="usr" /&gt;
              &lt;property name="javax.persistence.jdbc.password" value="pass" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>The Main source code:</p>
  
  <pre><code>....
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("MainPU");
  EntityManager em = emf.createEntityManager();
  EntityTransaction tx = em.getTransaction();
  
  tx.begin();
  Iterator&lt;Book&gt; booksItr = books.iterator();
  while (booksItr.hasNext())
      em.persist(booksItr.next());
  tx.commit();
  
  em.close();
  emf.close();
  ...
  </code></pre>
  
  <p>and unit test source code is almost the same except that it uses "TestPU" to create EntityManagerFactory.</p>