<Second try, full problem description this time around...>
Hi,
I'm facing the following issue: I have to DB instances, one for testing
(an in-memory HSQLDB database) and one for production (will be MySQL ,
for now I use exactly the same DB). To make the switch between the two,
I came up with the following setup:
1. Maven project, with OpenEJB registered for testing (I won't show
the pom here)
2. One persistence.xml with two persistence units (I tried to put
one persistence.xml in /src/test/resources, but it didn't got recognized
during testing) .
<persistence-unit name="ejb-example-openejb">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>exampleDatabase</jta-data-source>
<non-jta-data-source>exampleDatabaseUnmanaged</non-jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property
name="hibernate.transaction.manager_lookup_class"
value="org.apache.openejb.hibernate.TransactionManagerLookup"/>
</properties>
</persistence-unit>
<persistence-unit name="ejb-example-openejb-testing">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>exampleDatabase</jta-data-source>
<non-jta-data-source>exampleDatabaseUnmanaged</non-jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property
name="hibernate.transaction.manager_lookup_class"
value="org.apache.openejb.hibernate.TransactionManagerLookup"/>
</properties>
</persistence-unit>
3. A test case setup with the altdd-Option activated:
p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
p.put("openejb.altdd.prefix", "test");
p.put("exampleDatabase", "new://Resource?type=DataSource");
p.put("exampleDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("exampleDatabase.JdbcUrl", "jdbc:hsqldb:mem:exampledb");
p.put("exampleDatabaseUnmanaged",
"new://Resource?type=DataSource");
p.put("exampleDatabaseUnmanaged.JdbcDriver",
"org.hsqldb.jdbcDriver");
p.put("exampleDatabaseUnmanaged.JdbcUrl",
"jdbc:hsqldb:mem:exampledb");
p.put("exampleDatabaseUnmanaged.JtaManaged", "false");
context = new InitialContext(p);
4. An ejb-jar.xml and a test.ejb-jar.xml that look almost the same,
besides pointing to a different persistence unit (only test.ejb-jar.xml
shown here):
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SessionFacadeImpl</ejb-name>
<persistence-context-ref>
<persistence-context-ref-name>persistenceContext</persistence-context-re
f-name>
<persistence-unit-name>ejb-example-openejb-testing</persistence-unit-nam
e>
</persistence-context-ref>
</session>
</enterprise-beans>
</ejb-jar>
5. As you might have guessed, my stateless session bean looks like
follows:
@Stateless
public class SessionFacadeImpl implements SessionFacadeLocal {
@PersistenceContext(name="persistenceContext")
private EntityManager em;
...
}
Now I get an error when I run the tests. The error message looks like
follows:
ERROR - FAIL ... null: The persistence unit "" does not exist. Update
the "persistenceContext" PersistenceContext ref to one of the available
units [ejb-example-openejb-testing, ejb-example-openejb] or declare the
unit in a persistence.xml like the following:<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0"><persistence-unit
name=""><jta-data-source>java:openejb/Resource/myDataSource</jta-data-so
urce><non-jta-data-source>java:openejb/Resource/myUnmanagedDataSource</n
on-jta-data-source><properties><property
name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/></properties></persistence-unit><
/persistence>
If I do as I am told an add this empty persistence unit, I will get an
error like this:
ERROR - FAIL ... null: The reference
@PersistenceContext(name="persistenceContext", unitName="") cannot be
resolved as there are 3 units with the same name. Update your unitName
to one of the following: 390766514
SeminarEntityManager 390766514
ejb-example-openejb 390766514
So, what is wrong here? Is the configuration wrong, or is it a problem
with OpenEJB? Or am I down the wrong path with my setup and there's an
easier way to have a separate DB instance for testing.
Any help would be appreciated. I could narrow the problem to the point
that I could see that putting a second persistence unit into
persistence.xml caused the problem, so the ejb-jar.xml configuration on
a single persistence unit works as outlined.
Adrian Burri
Software Engineering