有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何找到哪个应用程序保留Postgres连接?

有几个应用程序在Tomcat上运行。他们使用Postgres数据库,有时超过最大连接限制:

org.hibernate.exception.GenericJDBCException: Error calling Driver#connect         at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

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:684)

我曾试图提高限额,但问题仍然存在。如何找出哪个应用程序接管连接插槽


共 (1) 个答案

  1. # 1 楼答案

    将ApplicationName添加到连接字符串,如下所示:

    jdbc:postgresql://localhost:5435/MyDB?ApplicationName=MyApp
    

    然后,您将在中看到它:

    select application_name, count(1), state 
    from pg_stat_activity 
    group by application_name, state;
    

    当然,这只有在不同应用程序有单独的连接字符串时才有效

    还要检查与JDBC的连接池上的一些info