1 2 Previous Next 16 Replies Latest reply: Sep 29, 2012 12:44 PM by Wolf-Dieter Fink RSS

JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]

Kousik Rajendran Newbie

Hi,

 

I have successfully created and deployed a datasource in my jboss as 7. I tested with jboss CLI and I got the success message like

 

[standalone@localhost:9999 /] /subsystem=datasources/data-source=java\:jboss\/datasources\/Test:test-connection-in-pool
{
    "outcome" => "success",
    "result" => [true]
}

But, when I try to access the same data source using the java code

       

DataSource ds = null;
Context ctx = null;
String strDSName = "java:jboss/datasources/Test";
try {
    Properties env = new Properties();
    env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
    env.setProperty("java.naming.provider.url", "jnp://localhost:1099");
    ctx = new InitialContext(env);
    ds = (javax.sql.DataSource) ctx.lookup(strDSName);
} catch (Exception e) {
    e.printStackTrace();
}

 

I am getting this error

 

javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
          at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1302)
          at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1382)
          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:579)
          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
          at javax.naming.InitialContext.lookup(InitialContext.java:392)
          at c     om.aayuwiz.clinic.tests.KousikTests.main(KousikTests.java:42)
Caused by: java.net.SocketTimeoutException: Receive timed out
          at java.net.PlainDatagramSocketImpl.receive0(Native Method)
          at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
          at java.net.DatagramSocket.receive(DatagramSocket.java:712)
          at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1272)
          ... 5 more

 

I have not done any changes to my standalone.xml under \JBOSS_HOME\standalone\configuration directory.

 

Can someone help me out in this regards please. I have tried many forum threads and lot of blogs in and out of jboss.org sites. I am in a very critical situation to fix this issue ASAP.

 

Thanks in Advance. Please let me know if you need more information.

  • 1. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Wolf-Dieter Fink Master

    What version of JBoss do you use and what kind of client it is. The datasource should only be available inside the server and your code looks like a standalone client.

  • 2. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Kousik Rajendran Newbie

    I am using JBoss AS 7 (as given in my subject) and yes, I am running this JAVA program from another instance. It is not inside my JBoss Server.

  • 3. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Wolf-Dieter Fink Master

    What AS7?

    AS7.0 does not provide remote JNDI or remote EJB access.

  • 4. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Kousik Rajendran Newbie

    I am using AS 7.0.2 Final release. Can I run atleast with in local JBoss server (localhost)? If so what should be my steps to start JNDI service? (I cannot see any service started in port 1099 (JNDI port by default)).

  • 5. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    abiya Newbie

    I am facing similar issue. Did u get a solution for this?

     

    My datasource config in standalone.xml is as follows,

    <datasource jndi-name="java:jboss/env/myds" enabled="true" use-java-context="true" pool-name="OracleDS">

    Testing same from CLI was successful.

    And when I try to connect from client within same instance

     

          private static Hashtable _env;
                _env.put(Context.INITIAL_CONTEXT_FACTORY,
                        System.getProperty("org.jnp.interfaces.NamingContextFactory"));   
                _env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
                _env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                _env.put(Context.SECURITY_PRINCIPAL, "");
                _env.put(Context.SECURITY_CREDENTIALS, "");
         ctx = new InitialContext(_env);

     

    DataSource ds =

    ctx.lookup("java:jboss/env/myds"); throws following exception,

        javax.naming.NameNotFoundException: java:jboss/env/myds

    Please help me resolve this.

     

  • 6. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Kousik Rajendran Newbie

    Hi Abiya,

     

    All you need to do is a very simple arroach. As Wolf-Dieter Fink told

     

    The datasource should only be available inside the server and your code looks like a standalone client.

     

    You should try running only within JBoss AS 7 Environment. Which means, you cannot try connecting from a standalone Java code. You should try putting the code inside your web application and test.

     

    What you can do is, put this block inside a test servlet and deploy and test it. This should work. Note: there is no need for you to set any property.

     

    DataSource ds = null;

    Context ctx = null;

    try {

            String strDSName = "java:jboss/datasources/Test";

            ctx = new InitialContext();

            ds = (javax.sql.DataSource) ctx.lookup(strDSName);

    } catch (Exception e) {

    }

     

    Hope this works for you.

     

    You can check in deatail on how it works in here http://kousikraj.wordpress.com/2011/11/25/datasource-configuration-setup-for-jboss-as-7-with-example-of-postgresql/

  • 7. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    abiya Newbie

    Hi Kousik,

     

    Thanks for the reply.I tried and it works fine in servlet.I need to have lookup from filter's init() method which looks like a problem again. It throws javax.naming.NameNotFoundException. Any input on this would be of great help.

     

    Thanks,

    Abiya

  • 8. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Kousik Rajendran Newbie

    Hey Abiya,

     

    I tried in init() method of a servlet container too. it works fine. Here is the test servlet

     

    import java.io.IOException;

    import javax.naming.Context;

    import javax.naming.InitialContext;

    import javax.servlet.ServletException;

    import javax.servlet.annotation.WebServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import javax.sql.DataSource;

     

     

    @WebServlet("/test")

    public class TestGateway extends AbstractGateway {

     

              private static final long          serialVersionUID          = 5756805325624066368L;

     

              @Override

              public void init() throws ServletException {

                        super.init();

                        DataSource ds = null;

                        Context ctx = null;

                        try {

          String strDSName = "java:jboss/datasources/Test";

          ctx = new InitialContext();

          ds = (javax.sql.DataSource) ctx.lookup(strDSName);

          System.out.println("Success getting DS : " + ds.getClass());

                        } catch (Exception e) {

          System.out.println("Error getting DS : " + e);

      }

              }

     

    }

    When I execute http://localhost:8080/test/test

    I got my output like this in console.

    16:11:51,818 INFO  [stdout] (http--0.0.0.0-8080-2) Success getting DS : class org.jboss.jca.adapters.jdbc.WrapperDataSource

    So, I think there should be some problem in your Servlet or I am not able to get your problem. Can you detail a little bit in your case.

  • 9. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    abiya Newbie

    Hi Koushik,

     

    I was trying to access in the init method of startupservlet

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    public class StartupServlet extends

    HttpServlet

    {

     

     

    // Initializing Logger.

     

     

    //private static MLogger s_logger = new MLogger(ServeImagesServlet.class);

     

     

     

    /**

    * init method

    */

     

     

    public void init(ServletConfig a_config) throws
    ServletException

    {

     

    super

    .init(a_config);

    System.out.println("StartupServlet.init() called."

    );

     

    try

    {

    InitialContext ctx = new

    InitialContext();

    DataSource ds = (javax.sql.DataSource) ctx.lookup("java:jboss/datasources/Test");

     

    System.out.println("context info in StratupServlet init method...."

    +ds);

     

    }

     

    catch(NamingException e) {

     

     

    // TODO

    Auto-generated catch block

    e.printStackTrace();

    }

    }

     

    And I get the following error,

    17:58:09,306 ERROR [stderr] (MSC service thread 1-2) javax.naming.NameNotFoundException: java:jboss/datasources/Test

    17:58:09,306 ERROR [stderr] (MSC service thread 1-2)  at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:55)

    17:58:09,306 ERROR [stderr] (MSC service thread 1-2)  at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)

    17:58:09,306 ERROR [stderr] (MSC service thread 1-2)  at javax.naming.InitialContext.lookup(Unknown Source)

    17:58:09,306 ERROR [stderr] (MSC service thread 1-2)  at com.common.web.servlet.StartupServlet.init(StartupServlet.java:40)

     

     

     

    I get similar error when I try to access from init method of filter as well.

     

    I have another basic question,

    suppose I invoke a method in utility class from servlet.... will I be able to get the initialcontext using

    InitialContext ctx = new InitialContext(); in the utility class?

     

    Thanks,

    Abiya

     

  • 10. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    Kousik Rajendran Newbie

    Abiya,

     

    I am not seeing your datasource loaded before the deployment of your servlet. Are you sure that your datasource is loaded before your web app?

    because, in my case its working for startup servlets too. Checkout my output. Here, the datasource is loaded way back this web app is loaded.

     

    18:15:52,229 INFO  [org.jboss.web] (MSC service thread 1-2) registering web context: /core

    18:15:52,560 INFO  [stdout] (MSC service thread 1-15) Success getting DS : class org.jboss.jca.adapters.jdbc.WrapperDataSource

    18:15:52,562 INFO  [org.jboss.web] (MSC service thread 1-15) registering web context: /test

    18:15:52,584 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Replaced deployment "test.ear" with deployment "test.ear"

     

    And for your basic question, you can have it anywhere. Normally, I will not suggest you to have DB activities in servlet, use MVC implemetation.

  • 11. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    abiya Newbie

    Thanks for checking it at your end.

     

    My datasource is well bound before the servlet invocation. It fails only on load-on-startup servlets. Still checking for a solution.

     

    Regards,

    Abiya

  • 12. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    jaikiran pai Master

    Instead of doing a lookup:

     

    DataSource ds = (javax.sql.DataSource) ctx.lookup("java:jboss/datasources/Test");
    

     

    use injection:

     

    @Resource (mappedName="java:jboss/datasources/Test")
    private DataSource ds;
    

     

    That'll setup the appropriate dependencies between the servlet and the datasource.

  • 13. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    abiya Newbie

    Thanks for the reply Jaikiran. While using annotation I get the following exception during deployment,

     

    Servlet /common threw load() exception: java.lang.IllegalArgumentException: Can not set javax.sql.DataSource field com.metreo.common.web.servlet.StartupServlet.ds to com.metreo.common.web.servlet.StartupServlet
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) [:1.6.0_24]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) [:1.6.0_24]
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source) [:1.6.0_24]
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source) [:1.6.0_24]
    at java.lang.reflect.Field.set(Unknown Source) [:1.6.0_24]
    at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptor.java:64)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:152)
    at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:77)
    at org.jboss.as.web.deployment.component.WebComponentInstantiator$1.<init>(WebComponentInstantiator.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.component.WebComponentInstantiator.getReference(WebComponentInstantiator.java:55) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.WebInjectionContainer.instantiate(WebInjectionContainer.java:99) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:78) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:72) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1156) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3631) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3844) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [:1.6.0_24]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [:1.6.0_24]
    at java.lang.Thread.run(Unknown Source) [:1.6.0_24]

     

    My servlet code is as follows,

     

     

     

     

     

    public

     

    class StartupServlet extends

    HttpServlet

    {

     

     

     

     

    @Resource(mappedName="java:jboss/datasources/weblogic.jdbc.jts.demopool")

     

    private DataSource ds;

    ....

    }

     

    I am new to annotations and not sure what is wrong over here.

     

    Thanks,

    Abiya

     

  • 14. Re: JNDI Lookup fails in JBoss AS 7 [java.naming.CommunicationException]
    rakesh c Newbie

    hi Wolf-Dieter Fink,

     

    Please confirm whether Jboss oracle connection pooling supported in Jboss AS 6 for standalone client? Its very urgent need for me..

1 2 Previous Next