title: Managing connection pools using Semaphores (Java)
body: <p>I maintain a connection pool of size N and have semaphore of size N controlling access to each of the connection in the pool. The application acquires a semaphore when trying to send data across and releases it on receiving a response or a socket time out. </p>
  
  <p>There is a background thread running within the connection pool which sends periodic heartbeats for each connection. Now if a heartbeat fails then that connection is closed and an effort is made to create a new one. What I want to make sure is as connection is opened and closed in the pool, I want the permits to be increased and decreased so that it directly reflects the number of connections in the pool. I can reduce the permits using the reducePermits(m) API but to increase, is calling release() (even when none has been acquired) multiple times on the semaphore the only way?</p>