无法通过JBoss远程访问数据源

发布于 2024-12-22 14:15:20 字数 2648 浏览 4 评论 0原文

上下文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临走之时 2024-12-29 14:15:20

如果您得到 ,

javax.naming.Reference 

则意味着您缺少解析所需的运行时依赖项。您可以通过检查 Reference 对象的 classFactory 成员

getFactoryClassName() 

来找出缺少的第一个依赖项。

在我使用 JBoss6.1 的情况下,它是:

org.jboss.resource.adapter.jdbc.remote.DataSourceFactory

将此类所在的 jar 放在类路径上 (common/lib/jbosscx-client.jar) 让我完成了第一步,然后这是一个问题跟踪所有 ClassNotFoundException 错误。

JBoss6.1 的完整列表如下:

- client/jnp-client.jar
- client/jboss-logging.jar
- common/lib/jbosscx-client.jar
- client/concurrent.jar
- client/jboss-client.jar
- client/jboss-common-core.jar
- client/jboss-integration.jar
- client/jboss-remoting.jar
- client/jboss-security-spi.jar
- client/jboss-serialization.jar
- client/jboss-transaction-api_1.1_spec.jar

If you get a

javax.naming.Reference 

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

getFactoryClassName() 

to figure out the first dependency you are missing.

In my case using JBoss6.1 it was:

org.jboss.resource.adapter.jdbc.remote.DataSourceFactory

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:

- client/jnp-client.jar
- client/jboss-logging.jar
- common/lib/jbosscx-client.jar
- client/concurrent.jar
- client/jboss-client.jar
- client/jboss-common-core.jar
- client/jboss-integration.jar
- client/jboss-remoting.jar
- client/jboss-security-spi.jar
- client/jboss-serialization.jar
- client/jboss-transaction-api_1.1_spec.jar
涫野音 2024-12-29 14:15:20

本指南说您必须

<use-java-context>false</use-java-context>

设置mydatasource-ds.xml。不确定你是否这样做了。

This guide says that you have to set

<use-java-context>false</use-java-context>

in mydatasource-ds.xml. Not sure if you did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文