【问题标题】:InitialContext lookup("java:comp/env") failsInitialContext 查找(“java:comp/env”)失败
【发布时间】:2020-08-29 09:14:13
【问题描述】:

创建了一个dao层,其中Datasource对象在预处理操作中创建

...
public static DataSource  pcpds;
    static {
        try {
        InitialContext ic = new InitialContext();
        Context c = (Context) ic.lookup("java:comp/env");
        pcpds = (DataSource) c.lookup("jdbc/reqtracker");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
...

数据源属性写在 Tomcat-9 容器的 context.xml 中

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
<Resource 
    name="jdbc/reqtracker" auth="Container" type="org.apache.tomcat.jdbc.pool.DataSource" 
    maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="postgres" 
    password="postgress" driverClassName="org.postgresql.Driver" 
    databasePlatform="org.hibernate.dialect.Postgresql9Dialect" url="jdbc:postgresql://localhost:5432/reqtracker"/>
</Context>

我得到的例外是

javax.naming.NamingException: Could not create resource instance
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:98)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:857)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:174)

在windows上可以正常工作,但是在linux下就不行了,

【问题讨论】:

  • 为什么要使用两个查找,而一个会这样做?
  • @user207421 我对“java:comp/env/jdbc/reqtracker”使用了单一查找,但仍然是相同的异常

标签: java linux tomcat jndi tomcat9


【解决方案1】:
Can you add bellow snippet in web.xml 

 <resource-ref>
        <description>Postgres Test</description>
        <res-ref-name>jdbc/reqtracker</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
 </resource-ref>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2012-07-22
    • 2012-09-23
    • 1970-01-01
    • 2011-11-19
    相关资源
    最近更新 更多