3 Replies Latest reply on Aug 18, 2014 12:03 AM by jaikiran pai

    Unable to Access Local WildFly 8 DataSource with JNDI

    Bryson Dunn Newbie

      Hi Everyone,

       

      I am having trouble accessing a WildFly 8 HSQLDB DataSource with JNDI. I am able to connect and use the DataSource with the @Resource annotation, however the JNDI lookup fails. For example:

      @Resource(lookup="java:jboss/datasources/MyDS")
      private DataSource myDs;
      
      try {
           Connection con = myDs.getConnection();
           System.out.println("This works!! Schema is: " + con.getSchema());
      
           InitialContext ictx = new InitialContext();
           DataSource jds = (DataSource) ictx.lookup("java:jboss/datasources/MyDS");
           Connection jcon = jds.getConnection();
           System.out.println("This doesn't work... " + jcon.getSchema());
      
           }
           catch (SQLException e) { e.printStackTrace(); }
           catch (NamingException e) { e.printStackTrace(); }
      
      
      
      


      Right now I'm trying to get this working within a Servlet (where @Resource works) but I need to access this DataSource in helper classes as well, which requires JNDI.

      Standalone.xml DataSource:

      <datasource jndi-name="java:jboss/datasources/MyDS" pool-name="MyDS" enabled="true" use-java-context="true" spy="false" use-ccm="true">
            <connection-url>jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}mydb;ifexists=true;shutdown=true;</connection-url>
            <driver>hsqldb</driver>
            <pool>
                 <min-pool-size>10</min-pool-size>
                 <max-pool-size>100</max-pool-size>
                 <prefill>true</prefill>
                 <flush-strategy>FailingConnectionOnly</flush-strategy>
            </pool>
            <security>
                 <user-name>sa</user-name>
                 <password>secret</password>
            </security>
            <statement>
                 <prepared-statement-cache-size>32</prepared-statement-cache-size>
                 <share-prepared-statements>true</share-prepared-statements>
            </statement>
      </datasource>
      
      
      

       

      Thanks in advance for the help!

        • 1. Re: Unable to Access Local WildFly 8 DataSource with JNDI
          jaikiran pai Master

          Please post the entire exception stackrace and which exact WildFly 8 version is this?

          • 2. Re: Unable to Access Local WildFly 8 DataSource with JNDI
            Bryson Dunn Newbie

            Should have specified - it's 8.0.0 Final. Below is the entire stacktrace. I'm going to upgrade to 8.1 and see if that does the trick. I'll post back here with an update.

             

            [0m [0m09:07:29,193 INFO  [stdout] (default task-1) This works!! Schema is: PUBLIC
            [0m [32m09:07:29,194 DEBUG [jndi] (default task-1) InitialContextFactory.getInitialContext()
            [0m [32m09:07:29,194 DEBUG [jndi] (default task-1) Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@8c4
            [0m [32m09:07:29,195 DEBUG [jndi] (default task-1) Looking up name="java:jboss/datasources/MyDS"
            [0m [31m09:07:29,219 ERROR [stderr] (default task-1) javax.naming.NameNotFoundException; remaining name 'java:jboss/datasources/MyDS'
            [0m [31m09:07:29,220 ERROR [stderr] (default task-1) at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:490)
            [0m [31m09:07:29,220 ERROR [stderr] (default task-1) at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:536)
            [0m [31m09:07:29,221 ERROR [stderr] (default task-1) at javax.naming.InitialContext.lookup(InitialContext.java:411)
            [0m [31m09:07:29,222 ERROR [stderr] (default task-1) at javax.naming.InitialContext.lookup(InitialContext.java:411)
            [0m [31m09:07:29,223 ERROR [stderr] (default task-1) at com.kbdunn.nweb.servlet.VdnServlet.servletInitialized(VdnServlet.java:51)
            [0m [31m09:07:29,224 ERROR [stderr] (default task-1) at com.vaadin.server.VaadinServlet.init(VaadinServlet.java:103)
            [0m [31m09:07:29,224 ERROR [stderr] (default task-1) at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:208)
            [0m [31m09:07:29,225 ERROR [stderr] (default task-1) at io.undertow.servlet.core.ManagedServlet.getServlet(ManagedServlet.java:151)
            [0m [31m09:07:29,229 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:84)
            [0m [31m09:07:29,231 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
            [0m [31m09:07:29,232 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
            [0m [31m09:07:29,232 ERROR [stderr] (default task-1) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
            [0m [31m09:07:29,232 ERROR [stderr] (default task-1) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
            [0m [31m09:07:29,233 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
            [0m [31m09:07:29,235 ERROR [stderr] (default task-1) at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52)
            [0m [31m09:07:29,235 ERROR [stderr] (default task-1) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
            [0m [31m09:07:29,236 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
            [0m [31m09:07:29,236 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
            [0m [31m09:07:29,237 ERROR [stderr] (default task-1) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
            [0m [31m09:07:29,237 ERROR [stderr] (default task-1) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
            [0m [31m09:07:29,238 ERROR [stderr] (default task-1) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
            [0m [31m09:07:29,239 ERROR [stderr] (default task-1) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
            [0m [31m09:07:29,240 ERROR [stderr] (default task-1) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
            [0m [31m09:07:29,240 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
            [0m [31m09:07:29,240 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
            [0m [31m09:07:29,241 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
            [0m [31m09:07:29,241 ERROR [stderr] (default task-1) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
            [0m [31m09:07:29,241 ERROR [stderr] (default task-1) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168)
            [0m [31m09:07:29,242 ERROR [stderr] (default task-1) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687)
            [0m [31m09:07:29,243 ERROR [stderr] (default task-1) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            [0m [31m09:07:29,243 ERROR [stderr] (default task-1) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            [0m [31m09:07:29,244 ERROR [stderr] (default task-1) at java.lang.Thread.run(Thread.java:745)
            
            • 3. Re: Unable to Access Local WildFly 8 DataSource with JNDI
              jaikiran pai Master
              1. [0m [31m09:07:29,219 ERROR [stderr] (default task-1) javax.naming.NameNotFoundException; remaining name 'java:jboss/datasources/MyDS' 
              2. [0m [31m09:07:29,220 ERROR [stderr] (default task-1) at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:490) 
              3. [0m [31m09:07:29,220 ERROR [stderr] (default task-1) at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:536) 
              4. [0m [31m09:07:29,221 ERROR [stderr] (default task-1) at javax.naming.InitialContext.lookup(InitialContext.java:411) 

              It looks like you have a jndi.properties in your application which is being picked up and that jndi.properties perhaps has an InitialContextFactory which use Eclipse JNDI as the JNDI provider. You will have to remove that jndi.properties. Or it maybe a case that you are passing these properties to the InitialContext constructor (but that's a bit unlikely).