InitialContext lookup("java:compenv") 失败。

问题描述 投票:0回答:1

创建了一个Daao层,其中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下就不行了。

java linux tomcat jndi tomcat9
1个回答
-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>
© www.soinside.com 2019 - 2024. All rights reserved.