无法使用jdbc和tomcat连接到postgresql.引发NamingException



我想创建一个Jersey ReST API,可以访问PostgreSQL数据库。我设置了Tomcat8服务器,并尝试访问本地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)

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

您能在您的数据库处理程序中尝试一下吗:

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

对于运行tomcat 8和9 的所有网络应用程序,我都是这样做的

相关内容

  • 没有找到相关文章

最新更新