首页 > 解决方案 > Java & Postgres 多线程失败

问题描述

语境

Spring Batch Application 和 Postgres 托管在同一台服务器上。

Spring Batch Step 以多线程方式运行,执行 SELECT 查询。

Spring Batch Step 正确执行了一段时间,然后在对 Postgres 执行查询时随机遇到以下错误。

错误

2021-04-29 20:57:31.974 [SimpleAsyncTaskExecutor-1] ERROR org.springframework.batch.core.step.AbstractStep - Encountered an error executing step ECIP_TN in job ECIP_tom_JOB
com.lit.tom.core.exception.ApplicationException: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at com.lit.tom.core.driver.retriever.sql.SqlRetriever.getData(SqlRetriever.java:98) ~[tom-engine-1.2.11-SNAPSHOT.jar!/:1.2.11-SNAPSHOT]
        at com.lit.tom.core.driver.retriever.LegacyDataRetriever.retrieveLegacyRecords(LegacyDataRetriever.java:48) ~[tom-engine-1.2.11-SNAPSHOT.jar!/:1.2.11-SNAPSHOT]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82) ~[spring-jdbc-5.2.13.RELEASE.jar!/:5.2.13.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371) ~[spring-jdbc-5.2.13.RELEASE.jar!/:5.2.13.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452) ~[spring-jdbc-5.2.13.RELEASE.jar!/:5.2.13.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462) ~[spring-jdbc-5.2.13.RELEASE.jar!/:5.2.13.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:490) ~[spring-jdbc-5.2.13.RELEASE.jar!/:5.2.13.RELEASE]
        at com.lit.tom.core.driver.retriever.sql.SqlRetriever.getData(SqlRetriever.java:59) ~[tom-engine-1.2.11-SNAPSHOT.jar!/:1.2.11-SNAPSHOT]
        ... 78 more
Caused by: org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.19.jar!/:42.2.19]
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.19.jar!/:42.2.19]
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.19.jar!/:42.2.19]
        at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.19.jar!/:42.2.19]
        at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.19.jar!/:42.2.19]

        ... 78 more
Caused by: java.net.ConnectException: Cannot assign requested address (connect failed)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_292]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_292]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_292]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_292]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_292]
        at java.net.Socket.connect(Socket.java:607) ~[?:1.8.0_292]

调查工作

Postgres

Postgres 正在运行,并接受该端口上的数据。

#Relevant postgres configuration
#listen_addresses = 'localhost'
max_connections = 100
port = 5432

Postgres 网络统计

爪哇

我不知道要看什么,任何帮助将不胜感激。

标签: javaspringpostgresqldatabase-connection

解决方案


我发现了这个问题,并且与这篇文章的评论一致。

已达到最大连接数。

该错误来自子项目,他们已将 DataSource 从 HikariCP 更改为DriverManagerDataSource

相关文档片段:

注意:这个类不是一个实际的连接池;它实际上并不池连接。它只是作为成熟连接池的简单替代品,实现相同的标准接口,但在每次调用时创建新的连接。

解决方案是改回 HikariCP。


推荐阅读