title: PSQLException: This connection has been closed - Spring Boot + AWS RDS + Postgres
body: <p>I am getting <code>PSQLException: This connection has been closed</code> when trying to access my Postgres database in AWS RDS. The exception happens randomly (not all the times I run the code) and it's more probable to happen if it takes a while before the execution reaches the code below:</p>
  
  <pre><code>@Transactional
  public void revokeChanges(ArrayList&lt;Integer&gt; changeIds) {
      jdbcTemplate.batchUpdate(sqlUpdate,
              new BatchPreparedStatementSetter() {
                  @Override
                  public void setValues(PreparedStatement ps, int i) throws SQLException {
                      ps.setStuff..
                      ...
                  }
                  @Override
                  public int getBatchSize() {
                      return changeIds.size();
                  }
              });
      jdbcTemplate.batchUpdate(sqlInsert,
              new BatchPreparedStatementSetter() {
                  @Override
                  public void setValues(PreparedStatement ps, int i) throws SQLException {
                      ps.setStuff...
                  }
                  @Override
                  public int getBatchSize() {
                      return changeIds.size();
                  }
              });
  </code></pre>
  
  <p>Connection Stuff:</p>
  
  <p><code>spring.datasource.url=jdbc:postgresql://my-url:port/dbName?tcpKeepAlive=true&amp;autoReconnect=true</code></p>
  
  <p><code>spring.datasource.driver-class-name=org.postgresql.Driver</code></p>
  
  <p>Relevant POM:</p>
  
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.1.9.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;version&gt;9.3-1102-jdbc41&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre>
  org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:296)
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320)
      at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214)
      at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134)
      at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.(SQLErrorCodeSQLExceptionTranslator.java:97)
      at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:673)
      at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:989)
      at my.package.DataServiceJdbc.revokeChanges(DataServiceJdbc.java:71)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy24.revokeChanges(Unknown Source)
      at my.package.DataExporterS3ToPostgre.exportData(DataExporterS3ToPostgre.java:59)
      at my.package.Application.main(Application.java:41)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  Caused by: org.postgresql.util.PSQLException: This connection has been closed.
      at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:843)
      at org.postgresql.jdbc4.Jdbc4Connection.getMetaData(Jdbc4Connection.java:54)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
      at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)
      at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:80)
      at com.sun.proxy.$Proxy23.getMetaData(Unknown Source)
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:285)
      ... 29 common frames omitted
  </pre>