PSQLException:该连接已关闭 - Spring Boot + AWS RDS + Postgres

5

当我尝试访问我的AWS RDS中的Postgres数据库时,会出现PSQLException: This connection has been closed异常。该异常在随机时间发生(并非每次运行代码都会发生),如果执行到下面的代码之前需要一段时间,则更有可能发生异常:

@Transactional
public void revokeChanges(ArrayList<Integer> 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();
                }
            });

连接相关:

spring.datasource.url=jdbc:postgresql://my-url:port/dbName?tcpKeepAlive=true&autoReconnect=true

spring.datasource.driver-class-name=org.postgresql.Driver

相关的POM:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1102-jdbc41</version>
    </dependency>

堆栈跟踪:

org.springframework.jdbc.support.MetaDataAccessException: 从数据库中提取MetaData时出错; 嵌套异常是org.postgresql.util.PSQLException:此连接已关闭。 在org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:296)处 在org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320)处 在org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214)处 在org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134)处 在org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.(SQLErrorCodeSQLExceptionTranslator.java:97)处 在org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)处 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)处 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:673)处 在org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:989)处 在my.package.DataServiceJdbc.revokeChanges(DataServiceJdbc.java:71)处 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)处 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处 在java.lang.reflect.Method.invoke(Method.java:483)处 在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)处 在org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:98)处 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)处 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)处 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)处 在com.sun.proxy.$Proxy24.revokeChanges(Unknown Source)处 在my.package.DataExporterS3ToPostgre.exportData(DataExporterS3ToPostgre.java:59)处 在my.package.Application.main(Application.java:41)处 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)处 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处 在java.lang.reflect.Method.invoke(Method.java:483)处 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)处 由org.postgresql.util.PSQLException引起:此连接已关闭。 在org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:843)处 在org.postgresql.jdbc4.Jdbc4Connection.getMetaData(Jdbc4Connection.java:54)处 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)处 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处 在java.lang.reflect.Method.invoke(Method.java:483)处 在org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)处 在org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)处 在org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:80)处 在com.sun.proxy.$Proxy23.getMetaData(Unknown Source)处 在org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:285)处 ... 29个公共帧

1个回答

4

看起来DB和池之间的连接丢失了。尝试设置removeAbandoned和removeAbandonedTimeout选项。有关这些选项的详细信息,请阅读JDBC连接池文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接