Java.HikaryCP.When i close connection in my code, does connection returns to pool ?

When you close a connection in your code, does the connection return to the pool?

Answer:
Yes — that’s exactly how connection pooling works.

🔹 How it works in HikariCP (and other connection pools):

  • When you call connection.close() on a Connection object you obtained from the pool, the connection isn’t actually closed at the database level.
  • Instead, HikariCP intercepts the close() call through a proxy object, marks the connection as idle, and returns it to the pool so it can be reused for the next request.

🔹 Why it’s important:

  • Because physically closing and reopening database connections is expensive (slow), the pool keeps the connection alive and simply gives it to other threads when needed.
  • This reuse of connections is what makes connection pools performant and scalable.

🔹 What happens if you don’t call close()?

  • The connection stays marked as active in the pool, and never becomes available to other threads → over time, this can exhaust the pool, leading to timeouts or errors.

Key takeaway:
Always close your connections in a finally block (or use try-with-resources) to ensure they return to the pool — you’re not shutting down the connection, you’re giving it back for reuse.

This entry was posted in Без рубрики. Bookmark the permalink.