Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

spring - javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context

I'm developing a spring application which uses tomcat server managed connection pool.

I defined datasource in tag in context.xml of tomcat: (i am using spring 2.0.7) in context.xml:

<context>
    <Resource name="jdbc/irb_prod"
    auth="Container"
    type="javax.sql.DataSource"
    factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
    username="xxx"
     password="xxx"
     driverClassName="oracle.jdbc.driver.OracleDriver"
     url="jdbc:oracle:thin:@xxx:1521:xxx"
     maxWait="1000"
     removeAbandoned="true"
     maxActive="5"
     maxIdle="5"
     removeAbandonedTimeout="60"
     logAbandoned="true"/>
</context>

And in my ApplicationContext.xml(i.e in spring configuration file), the code is:

     <bean id="dataSource" 
     class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
    <value>java:comp/env/jdbc/irb_prod</value>
    </property>
    </bean>


 <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource"><ref local="dataSource"/></property>
    </bean>

When the application starts, I get the following errors:

javax.naming.NameNotFoundException: Name java:/comp/env/mypool is not bound in this              Context
org.apache.naming.NamingContext.lookup(NamingContext.java:803)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
javax.naming.InitialContext.lookup(Unknown Source)
org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:132)
org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:130)
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:155)
org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:93)
org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java :197)
org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:184)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInit         ialization(AbstractApplicationContext.java:736)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractAppli  cationContext.java:369)
org.springframework.context.support.ClassPathXmlApplicationContext.<init> (ClassPathXmlApplicationContext.java:123)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>  (ClassPathXmlApplicationContext.java:66)
MainServlet.init(MainServlet.java:21)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java :188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

please help me to solve this problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I faced similar problem with tomcat and did the following in eclipse to solve it:

  1. In the server's context.xml, included the jdbc connection details.
  2. Stop the server
  3. Clean the project(Project --> Clean) , the tomcat server (Server--> Right Click --> Clean) and Tomcat work directory (Server--> Right Click--> Clean Tomcat Work Directory).
  4. Start the server and run.

Even after maintaining the details in context.xml, sometimes it might not get reflected. In such cases follow step#3 and hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...