13 Replies Latest reply: Jan 4, 2014 10:35 AM by Wolf-Dieter Fink RSS

JNDI issue in as7

Oved Ourfali Newbie

Hello,

 

I have an ear, containing a war. In the WEB-INF/applicationContext.xml file I have:

 

  <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">

    <property name="environment">

      <props>

        <prop key="java.naming.factory.initial">

          org.jnp.interfaces.NamingContextFactory

        </prop>

        <prop key="java.naming.provider.url">

          jnp://localhost:${application.jndi.port}

        </prop>

        <prop key="java.naming.factory.url.pkgs">

          org.jboss.naming:org.jnp.interfaces

        </prop>

      </props>

    </property>

  </bean>

 

When starting the application I get the following error from the WAR:

 

09:59:24,881 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-1) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'backend' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.rhevm.ear.restapi.war:main" from Service Module Loader

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) [spring-2.5.6.jar:]

        at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_26]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423) [spring-2.5.6.jar:]

        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) [spring-2.5.6.jar:]

        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) [spring-2.5.6.jar:]

        at org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:44) [resteasy-spring-2.1.0.GA.jar:]

        at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3368) [jbossweb-7.0.0.CR4.jar:7.0.0.CR1]

        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3821) [jbossweb-7.0.0.CR4.jar:7.0.0.CR1]

        at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.0.CR1.jar:7.0.0.CR1]

        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)

        at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)

        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_26]

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_26]

        at java.lang.Thread.run(Thread.java:662) [:1.6.0_26]

Caused by: javax.naming.NamingException: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.rhevm.ear.restapi.war:main" from Service Module Loader

        at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:58)

        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:664) [:1.6.0_26]

        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) [:1.6.0_26]

        at javax.naming.InitialContext.init(InitialContext.java:223) [:1.6.0_26]

        at javax.naming.InitialContext.<init>(InitialContext.java:197) [:1.6.0_26]

        at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:137) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.getContext(JndiTemplate.java:104) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335) [spring-2.5.6.jar:]

        ... 24 more

 

09:59:24,886 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/rhevm-api]] (MSC service thread 1-1) Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.spring.SpringContextLoaderListener: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'backend' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.rhevm.ear.restapi.war:main" from Service Module Loader

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) [spring-2.5.6.jar:]

        at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_26]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423) [spring-2.5.6.jar:]

        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) [spring-2.5.6.jar:]

        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) [spring-2.5.6.jar:]

        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) [spring-2.5.6.jar:]

        at org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:44) [resteasy-spring-2.1.0.GA.jar:]

        at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3368) [jbossweb-7.0.0.CR4.jar:7.0.0.CR1]

        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3821) [jbossweb-7.0.0.CR4.jar:7.0.0.CR1]

        at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.0.CR1.jar:7.0.0.CR1]

        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)

        at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)

        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_26]

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_26]

        at java.lang.Thread.run(Thread.java:662) [:1.6.0_26]

Caused by: javax.naming.NamingException: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.rhevm.ear.restapi.war:main" from Service Module Loader

        at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:58)

        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:664) [:1.6.0_26]

        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) [:1.6.0_26]

        at javax.naming.InitialContext.init(InitialContext.java:223) [:1.6.0_26]

        at javax.naming.InitialContext.<init>(InitialContext.java:197) [:1.6.0_26]

        at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:137) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.getContext(JndiTemplate.java:104) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200) [spring-2.5.6.jar:]

        at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369) [spring-2.5.6.jar:]

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335) [spring-2.5.6.jar:]

        ... 24 more

 

 

I saw in one of the discussions here that NamingContextFactory does not exist in as7.

 

What would be the alternative configuration in my war?

 

Thank you,

Oved

  • 1. Re: JNDI issue in as7
    jorge.middleton Newbie

    I have the same problem. Did you find any solution?

  • 2. Re: JNDI issue in as7
    Oved Ourfali Newbie

    I managed to overcome this error by initializing the jndiTemplate bean with no environment, as I understand that the jndi lookups should work in as7 that way.

     

    So the initialization worked, but I couldn't make the "lookup" find the beans I needed, so either my configuration was wrong, and you do need to provide something to the jndi template, or my lookup string was wrong.

     

    Try initializing the jndi template with no environment.

    Let me know how you progress, as I haven't found a complete solution yet.

  • 3. Re: JNDI issue in as7
    jorge.middleton Newbie

    Yesterday I tried without environment variables and the error changed to a something like "the Context is read only" so I thought that I could connect but I have to change something in my datasource configuration. After that at night I found this http://planet.jboss.org/view/feed.seam?name=jbossas7&from=0 and I saw that Jboss only recognise jndi with java:/ or java:jboss/ so today I'll try it.

     

    Today I will try this and if it works I'll tell you

     

    Thanks

    Jorge

  • 4. Re: JNDI issue in as7
    Gonne Martens Novice

    Instead of using no environment it works for me with the following InitialContextFactory:

     

    java.naming.factory.initial = org.jboss.as.naming.InitialContextFactory

     

    Kind regards,

    Gonne

  • 5. Re: JNDI issue in as7
    Oved Ourfali Newbie

    I'll test it.

    Thank you very much for your help.

     

    Oved

  • 6. Re: JNDI issue in as7
    Gerry Matte Novice

    The top level contexts created by jboss 7 are shown below.  My code to lookup and tests a DataSource is also shown.

    java:app

    AppName    org.jboss.as.naming.context.ModularReference

     

     

    java:comp

    Validator    org.jboss.as.naming.context.ModularReference

    UserTransaction    org.jboss.as.naming.context.ModularReference

    ValidatorFactory    org.jboss.as.naming.context.ModularReference

    ModuleName    org.jboss.as.naming.context.ModularReference

    TransactionSynchronizationRegistry    org.jboss.as.naming.context.ModularReference

     

     

    java:module

    Validator    org.jboss.as.naming.context.ModularReference

    UserTransaction    org.jboss.as.naming.context.ModularReference

    ValidatorFactory    org.jboss.as.naming.context.ModularReference

    ModuleName    org.jboss.as.naming.context.ModularReference

    TransactionSynchronizationRegistry    org.jboss.as.naming.context.ModularReference

     

     

    java:global

     

     

    java:jboss

    jaas    org.jboss.as.naming.context.ModularReference

    TransactionManager    org.jboss.as.naming.context.ModularReference

    datasources    javax.naming.Context

    TransactionSynchronizationRegistry    org.jboss.as.naming.context.ModularReference

     

     

    java:jboss/datasources

    TestDS    org.jboss.as.naming.context.ModularReference

    ExampleDS    org.jboss.as.naming.context.ModularReference

     

    ========================================================================================

    my code to lookup and test a DataSource is:

     

    InitialContext ctx = new InitialContext();

    Context envCtx = (Context) ctx.lookup("java:");

    String dsname="java:jboss/datasources/TestDS";

    ds = (DataSource) ctx.lookup("java:jboss/datasources/TestDS");

    Connection con = ds.getConnection();

    ResultSet rs = con.createStatement().executeQuery("select count(*) from Persons;");

  • 7. Re: JNDI issue in as7
    jorge.middleton Newbie

    I can connect successful

     

    Thanks.

    Jorge

  • 8. Re: JNDI issue in as7
    Juergen Zimmermann Master

    Gerry, are you really sure that you have "java:comp/UserTransaction" and "java:module/UserTransaction"? I'm getting a NameNotFoundException for each of them.

  • 9. Re: JNDI issue in as7
    jaikiran pai Master

    Those jndi names should be used within a "component" (like EJB, servlet etc...). Using those in non-managed classes will not work, since they are component specific jndi names.

  • 10. Re: JNDI issue in as7
    Juergen Zimmermann Master

    OK, got it. Now, I'm using a singleton which can provide the UserTransaction object. Thank you very much!

  • 11. Re: JNDI issue in as7
    Borisa Zivkovic Newbie

    java.naming.factory.initial=org.jboss.as.naming.InitialContextFactory

     

    worked for me too!

     

    thanks a lot

  • 12. Re: JNDI issue in as7
    Gulam Samdani Newbie

    Hi ,

     

    I want to access jdbc datasource from standalone client :

     

    //<datasource jndi-name="java:jboss/datasources/dream" pool-name="MySQLPool" enabled="true" use-java-context="false">

       

        public static void main(String[] args) throws SQLException {

       DataSource cname = null;
       Context context = null;

       

       try {
       Properties properties = new Properties();
       properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
       context = new InitialContext(properties);
       cname = (DataSource) context.lookup("java:jboss/datasources/dream");
       Connection connection = cname.getConnection();
       System.out.println("connection-----masum : "+ connection.isClosed());
       } catch (NamingException e) {
       e.printStackTrace();
       }

       

        }

    ========================================================================================================

     

    Error :

     

    javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory

    [Root exception is java.lang.ClassNotFoundException: org.jboss.as.naming.InitialContextFactory]

  • 13. Re: JNDI issue in as7
    Wolf-Dieter Fink Master

    Hi Gulam,

     

    please do not cross post in different threads!

    I answered it in your other post.

    Also you should better open a new thread instead of use old ones with different meaning and maybe still marked as answered!