0

Error log:

        SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appDatasource' defined in ServletContext resource [/WEB-INF/root-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/test] is not bound in this  Context. Unable to find [jdbc].
    at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.NameNotFoundException: Name [jdbc/test] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 21 more

My web.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>RestfulWebService</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      <!-- The definition of the Root Spring Container shared by all Servlets and Filters - Starts -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/root-context.xml</param-value>
      </context-param>
      <!-- The definition of the Root Spring Container shared by all Servlets and Filters - Ends -->  

    <!-- Creates the Spring Container shared by all Servlets and Filters - Starts -->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- User Session Handling - Starts 
      <listener>
          <listener-class>com.ericsson.utility.UserSessionListener</listener-class>
      </listener>-->
      <session-config>
            <session-timeout>45</session-timeout>
      </session-config>
    <!-- User Session Handling - Ends -->
    <!-- Spring Processes application requests -Starts -->
      <servlet>
          <servlet-name>OredooWalletServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
              /WEB-INF/EngineConfig/rest-context.xml
              </param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>OredooWalletServlet</servlet-name>
          <url-pattern>/owp/*</url-pattern>
      </servlet-mapping>
    </web-app>

My root-context.xml

                <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

                <context:annotation-config></context:annotation-config>

             <!--  Read from Properties file 
             <bean id="reportingProperties"
                    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                    <property name="locations">
                        <list>
                            <value>classpath*:owpsimulator.properties</value>
                        </list>
                    </property>
                    <property name="ignoreUnresolvablePlaceholders" value="true"/>
            </bean>  -->
             <!-- Create DataSource Bean-Starts -->
                <bean id="appDatasource" class="org.springframework.jndi.JndiObjectFactoryBean">
                    <property name="jndiName" value="java:comp/env/jdbc/test"/>
                </bean>  
             <!-- Create DataSource Bean-Ends -->   
            <!-- Create DataSource Bean-Ends -->   
            <!-- JdbcTemplate -Starts -->
                   <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
                     <property name="fetchSize" value="100"/>
                     <property name="dataSource" ref="appDatasource"/>
                   </bean>  
             <!-- JdbcTemplate -Ends -->
             <!-- NamedParameterJdbcTemplate -Starts -->
                   <bean id="namedParameterJdbcTemplate"  class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
                      <constructor-arg ref="jdbcTemplate"/>
                   </bean>  
             <!-- NamedParameterJdbcTemplate -ends -->

            </beans>

I have created a REST web service with spring, mysql with jdbc template. I have added mysql connector jar. I have included mysql in pom.xml too.

In the tomcat server.xml I have added <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/test" password="root" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/test" username="root"/>.

But still I am unable to find the source of this error.

After adding<ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSource" /> to the tomcat's context.xml, i am not getting the exception javax.naming.NameNotFoundException, but my ResourceMapping is not working.

Sp1
  • 469
  • 2
  • 8
  • 16
  • Are you sure? We put Resource definitions inside `` in server.xml. – Sp1 Mar 15 '17 at 11:56
  • `Tomcat provides a number of Tomcat specific options for JNDI resources that cannot be specified in web.xml. These include closeMethod that enables faster cleaning-up of JNDI resources when a web application stops and singleton that controls whether or not a new instance of the resource is created for every JNDI lookup. To use these configuration options the resource must be specified in a web application's element or in the element of $CATALINA_BASE/conf/server.xml.` quoting from [link](https://tomcat.apache.org/tomcat-8.5-doc/jndi-resources-howto.html) – Sp1 Mar 15 '17 at 12:09

0 Answers0