原因:java.sql.SQLException:未建立连接:处理超过 1k 条记录时出现此错误

Caused by: java.sql.SQLException: Connection not established : Getting this error while processing more than 1k records

我正在开发一个 spring 引导应用程序,它并行处理超过 1k json 条消息并同时更新数据库中的各个字段。 对于此进程,最多 20 个线程 运行 并行。 少于 800 json 条消息运行 没问题。

对于这种群发消息场景,只要有数据库更新,我就会遇到以下错误:

ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Connection not established
2022-04-04 16:35:22.647  [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection com.informix.jdbc.IfxSqliConnect@43a6bd0c: (connection is dead)
ERROR o.s.o.jpa.EntityManagerFactoryUtils - Failed to release JPA EntityManager
org.hibernate.exception.GenericJDBCException: Unable to release JDBC Connection
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
        at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection(LogicalConnectionManagedImpl.java:223)
        at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.close(LogicalConnectionManagedImpl.java:261)
        at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:175)
        at org.hibernate.internal.AbstractSharedSessionContract.close(AbstractSharedSessionContract.java:351)
        at org.hibernate.internal.SessionImpl.closeWithoutOpenChecks(SessionImpl.java:376)
        at org.hibernate.internal.SessionImpl.close(SessionImpl.java:361)
        at org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager(EntityManagerFactoryUtils.java:427)
        at org.springframework.orm.jpa.JpaTransactionManager.doCleanupAfterCompletion(JpaTransactionManager.java:650)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:1004)
Caused by: java.sql.SQLException: Connection not established
        at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:408)
        at com.informix.jdbc.IfxSqliConnect.rollback(IfxSqliConnect.java:2324)
        at com.zaxxer.hikari.pool.ProxyConnection.close(ProxyConnection.java:257)
        at java.base/jdk.internal.reflect.GeneratedMethodAccessor230.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at net.bull.javamelody.JdbcWrapper$ConnectionInvocationHandler.invoke(JdbcWrapper.java:202)
        at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:300)
        at com.sun.proxy.$Proxy138.close(Unknown Source)
        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.closeConnection(DatasourceConnectionProviderImpl.java:127)

配置的数据源属性:

spring:
  datasource:
    type: org.apache.tomcat.jdbc.pool.DataSource
    tomcat:
       init-SQL: select col1  from table ( set {1} ) as x( col1 )
       default-transaction-isolation: 1
       initial-size: 10
       max-wait: 60000
       max-active: 200
       max-idle: 200
       min-idle: 100
       test-while-idle: true
       test-on-connect: true
       test-on-borrow: true
       test-on-return: true
       remove-abandoned : true
       remove-abandoned-timeout: 300
       time-between-eviction-runs-millis: 5000
       min-evictable-idle-time-millis: 60000
       validation-interval: 30000
       validation-query: select col1  from table ( set {1} ) as x( col1 )
       validation-query-timeout: 10
  jpa:
      open-in-view: false
      database-platform: org.hibernate.dialect.InformixDialect
      show-sql: false
      properties.hibernate.format_sql: true

有人可以suggest/advice吗?

因为您正在使用 Hikari 进行连接池 我认为您还需要管理 hikari

的属性

请尝试使用这些道具进行配置: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.data.spring.datasource.hikari