org.postgresql.util.PSQLException:连接尝试失败

时间:2013-04-04 08:26:29

标签: java web-services postgresql jdbc

我使用带有 JDBC 连接的java web服务( apache axis )连接到postgres数据库以获取数据。但是在大多数情况下,我突然得到一个org.postgresql.util.PSQLException: The connection attempt failed.的例外,有时它工作正常。在这里,我使用了许多预备语句。我的示例代码是

Connection connection=null;
try
{
    Class.forName(driver);
    connection = DriverManager.getConnection(url, username, password);
    ps1=connection.prepareStatement("select * from emp");
    rs1=ps1.executeQuery();
    while(rs1.next())
    {            
        ps2=connection.prepareStatement("select * from dept where dept_no="+rs1.getInt("dept_no"));
        rs2=ps2.executeQuery();
        while(rs2.next())
        {
            ................
            ................
        }
    }
}
catch(Exception e)
{
    System.out.println("Exception occurred is"+e.getMessage());            
}
finally
{
    try{if(rs1!=null)rs1.close();}catch(Exception e){ System.out.println("*1closing error--->"+e);}
    try{if(ps1!=null)ps1.close();}catch(Exception e){ System.out.println("**1closing error--->"+e);}
    try{if(rs2!=null)rs2.close();}catch(Exception e){ System.out.println("*2closing error--->"+e);}
    try{if(ps2!=null)ps2.close();}catch(Exception e){ System.out.println("**2closing error--->"+e);}
    try{if(connection!=null)connection.close();}catch(Exception e){ System.out.println("***closing error--->"+e);}
}

关于此异常的堆栈跟踪是

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:137)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:124)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:386)
    at org.postgresql.Driver.connect(Driver.java:260)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.codon.service.WareHouseServer.get_picks(WareHouseServer.java:7415)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
    at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
    at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
    at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
    at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
    at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
    at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
    at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
    at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:135)
    at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:104)
    at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:73)
    at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:259)
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:254)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:95)
    ... 37 more

我检查了postgres日志,我在不同情况下发现了以下语句

1.警告:工人花了太长时间才开始;取消。

2.不能重新连接到共享内存(key = ....,addr = .....):487。

3.无法从客户端接收数据:由于目标计算机主动拒绝,因此无法建立连接。

4.客户端连接上的'Exxpected EOF。

请帮助我。谢谢提前

3 个答案:

答案 0 :(得分:8)

真正的问题是:

Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read

当Java试图从中读取连接时,连接已关闭。这可能是由以下原因引起的:

  • 正在重启的PostgreSQL服务器
  • 您已连接到已被终止的PostgreSQL后端
  • 你连接到崩溃的PostgreSQL后端
  • 狡猾的网络连接
  • 状态不佳的防火墙
  • 空闲连接超时NAT防火墙/路由器的NAT连接表

......可能还有更多。检查PostgreSQL服务器日志,看看那里是否有任何信息;还考虑使用像Wireshark这样的工具进行一些网络跟踪。

答案 1 :(得分:0)

尝试 chekin Postgresql 版本,在我的情况下,我部署了我的应用程序:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.16</version>
    </dependency>

但是在本地我用的是 42.2.18 并且工作正常,所以检查版本后我不知道,所以你应该尝试其他版本。

答案 2 :(得分:0)

已为我修复:

跳转到 pg_hba.conf 文件并在 IPV4 连接下将其 127.xxxx 更改为 0.0.0.0/0

再次检查:跳到 postgresql.conf 并将 listenaddresses 更改为 *(我确定您知道需要先删除起始 # 才能使它工作)

祝你好运!