0

I'm trying to access to JNDI Oracle datasource through @Resource annotation in RestEasy webservice on Wildfly 10, but null is returned. It works with InitialContext.lookup method. This my java code:

@Path("/rest")
public class ServiceTest {
    @Resource(name = "jdbc/OracleDS") DataSource source1;
    @Resource(name = "java:/jdbc/OracleDS") DataSource source2;
    @Resource(lookup = "java:/jdbc/OracleDS") DataSource source3;

    @GET
    @Path("/test")
    @Produces(MediaType.TEXT_PLAIN)
    public String testJNDI () {
        try {
            javax.naming.Context initCtx = new InitialContext();
            DataSource source4 = (DataSource)initCtx.lookup("java:/jdbc/OracleDS");
            initCtx.close();
            return "" + source1 + " / " + source2 + " / " + source3 + " / " + source4;
        } catch (Exception error) {
            e.printStackTrace();
    }
}

This is my standalone.xml datasource definition:

<datasource jta="true" jndi-name="java:/jdbc/OracleDS" pool-name="OracleDS" enabled="true" use-ccm="true" statistics-enabled="true">
   <connection-url>
       jdbc:oracle:thin:@192.168.10.20:1521:log
   </connection-url>
   <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
   <driver>oracle</driver>
   <pool>
       <initial-pool-size>1</initial-pool-size>
       <max-pool-size>10</max-pool-size>
   </pool>
   <security>
       <user-name>user</user-name>
       <password>passw</password>
   </security>
   <validation>
       <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
       <background-validation>true</background-validation>
       <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
       <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
   </validation>
   <timeout>
        <blocking-timeout-millis>30000</blocking-timeout-millis>
        <idle-timeout-minutes>15</idle-timeout-minutes>
        <query-timeout>300</query-timeout>
   </timeout>
   <statement>
       <track-statements>nowarn</track-statements>
   </statement>
</datasource>

And this is my module.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.oracle">
    <resources>
        <resource-root path="ojdbc6.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

The service output is this:

null / null / null / org.jboss.as.connector.subsystems.datasources.WildFlyDataSource@4b1881

Why source4 founds the resource and others not? I expected that at least source3 would be like source4. Is something wrong in the @Resource definition?

Thanks

user1151816
  • 187
  • 4
  • 13

1 Answers1

2

Make sure you have the beans.xml in your WEB-INF dir.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

This will enable CDI in your application.

fhofmann
  • 847
  • 7
  • 15