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.
The persistence.xml file used by the main source code is different from the one that unit test code uses (also suggested here). 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.
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.
Could someone enlighten me, please?
-- Updated
Here is the src/main/resources/META-INF/Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="MainPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.domain.Book</class>
<class>org.domain.Tag</class>
<properties>
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@xxx:1526:XE" />
<property name="javax.persistence.jdbc.user" value="usr" />
<property name="javax.persistence.jdbc.password" value="pass" />
</properties>
</persistence-unit>
</persistence>
and here is src/test/resources/META-INF/Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.domain.Book</class>
<class>org.domain.Tag</class>
<properties>
<property name="eclipselink.target-database" value="DERBY" />
<property name="eclipselink.ddl-generation" value="drop-and-create- tables" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:testDB;create=true" />
<property name="javax.persistence.jdbc.user" value="usr" />
<property name="javax.persistence.jdbc.password" value="pass" />
</properties>
</persistence-unit>
</persistence>
The Main source code:
....
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MainPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Iterator<Book> booksItr = books.iterator();
while (booksItr.hasNext())
em.persist(booksItr.next());
tx.commit();
em.close();
emf.close();
...
and unit test source code is almost the same except that it uses "TestPU" to create EntityManagerFactory.