将数据源连接到JBOSS EAP 6.4的Java代码:命名异常

时间:2015-06-26 14:18:50

标签: jboss jboss-eap-6

我需要将为jboss eap 6.4定义的数据源连接到我的java代码。

我的standalone.xml是:

    <datasources>
        <datasource jndi-name="java:jboss/datasources/mn572phOraDataSource" pool-name="OraDataSource" enabled="true" use-java-context="true">
            <connection-url>jdbc:oracle:thin:@test:1521:db11g</connection-url>
            <driver>h1</driver>
            <pool>
                <min-pool-size>20</min-pool-size>
                <max-pool-size>40</max-pool-size>
            </pool>
            <security>
                <user-name>userName</user-name>
                <password>password</password>
            </security>
        </datasource>
        <drivers>
            <driver name="h1" module="com.oracle">
                <datasource-class>oracle.jdbc.OracleDriver</datasource-class>
            </driver>
        </drivers>
    </datasources>
...
<subsystem xmlns="urn:jboss:domain:naming:1.4">
            <remote-naming/>
        </subsystem>
...
<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
        <socket-binding name="management-native" interface="management" port="${jboss.management.native.port:7204}"/>
        <socket-binding name="management-http" interface="management" port="${jboss.management.http.port:7203}"/>
        <socket-binding name="management-https" interface="management" port="${jboss.management.https.port:7205}"/>
        <socket-binding name="http" port="7201"/>
        <socket-binding name="https" port="7202"/>
        <socket-binding name="ajp" port="7206"/>
        <socket-binding name="remoting" port="7207"/>
        <socket-binding name="txn-recovery-environment" port="7208"/>
        <socket-binding name="txn-status-manager" port="7209"/>
        <outbound-socket-binding name="mail-smtp">
            <remote-destination host="localhost" port="25"/>
        </outbound-socket-binding>
    </socket-binding-group>

jboss-web.xml有以下内容:

<resource-ref>
    <res-ref-name>jdbc/mn572phOraDataSource</res-ref-name>
    <jndi-name>java:jboss/datasources/mn572phOraDataSource</jndi-name>
</resource-ref>

我的java代码是:

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
p.put(Context.PROVIDER_URL, "remote://localhost:7207/");
Context context = new InitialContext(p);
dataSource = (DataSource)context.lookup("jdbc/mn572phOraDataSource");

我认为jboss-web.xml正确连接到JBoss数据源(通过更改jndi-name测试它并且它给出了错误)

我已将jboss-client.jar添加到类路径中:

<resources>
    <resource-root path="ojdbc6.jar"/>
<resource-root path="jboss-client.jar"/>
</resources>
<dependencies>
    <module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>

我无法从Java Code连接到数据源。

我遇到以下异常:

Caused by: javax.naming.NameNotFoundException: jdbc/mn572phOraDataSource -- service jboss.naming.context.java.jboss.exported.jdbc.mn572phOraDataSource
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:104)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:197)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:174)
    at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127) [jboss-client.jar:7.5.0.Final-redhat-21]
    at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73) [jboss-client.jar:7.5.0.Final-redhat-21]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_55]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_55]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_55]

任何解决此问题的指针都非常有用并且非常感激。

感谢。

2 个答案:

答案 0 :(得分:2)

JBoss-eap-5提供了对数据源的远程(客户端)访问,但在JBoss-eap-6及更高版本中不可用。

请参阅https://developer.jboss.org/thread/196876

答案 1 :(得分:0)

由于无法远程访问数据源,因此我没有尝试过。

Context context = new InitialContext(); DataSource dataSource = (DataSource)context.lookup("java:jboss/jdbc/mn572phOraDataSource");