简体   繁体   English

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

[英]org.postgresql.util.PSQLException: The connection attempt failed

I am connecting to postgres data base using java web services ( apache axis ) with JDBC connections to get the data.But suddenly in most of times i am getting an exception of org.postgresql.util.PSQLException: The connection attempt failed.我正在使用带有JDBC连接的 java web 服务( apache 轴)连接到 postgres 数据库以获取数据org.postgresql.util.PSQLException: The connection attempt failed. and some times it is working fine.有时它工作正常。 Here I am using many prepared statements.在这里,我使用了许多准备好的语句。 My sample code is我的示例代码是

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);}
}

stack trace about this exception is关于此异常的堆栈跟踪是

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

I checked the postgres logs and i found the following statements in different cases我检查了 postgres 日志,发现在不同情况下有以下语句

1.WARNING: worker took too long to start; 1.警告:worker 启动时间过长; cancelled.取消。

2.could not reattach to shared memory (key=...., addr=.....): 487. 2.无法重新附加到共享内存(key=...., addr=.....):487。

3.could not receive data from client: No connection could be made because the target machine actively refused it. 3.could not receive data from client:无法建立连接,因为目标机器主动拒绝。

4.unexpected EOF on client connection. 4. 客户端连接出现意外 EOF。

please help me.Thanks In advance请帮助我。提前致谢

The real problem is:真正的问题是:

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

The connection was closed when Java tried to read from it.当 Java 尝试从中读取时,连接已关闭。 This can be caused by:这可能是由以下原因引起的:

  • The PostgreSQL server being restarted正在重启的 PostgreSQL 服务器
  • The PostgreSQL backend you were connected to being terminated您连接的 PostgreSQL 后端被终止
  • The PostgreSQL backend you were connected to crashing您连接到的 PostgreSQL 后端崩溃
  • Dodgy network connection狡猾的网络连接
  • Badly behaved stateful firewalls行为不良的状态防火墙
  • Idle connections timing out of the NAT connection tables of NAT firewall/routers空闲连接超时 NAT 防火墙/路由器的 NAT 连接表

... and probably more. ……而且可能更多。 Check the PostgreSQL server logs to see if there's anything informative there;检查 PostgreSQL 服务器日志以查看那里是否有任何信息; also consider doing some network tracing with a tool like Wireshark.还可以考虑使用 Wireshark 之类的工具进行一些网络跟踪。

Fixed for me:为我固定:

hop onto the pg_hba.conf file and under IPV4 connection change its 127.xxxx to the 0.0.0.0/0跳转到pg_hba.conf文件并在 IPV4 连接下将其127.xxxx更改为0.0.0.0/0

double check: hop onto the postgresql.conf and change the listenaddresses to * (im sure you know that you need to delete the starting # first in order to make it work)仔细检查:跳到postgresql.conf并将listenaddresses更改为* (我确定你知道你需要先删除起始#以使其工作)

Good luck!祝你好运!

Try chekin Postgresql version, in my case I deployed my app with:尝试 chekin Postgresql 版本,在我的情况下,我部署了我的应用程序:

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

But in local I used 42.2.18 and worked fine, so I din't know after checking version, so you should try another versions maybe.但是在本地我使用了42.2.18并且运行良好,所以检查版本后我不知道,所以你应该尝试其他版本。

暂无
暂无

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

相关问题 org.postgresql.util.PSQLException:连接尝试失败。 引起:java.net.UnknownHostException - org.postgresql.util.PSQLException: The connection attempt failed. Caused by: java.net.UnknownHostException 无法连接到 postgres Scala slick org.postgresql.util.PSQLException:连接尝试失败 - Can't connect to postgres Scala slick org.postgresql.util.PSQLException: The connection attempt failed ClassNotFoundException:org.postgresql.util.PSQLException - ClassNotFoundException: org.postgresql.util.PSQLException org.postgresql.util.PSQLException - 2019 - org.postgresql.util.PSQLException - 2019 Docker + Spring 引导 + Postgres-org.postgresql.util.PSQLException:连接尝试失败 - Docker + Spring Boot + Postgres-org.postgresql.util.PSQLException: The connection attempt failed 引起:org.postgresql.util.PSQLException:致命:剩余的连接槽是为非复制超级用户连接保留的 - Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections 如何修复 org.postgresql.util.PSQLException:致命:用户密码验证失败 - How to fix org.postgresql.util.PSQLException: FATAL: password authentication failed for user org.postgresql.util.PSQLException:协议错误。 会话设置失败 - org.postgresql.util.PSQLException: Protocol error. Session setup failed org.postgresql.util.PSQLException: FATAL: 用户 springboot maven 项目的密码验证失败 - org.postgresql.util.PSQLException: FATAL: password authentication failed for user springboot maven project 引起:org.postgresql.util.PSQLException:致命:用户“admin”的密码验证失败 - Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "admin"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM