简体   繁体   English

Java App无法从PostgreSQL加载数据

[英]Java App Does Not Load Data from PostgreSQL

My Java app works fine locally, but now when I have deployed it on an EC2 instance (Ubuntu 14, Tomcat 8, PostgreSQL 9.3), I am getting the following error (see below). 我的Java应用程序在本地运行良好,但是现在当我将其部署到EC2实例(Ubuntu 14,Tomcat 8,PostgreSQL 9.3)上时,出现以下错误(请参见下文)。 The back-end was written by another developer who is no longer part of the team. 后端由不再属于团队的另一位开发人员编写。 Basically, it needs to display some data once a value is chosen from a dropdown menu. 基本上,一旦从下拉菜单中选择了一个值,它就需要显示一些数据。

I've been Google-ing this issue like crazy but have found no solution. 我一直在Google上发疯,但没有找到解决方案。 I was thinking it may be related to the jdbc:postgresql://localhost:5432/dbName . 我以为它可能与jdbc:postgresql://localhost:5432/dbName I even tried inserting the actual IP instead of localhost but still no luck. 我什至尝试插入实际IP而不是localhost但还是没有运气。 To me, it seems like an issue with connecting with the database rather than with the code. 对我来说,连接数据库而不是代码似乎是一个问题。 As I mentioned, it is running fine locally on my computer. 如前所述,它在我的计算机上本地运行良好。

Any guidance will be greatly appreciated as I am not a Java expert. 因为我不是Java专家,所以任何指导都将不胜感激。 Thanks for your help. 谢谢你的帮助。

The full error log is below: 完整的错误日志如下:

java.lang.NullPointerException
com.app.data.ConnectionPool.freeConnection(ConnectionPool.java:52)
com.app.data.appDB.getData(appDB.java:155)
com.app.servlets.GetAppServlet.processRequest(GetAppServlet.java:42)
com.app.servlets.GetAppServlet.doGet(GetAppServlet.java:67)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

Based on what little you have provided, my best guess is that the connection.close() method you mention is being called in a finally block where there isn't any check to see whether connection is null . 根据您提供的内容,我的最佳猜测是,您提到的connection.close()方法是在finally块中调用的,该块中没有任何检查以查看connection是否为null

I imagine your code looks something like the following: 我想你的代码看起来像下面这样:

Connection connection = null;
try {
    connection = getDatabaseConnectionSomehow(...);
    // do stuff with database
}
finally {
    connectionPool.freeConnection(connection);
}

If the calll to getDatabaseConnectionSomehow(...) throws an exception, then connection will be null when the finally block is entered. 如果getDatabaseConnectionSomehow(...)的calll引发异常,则在进入finally块时, connection将为null The code I presented above doesn't check to see whether connection is null , and if your freeConnection method also fails to check whether the connection is null before closing it, you will get a NullPointerException. 我上面提供的代码不会检查connection是否为null ,并且如果您的freeConnection方法在关闭连接之前也无法检查连接是否为null ,则会得到NullPointerException。

An exception thrown from within a finally block will replace any exception thrown within the corresponding try block. finally块中引发的异常将替换在相应try块中引发的任何异常。 So in your case, you don't see the real exception getting the database connection because the NullPointerException gets in the way. 因此,在您的情况下,您看不到真正的异常获取数据库连接,因为NullPointerException妨碍了您。

Hopefully, adding a suitable null-check should help you figure out what the real problem is here. 希望添加适当的空检查应该可以帮助您找出真正的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM