无法通过JBoss远程访问数据源
上下文
我有一个 JBoss,我已在其中成功部署了数据源 mydatasource-ds.xml
。 它的 JNDI 名称是 java:mydatasourceDS
。 JBoss 声称数据源已成功部署。 JMX 控制台也有问题。
问题
我想使用在单独的 JVM 上启动的客户端 Java 应用程序中的此数据源。 但我收到一个异常,说找不到 java:mydatasourceDS
。
java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.sql.DataSource
详细信息
这是persistence.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="mydatasource-db" transaction-type="JTA">
<jta-data-source>java:mydatasourceDS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.default_schema" value="rec" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
这是jndi.properties
文件:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
这是应用程序类路径:
jndi.properties
log4j.properties
META-INF\persistence.xml
hibernate-jpa-2.0-api-1.0.0.Final.jar
jnp-client-5.0.3.GA.jar
jboss-common-core-2.2.14.GA.jar
jboss-logging-spi-2.1.0.GA.jar
hibernate-entitymanager-3.6.4.Final.jar
hibernate-core-3.6.4.Final.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-3.2.0.Final.jar
jta-1.1.jar
javassist-3.12.0.GA.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.16.jar
xstream-1.4.1.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar
编辑 我已经找到问题的根源了。我的客户端应用程序可用的 javax.sql.DataSource
不是从 JNDI 接收的。我从 JNDI 收到的是 JBoss javax.sql.DataSource
。名称相同,但类略有不同,因此出现 ClassCastException...
在撰写本文时,可以在此包中找到 JBoss javax.sql.DataSource:jboss-j2ee-4.2.3.GA.jar
Context
I have a JBoss where I have sucessfully deployed a datasource mydatasource-ds.xml
.
It's JNDI name is java:mydatasourceDS
. JBoss claims that the datasource is succesfully deployed. The JMX console agress too.
Problem
I want to use this datasource from a client java app launched on a separate JVM.
But I get an exception saying the java:mydatasourceDS
cannot be found.
java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.sql.DataSource
Details
Here is the persistence.xml
file :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="mydatasource-db" transaction-type="JTA">
<jta-data-source>java:mydatasourceDS</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.default_schema" value="rec" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
Here is the jndi.properties
file :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Here is the application classpath :
jndi.properties
log4j.properties
META-INF\persistence.xml
hibernate-jpa-2.0-api-1.0.0.Final.jar
jnp-client-5.0.3.GA.jar
jboss-common-core-2.2.14.GA.jar
jboss-logging-spi-2.1.0.GA.jar
hibernate-entitymanager-3.6.4.Final.jar
hibernate-core-3.6.4.Final.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-3.2.0.Final.jar
jta-1.1.jar
javassist-3.12.0.GA.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.16.jar
xstream-1.4.1.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar
EDIT
I have found the source of the problem. The javax.sql.DataSource
that was available to my client application was NOT the one received from JNDI. The one I receive from JNDI is the JBoss javax.sql.DataSource
. Same name BUT slightly different classes hence the ClassCastException...
As of this writing, the JBoss javax.sql.DataSource can be found in this package : jboss-j2ee-4.2.3.GA.jar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您得到 ,
则意味着您缺少解析所需的运行时依赖项。您可以通过检查 Reference 对象的 classFactory 成员
来找出缺少的第一个依赖项。
在我使用 JBoss6.1 的情况下,它是:
将此类所在的 jar 放在类路径上 (
common/lib/jbosscx-client.jar
) 让我完成了第一步,然后这是一个问题跟踪所有 ClassNotFoundException 错误。JBoss6.1 的完整列表如下:
If you get a
that means you are missing runtime dependencies that are required for it to resolve. You can inspect the classFactory member of the Reference object via
to figure out the first dependency you are missing.
In my case using JBoss6.1 it was:
Putting the jar this class is in on the classpath (
common/lib/jbosscx-client.jar
) got me past the first step and then it was a matter of tracking down all the ClassNotFoundException errors.The full list for JBoss6.1 turned out to be:
本指南说您必须
设置
mydatasource-ds.xml
。不确定你是否这样做了。This guide says that you have to set
in
mydatasource-ds.xml
. Not sure if you did.