我在 Windows 环境的本地计算机上将我的代码部署在 Tomcat 6.0.32 中。并且相同的WAR文件部署在Linux环境下远程机器的Tomcat 7.0.39中。
context.xml 的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/CentralizedPush">
<Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/postgres" password="commonpush12#" type="javax.sql.DataSource" url="jdbc:postgresql://10.40.5.59:5444/commonpush_database" username="commonpush"/>
</Context>
DBConnection.java 的代码如下:
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.slf4j.LoggerFactory;
public class DBConnection {
private static Connection con = null;
/**function is used for creating connection
* @return Connection object
*/
public static Connection getConnection() {
org.slf4j.Logger LOG = LoggerFactory.getLogger("Error");
try {
Driver d = (Driver) Class.forName(Constant.CLASS_NAME).newInstance();
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/postgres");
con = ds.getConnection();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
LOG.error("ClassNotFoundException" );
} catch (InstantiationException ex) {
ex.printStackTrace();
LOG.error("InstantiationException" );
} catch (IllegalAccessException ex) {
ex.printStackTrace();
LOG.error("IllegalAccessException" );
} catch (NamingException ex) {
ex.printStackTrace();
LOG.error("NamingException" );
} catch (SQLException ex) {
ex.printStackTrace();
LOG.error("SQLException in db connection" );
}
return con;
}
我在 postgres 中有我试图访问的数据库,其详细信息如下:
Name: CDCUCOMPUDB
Host: 10.40.5.59
Port: 5444
database name: commonpush_database
问题是当我在本地机器上部署代码时一切都很好,但是当我在 Linux 环境中的远程服务器上部署代码并且具有不同的 Tomcat 版本时,我收到以下错误:
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
请提出任何解决方案。