【问题标题】:Cannot connect to postgresql using jdbc and tomcat. NamingException is thrown无法使用 jdbc 和 tomcat 连接到 postgresql。抛出命名异常
【发布时间】:2021-03-19 18:23:28
【问题描述】:

我想创建一个可以访问 PostgreSQL 数据库的 Jersey ReST API。我设置了Tomcat 8服务器并尝试访问本地PostgreSQL服务器,但它不起作用。

我的 context.xml(在文件夹“project_folder/main/webapp/META_INF/”中)看起来像

<Context>
    <Resource name="jdbc/mpmttest"
              auth="Container"
              driverClassName="org.postgresql.Driver"
              url="jdbc:postgresql://localhost:5432/mpm"
              user="postgres"
              password="pw"
              maxActive="15"
              maxIdle="3"/>
</Context>

我在 web.xml 中添加了以下内容

<resource-ref>
        <description>mpmt-database</description>
        <res-ref-name>jdbc/mpmttest</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

但是当我尝试查找数据库时,我得到了 NamingException。

我的源代码:

public class DatabaseHandler {

    private DataSource standardDatabase = null;

    public DatabaseHandler() {
        try {
            InitialContext initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            standardDatabase = (DataSource) envCtx.lookup("jdbc/mpmttest");
        } catch (NamingException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to create datasource for standard binding", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException
    {
        return standardDatabase.getConnection();
    }

}

堆栈跟踪显示在浏览器显示:


Root Cause

javax.naming.NamingException: Unexpected exception resolving reference [Root exception is java.lang.NullPointerException]
    org.apache.naming.NamingContext.lookup(NamingContext.java:856)
    org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    com.sap.ida.ae.mpmtservices.persistence.db.DatabaseHandler.<init>(DatabaseHandler.java:21)
    com.sap.ida.ae.mpmtservices.persistence.db.StdDatabaseProvider.getDatabaseHandler(StdDatabaseProvider.java:8)
    com.sap.ida.ae.mpmtservices.api.TestCaseAPI.createTable(TestCaseAPI.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.NullPointerException
    org.apache.naming.factory.ResourceFactory.getDefaultFactory(ResourceFactory.java:45)
    org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:92)
    javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    org.apache.naming.NamingContext.lookup(NamingContext.java:839)
    org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    com.sap.ida.ae.mpmtservices.persistence.db.DatabaseHandler.<init>(DatabaseHandler.java:21)
    com.sap.ida.ae.mpmtservices.persistence.db.StdDatabaseProvider.getDatabaseHandler(StdDatabaseProvider.java:8)
    com.sap.ida.ae.mpmtservices.api.TestCaseAPI.createTable(TestCaseAPI.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我在配置和编码方面玩了很多,但没有解决问题。似乎无法访问数据库。我可以关闭数据库服务器并遇到同样的问题。我不知道我能做些什么来解决这个问题。

【问题讨论】:

    标签: java postgresql jdbc tomcat8


    【解决方案1】:

    你能像这样在你的数据库处理程序中试一试吗:

    //Context envCtx = (Context) initCtx.lookup("java:comp/env");
    standardDatabase = (DataSource) initCtx.lookup("java:comp/env/jdbc/mpmpttest");
    

    我对所有运行 tomcat 8 和 9 的 web 应用程序都这样做

    【讨论】:

    • 没有任何变化,导致同样的错误
    猜你喜欢
    • 2020-01-22
    • 2015-09-21
    • 2012-10-19
    • 1970-01-01
    • 2015-02-19
    • 2021-11-13
    • 2019-09-28
    • 2013-05-09
    相关资源
    最近更新 更多