Connect Datasource to Java code for JBOSS EAP 6.4: Naming Exception

6.6k views Asked by At

I need to connect the datasource defined for jboss eap 6.4 to my java code.

My standalone.xml is:

    <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 has following:

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

My java code is:

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");

I think jboss-web.xml connects properly to JBoss datasource(Tested it by changing the jndi-name and it gives error)

I have added jboss-client.jar to the classpath:

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

I am unable to connect to the datasource from Java Code.

I am getting following exception:

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]

Any pointer to solve this will be highly helpful and appreciated.

Thanks.

2

There are 2 answers

0
shonky linux user On BEST ANSWER

Remote (client) access to datasources was available in JBoss-eap-5, but is not available in JBoss-eap-6 and beyond.

Refer to https://developer.jboss.org/thread/196876

0
Swaraj Shekhar On

As remote access to datasource is not available and hence I did not try that.

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