Search code examples
javamultithreadingconcurrencyconnection-poolingjava.util.concurrent

Concurrency: implement connection timeout in custom connection pool


I have to implement my own connection pool, and I want the connection will automatically return to the pool after some CONNECTION_TIMEOUT. How can I achive that?

Everything that comes to mind is to use ScheduledExecutorService in a separate thread and replan it each time the connection is used.

Any other ideas?


Solution

  • You can use the client petition for a pooled connection, for triggering some actions, for example, check the timeout for currently used connections, and realease the currently used connections which timeout has expired. You will avoid using a new thread.