title: Implementing Connection Pooling : Java
body: <p>In one of the interviews that I faced,I was asked to implement connection pooling.
  So approach was this:</p>
  
  <ol>
  <li>Create a <code>List</code> or <code>HashMap</code></li>
  <li>Create predefined number of connections</li>
  <li>Add them to the collection.</li>
  <li>Now when the <code>ConnectionImpl</code> <code>getConnection()</code> method of <code>ConnectionPoolingImpl</code> class is invoked return a connection reference.</li>
  </ol>
  
  <p>Now when someone returns the connection (<code>releaseConnection(ConnectionImpl O)</code>) how can I ensure that when the same application again tries to reuse the connection object, my implementation throws an exception?</p>
  
  <p>The same connection object might have been returned to a new application and that should be able to use it.</p>
  
  <p>My point of view would be to maintain a flag variable in another array kind of structure for each <code>Connectionimpl</code> object and set that variable to a valid value. When user returns the connection object I would make that some invalid value. For every operation in my <code>ConnectionImpl</code>, I will have to verify if the user had a valid flag.</p>
  
  <p>What would you say to that approach?</p>