What value should I place into <jta-data-source>
of my persistence.xml
?
In glassfish admin panel I created a datasource name "abcDS"
. In my jndi.properties
(inside src/test/resources
) I defined it like this:
[...]
abcDS=new://Resource?type=DataSource
abcDS.JdbcDriver=org.hsqldb.jdbcDriver
abcDS.JdbcUrl=jdbc:hsqldb:mem:testdb
abcDS.JtaManaged=true
[...]
What shall I place into persistence.xml
? I've found a lot of variants in the Net, like: "jdbc/abcDS"
, "java:/abcDS"
, "abcDS"
. Which one is right? And is there some rule for this? I understand that it's related to JNDI, but...
I'm trying to create EMF in my unit test:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("abc");
This is what I'm getting in log:
[...]
SEVERE: Could not find datasource: abcDS javax.naming.NameNotFoundException:
Name "abcDS" not found.
at org.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:193)
at org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:150)
at org.apache.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:115)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
[...]
The problem is that
Persistence.createEntityManagerFactory("abc")
is the "do it yourself" API and doesn't take advantage of the Embedded EJB Container. You can get a container managedEntityManager
in your test case very easily.Just as with the related jndi/datasource question I recommend you check out the examples in the examples.zip. They're all designed to take the struggle out of getting started.
Here's a snippet from the
testcase-injection
example which shows how you can get an EntityManager and other things from the container for use in a test.First, add an empty ejb-jar.xml or application-client.xml to your test to turn on scanning for your test code:
Then, annotate your test case with
@org.apache.openejb.api.LocalClient
and use the standard JavaEE annotations for the actual injection.As
movieDatabase
is the only DataSource that we've setup, OpenEJB will automatically assign that DataSource to your persistence unit without the need to modify your persistence.xml. You can even leave the<jta-data-source>
or<non-jta-data-source>
empty and OpenEJB will still know what to do.But for the sake of completeness, here's how this particular application has defined the
persistence.xml
Then the fun part, using it all together in tests