Thank you for the hint
.
I have tried this setup. The time improved, indeed
. For my test data, it dropped from 450ms to 50ms.
I’d say that my DB is pretty small:
SELECT pg_size_pretty(pg_database_size('leave-management-test'));
--- 7475 kB
There is another issue which existed since the beginning; but now it became important.
For the moment, in order to create a new temp DB (scoped for the duration of a single test), I need to delete 1/ disconnect from it and 2/ delete the old temp DB. “old temp DB” = the DB used for the previous test.
For the moment, to disconnect I do the “disconnect forcibly” approach:
String sql = "SELECT pg_terminate_backend(pid) " +
"FROM pg_stat_activity " +
"WHERE datname = '" + dbName + "' " +
"AND pid <> pg_backend_pid()";
It works. It’s reasonably fast. However, the connection pool doesn’t like this. It yells at me with multiple lines such as:
2025-07-22 20:31:06.276 WARN --- [ Thread-27] com.zaxxer.hikari.pool.PoolBase : HikariPool-3 - Failed to validate connection org.postgresql.jdbc.PgConnection@5be0b67c (This connection has been closed.). Possibly consider using a shorter maxLifetime value.
...
It seems to recover is I give it a little “time to breathe”. E.g. sleep 1 second. But w/o this sleep, the connection breaks and it doesn’t reconnect:
2025-07-26 19:50:02.195 WARN --- [ Thread-12] com.zaxxer.hikari.pool.ProxyConnection : HikariPool-3 - Connection org.postgresql.jdbc.PgConnection@30de8451 marked as broken because of SQLSTATE(57P01), ErrorCode(0)
org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
2025-07-26 19:50:02.195 WARN --- [ Thread-12] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 57P01
2025-07-26 19:50:02.196 ERROR --- [ Thread-12] o.h.engine.jdbc.spi.SqlExceptionHelper : FATAL: terminating connection due to administrator command
2025-07-26 19:50:02.196 WARN --- [ Thread-12] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08006
2025-07-26 19:50:02.196 ERROR --- [ Thread-12] o.h.engine.jdbc.spi.SqlExceptionHelper : An I/O error occurred while sending to the backend.
2025-07-26 19:50:02.197 ERROR --- [ Thread-12] o.s.t.i.TransactionInterceptor : Application exception overridden by rollback exception
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
Is there a way to do this in a nice way? Preferably, to initiate the disconnect from Java. However, afterwards I need to: 1/ reconnect and 2/ do this fast. Can I somehow configure the connection pool, or interact w/ it? Or maybe I can get rid of it, if things are simpler so.