I have a Spring Boot application that provides a REST API to front-ends. I am using jOOQ and Postgresql. I am currently getting this error when executing all integration tests locally (around 1000 tests, this starts happening after executing 700-800 tests):
org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
I tried to limit the max idle and active connections via application.properties
, but it seems that these values are somewhat ignored. I simply monitor the open connections when executing the tests with this statement:
SELECT datname, state, query FROM pg_stat_activity;
That's how my application.properties looks like:
spring.datasource.driverClassName = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://localhost:5432/xxx
spring.datasource.username = xxx
spring.datasource.password = xxx
spring.datasource.initialize = true
spring.datasource.continue-on-error = false
spring.jooq.sql-dialect = POSTGRES
spring.datasource.max-active = 50
spring.datasource.max-idle = 5
That's how I create my data source:
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource() {
return DataSourceBuilder
.create()
.build();
}
I see that jOOQ uses the correct data source and the connection is handled by jOOQ properly (acquiring and releasing connection from data source). So the problem shouldn't be on the jOOQ side.
I have max_connections = 200
in my postgresql.conf
, so my Spring configuration should be fine. While running tests I see a lot more idle connections in pg_stat_activity
than what I specify in my config. Eventually when the tests start failing due to that PSQLException
I see around 90-100 idle connections in pg_stat_activity
. So this yields two problems:
- Why do my tests fail although my local database should allow more connections than what I see in
pg_stat_activity
? - It seems that the data source configuration in my
application.properties
is ignored. Any idea why?
spring.datasource.max-active = 20
: it is a typo I assume. – Wellfed20
, the error is expected. No ? In fact, you don't tell how many connections are currently opened as you have the error. This information would be useful. – Wellfedmax_connections
config in mypostgresql.conf
. In myapplication.properties
I tried various combinations of max-active and max-idle (I also tried having max-active = max_connections and not specifying max-idle), but I always encounter that PSQLException. I get between 90 -100 results frompg_stat_activity
when I receive the exception. – Protrusive