Search code examples
postgresqlspring-bootconnection-pooling

Postgres 11, receiving broken pipe, if stays inactive


Spring application is logging broken pipe occasionally. To deal with stale connections issue, application is already using -

testWhileIdle=true and validationQuery=SELECT 1

Stack trace -

    Caused by: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
            at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:333)
            at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
            at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
            at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
            at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118)
            at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
            at com.sun.proxy.$Proxy137.executeQuery(Unknown Source)
            at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
            ... 133 more
    Caused by: java.net.SocketException: Connection reset
            at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:115)
            at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
            at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
            at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
            at org.postgresql.core.PGStream.flush(PGStream.java:514)
            at org.postgresql.core.v3.QueryExecutorImpl.sendSync(QueryExecutorImpl.java:1363)
            at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:304)
            ... 143 more
     Caused by java.net.SocketException: Broken pipe (Write failed)

A vague feeling, if application stays inactive for while, then it might be giving problem connecting to backend or vice-versa. But since can not reproduce this issue locally, it is hard to point.


Solution

  • You can try setting the timeout of idle connection higher.

    idle_in_transaction_session_timeout = 30000
    

    in postgresql.conf

    But your error seems strange to me. I would probably check if you dont have way to many connection as well.

    On the spring-boot side you could try adding the following properties:

    spring.datasource.test-on-borrow=true
       spring.datasource.validation-query=SELECT 1
       spring.datasource.validation-interval=30000
    

    you could try reducing the validation interval. I set it to the default one there

    Hope it helps