brettwooldridge / HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last.
Apache License 2.0
19.63k stars 2.9k forks source link

Why do Session Settings get reset on a connection? #2200

Open ldaIas opened 1 month ago

ldaIas commented 1 month ago

Hello, I had a question concerning the behavior when connections are returned to the pool. For background, we are working with Java 21, HikariCP 5.0.1, and PostgreSQL 15.5

In our application, we make prepared statements such as "SET statement_timeout=xxx" when we first retrieve a connection from the pool. We found we were executing these a lot and wanted to cut down, so we cached the previously executed timeout values on the clientInfo Property on the connection. When getting a new connection from the pool, if we see that the timeout values have already been cached on it's clientInfo, we won't run the query again. We had been assuming that a connection in the pool will not have its session and session settings reset, so that is how we arrived at the solution of not running the query again, since we thought the values would still be on the connection.

What we are finding is that either our assumption is wrong or we misconfigured something. When we get a connection back from the pool that has had its clientInfo modified and immediately query for the timeout values, we see that they are 0 instead of the previously set value. What we want to know is if these values are being reset every time we get the connection from the pool or put it back, or if we misconfigured something somewhere.

I have looked at resetConnectionState() but that only seems to reset the properties of the connection, not settings that would be in the database, but please correct me if wrong.