Nice programing

org.postgresql.util.PSQLException : FATAL : 죄송합니다. 이미 클라이언트가 너무 많습니다.

nicepro 2020. 10. 31. 10:05
반응형

org.postgresql.util.PSQLException : FATAL : 죄송합니다. 이미 클라이언트가 너무 많습니다.


Postgresql 데이터베이스에 연결하려고하는데 다음과 같은 오류가 발생합니다.

Error : org.postgresql.util.PSQLException : FATAL : 죄송합니다. 이미 클라이언트가 너무 많습니다.

오류의 의미는 무엇이며 어떻게 수정합니까?

server.properties파일은 다음과 같습니다.

serverPortData=9042
serverPortCommand=9078
trackConnectionURL=jdbc:postgresql://127.0.0.1:5432/vTrack?user=postgres password=postgres
dst=1
DatabaseName=vTrack
ServerName=127.0.0.1
User=postgres
Password=admin
MaxConnections=90
InitialConnections=80
PoolSize=100
MaxPoolSize=100
KeepAliveTime=100
TrackPoolSize=120
TrackMaxPoolSize=120
TrackKeepAliveTime=100
PortNumber=5432
Logging=1

우리는 server.properties 파일이 무엇인지 모르고 SimocoPoolSize 가 무엇을 의미 하는지 모릅니다.

데이터베이스 연결의 사용자 지정 풀을 사용하고 있다고 가정 해 봅시다. 그런 다음 문제는 풀이 100 개 또는 120 개의 연결을 열도록 구성되어 있지만 Postgresql 서버는 MaxConnections=90. 이것은 충돌하는 설정으로 보입니다. 을 늘리십시오 MaxConnections=120.

그러나 풀에 너무 많은 열린 연결이 필요한 경우 먼저 db 계층 인프라를 이해하고 사용중인 풀을 알아야합니다. 특히, 열린 연결을 풀에 우아하게 반환하는 경우


다음 오류에 대한 설명 :

org.postgresql.util.PSQLException: FATAL: sorry, too many clients already.

요약:

허용 된 데이터베이스 연결 제한을 초과하여 열었습니다. 다음과 같이 Connection conn = myconn.Open();실행했습니다 conn.close();. 루프 내부에서 실행하는 것을 잊었습니다 . 클래스가 파괴되고 가비지 수집이 데이터베이스에 대한 연결을 해제하지 않습니다. 이에 대한 가장 빠른 수정은 연결을 생성하는 클래스에 대해 다음 코드가 있는지 확인하는 것입니다.

protected void finalize() throws Throwable  
{  
    try { your_connection.close(); } 
    catch (SQLException e) { 
        e.printStackTrace();
    }
    super.finalize();  
}  

연결을 만드는 모든 클래스에 해당 코드를 배치합니다. 그런 다음 클래스가 가비지 수집되면 연결이 해제됩니다.

이 SQL을 실행하여 허용 된 postgresql 최대 연결을 확인합니다.

show max_connections;

기본값은 100입니다. 양호한 하드웨어의 PostgreSQL은 한 번에 수백 개의 연결을 지원할 수 있습니다. 수천 개를 원할 경우 연결 풀링 소프트웨어를 사용하여 연결 오버 헤드를 줄이는 것을 고려해야합니다.

누가 / 무엇을 / 언제 / 어디서 연결을 유지하고 있는지 정확히 살펴보십시오.

SELECT * FROM pg_stat_activity;

현재 사용되는 연결 수는 다음과 같습니다.

SELECT COUNT(*) from pg_stat_activity;

디버깅 전략

  1. 연결을 해제하지 않을 수있는 프로그램에 다른 사용자 이름 / 비밀번호를 제공하여 연결을 해제 한 다음 pg_stat_activity에서 어떤 것이 자체적으로 정리되지 않는지 확인할 수 있습니다.

  2. 연결을 생성 할 수 없을 때 전체 예외 스택 추적을 수행하고 코드를 새로 생성 한 위치까지 다시 따르십시오 Connection. 연결을 생성하는 모든 코드 라인이connection.close();

max_connections를 더 높게 설정하는 방법 :

postgresql.conf의 max_connections는 데이터베이스 서버에 대한 최대 동시 연결 수를 설정합니다.

  1. 먼저 postgresql.conf 파일을 찾으십시오.
  2. 위치를 모르는 경우 sql을 사용하여 데이터베이스를 쿼리합니다. SHOW config_file;
  3. 내 위치 : /var/lib/pgsql/data/postgresql.conf
  4. 루트로 로그인하고 해당 파일을 편집하십시오.
  5. 문자열 "max_connections"를 검색합니다.
  6. 라는 줄이 표시 max_connections=100됩니다.
  7. 그 숫자를 더 크게 설정하고 postgresql 버전의 제한을 확인하십시오.
  8. 변경 사항을 적용하려면 postgresql 데이터베이스를 다시 시작하십시오.

최대 최대 연결 수는 얼마입니까?

이 쿼리를 사용하십시오.

select min_val, max_val from pg_settings where name='max_connections';

I get the value 8388607, in theory that's the most you are allowed to have, but then a runaway process can eat up thousands of connections, and surprise, your database is unresponsive until reboot. If you had a sensible max_connections like 100. The offending program would be denied a new connection.


No need to increase the MaxConnections & InitialConnections. Just close your connections after after doing your work. For example if you are creating connection:

try {
     connection = DriverManager.getConnection(
                    "jdbc:postgresql://127.0.0.1/"+dbname,user,pass);

   } catch (SQLException e) {
    e.printStackTrace();
    return;
}

After doing your work close connection:

try {
    connection.commit();
    connection.close();
} catch (SQLException e) {
    e.printStackTrace();
}

The offending lines are the following:

MaxConnections=90
InitialConnections=80

You can increase the values to allow more connections.


You need to close all your connexions for example: If you make an INSERT INTO statement you need to close the statement and your connexion in this way:

statement.close();
Connexion.close():

And if you make a SELECT statement you need to close the statement, the connexion and the resultset in this way:

resultset.close();
statement.close();
Connexion.close();

I did this and it worked

참고URL : https://stackoverflow.com/questions/2757549/org-postgresql-util-psqlexception-fatal-sorry-too-many-clients-already

반응형