为什么从jndi.properties注入后,JNDI中找不到datasource? [英] Why datasource is not found in JNDI after injection from jndi.properties?
问题描述
这是我的 persistence.xml
:
<持续性>> ;
< persistence-unit name =MyUnit>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> jdbc / abcDS< / jta-data-source>
< / persistence-unit>
< /余辉>
这是 jndi.properties
code> src / test / resources 这是应该在测试过程中创建一个数据源的,因为真正的应用服务器没有真实的数据源:
java.naming.factory.initial = org.apache.openejb.client.LocalInitialContextFactory
jdbc / abcDS = new:// Resource?type = DataSource
jdbc / abcDS.JdbcDriver = org.hsqldb.jdbcDriver
jdbc / abcDS.JdbcUrl = jdbc:hsqldb:mem:testdb
jdbc / abcDS.JtaManaged = true
jdbc / abcDS.DefaultAutoCommit = false
jdbc / abcDS.UserName = sa
jdbc / abcDS.Password =
这是测试类:
public class FinderTest {
@BeforeClass
public static void startEJB( )抛出异常{
InitialContext ic = new InitialContext();
ic.lookup(jdbc / abcDS);
}
}
不幸的是,数据源没有创建,这就是我一直在看:
[...]
javax.naming.NameNotFoundException:名称jdbc / abcDSnot找到。
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.IvmContext.lookup(IvmContext.java:124)
at org.apache.openejb.core.ivm.naming.ContextWrapper。查找(ContextWrapper.java:115)
在javax.naming.InitialContext.lookup(InitialContext.java:392)
在com.XXX.FinderTest.startEJB(FinderTest.java:31)
[...]
我做错了什么?请帮助!
ps。顺便说一句,它以这种方式工作(发生了什么???):
$ $ $ $ $ $ $ $> ic.lookup(java:/ openejb /资源/ JDBC / abcDS);
如果您查找 openejb:Resource / jdbc / abcDS
你也可以得到注入你的TestCase 。基本上,你:
- 添加一个空的
src / test / resources / META-INF / application-client.xml 或
ejb-jar.xml
- 使用
@LocalClient
- 调用
initialContext.bind(inject,this)
编辑如果查找仍然失败,请发布您的日志输出(控制台输出)。
This is my persistence.xml
:
<persistence>
<persistence-unit name="MyUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/abcDS</jta-data-source>
</persistence-unit>
</persistence>
This is jndi.properties
file from src/test/resources
which is supposed to create a datasource during testing, since a real application server with a real datasource is absent:
java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory
jdbc/abcDS=new://Resource?type=DataSource
jdbc/abcDS.JdbcDriver=org.hsqldb.jdbcDriver
jdbc/abcDS.JdbcUrl=jdbc:hsqldb:mem:testdb
jdbc/abcDS.JtaManaged=true
jdbc/abcDS.DefaultAutoCommit=false
jdbc/abcDS.UserName=sa
jdbc/abcDS.Password=
This is the test class:
public class FinderTest {
@BeforeClass
public static void startEJB() throws Exception {
InitialContext ic = new InitialContext();
ic.lookup("jdbc/abcDS");
}
}
Unfortunately, the datasource is not created and this is what I keep seeing:
[...]
javax.naming.NameNotFoundException: Name "jdbc/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.IvmContext.lookup(IvmContext.java:124)
at org.apache.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:115)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.XXX.FinderTest.startEJB(FinderTest.java:31)
[...]
What am I doing wrong? Please help!
ps. By the way it works this way (what's going on???):
ic.lookup("java:/openejb/Resource/jdbc/abcDS");
Should be found if you lookup openejb:Resource/jdbc/abcDS
As well you can get injection in your TestCase. Basically, you:
- add an empty
src/test/resources/META-INF/application-client.xml
orejb-jar.xml
- Annotate your test with
@LocalClient
- Call
initialContext.bind("inject", this)
See the testcase-injection
example in the examples.zip
EDIT If the lookup still fails, post your log output (the console output).
这篇关于为什么从jndi.properties注入后,JNDI中找不到datasource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!