引起:org.postgresql.util.PSQLException:致命:剩余的连接槽是为非复制超级用户连接保留的

时间:2017-12-05 10:54:38

标签: java postgresql c3p0

我使用c3p0-0.9.5.2.jar和mchange-commons-java-0.2.11.jar来管理池连接, 我使用postgreSql 9.3。

我在Prod环境中每天至少收到一次这些消息:

Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:692)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    ... 212 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1469)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:644)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:554)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:758)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:685)
    ... 215 more
Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
    at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:712)

我的配置中有这个配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
         <bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
       <property name="driverClass" value="org.postgresql.Driver"/>

        <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/Test"/> 

        <property name="user" value="postgres"/>
        <property name="password" value="postgres"/>
           <!-- pool sizing -->
     <!-- pool sizing -->
        <property name="initialPoolSize" value="32" />
        <property name="minPoolSize" value="30" />
        <property name="maxPoolSize" value="300" />
        <property name="acquireIncrement" value="10" />
        <property name="maxStatements" value="0" />

        <!-- retries -->
        <property name="acquireRetryAttempts" value="30" />
        <property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
        <property name="breakAfterAcquireFailure" value="false" />

        <!-- refreshing connections -->
        <property name="maxIdleTime" value="180" /> <!-- 3min -->
        <property name="maxConnectionAge" value="10" /> <!-- 1h -->

        <!-- timeouts and testing -->
        <property name="checkoutTimeout" value="0" /> <!-- 60s -->
        <property name="idleConnectionTestPeriod" value="60" /> <!-- 60 -->
        <property name="testConnectionOnCheckout" value="true" />
        <property name="preferredTestQuery" value="SELECT 1" />
        <property name="testConnectionOnCheckin" value="true" /> 

    </bean>
</beans>
postgresql.conf中的

我有这个配置:

max_connections = 300
shared_buffers = 32GB

我的服务器具有以下性能: 24 cpu,256 GB内存

使用该应用程序的用户数量约为1300

是否有人可以帮助我解决这个问题

提前谢谢

2 个答案:

答案 0 :(得分:10)

为超级用户保留了superuser_reserved_connections个连接插槽(默认情况下为 ),以便即使在采用所有连接插槽的情况下也可以连接。

所以你实际上只有297个可用的插槽。

要么减少连接池的最大连接数,要么增加PostgreSQL中的max_connections

顺便说一句,300太高了。您应该在连接池中使用更低的设置(除非数据库计算机中有数百个核心)。

答案 1 :(得分:0)

您看到的错误表明Postgres没有任何可用的连接。

在您的SQL数据库上运行此命令,以查看是否可以更好地了解哪些命令/用户正在使用如此多的连接:

 select * from pg_stat_activity;

如果您只想知道打开的连接数,请使用:

SELECT count(*) FROM pg_stat_activity;

有时打开的连接数达到max_connections限制。

对我来说,重新启动pgAdmin应用解决了该问题。