title: Get database connection from a connection pool
body: <p>I am refactoring others code.  The one thing I notice is that of the manner on how the system is getting a connection from the connection pool.</p>
  
  <p>Sample is like this. On every call of the service method, the system is making a context lookup on the JNDI for the datasource.</p>
  
  <pre><code>public class CheckinServlet extends HttpServlet {
  
      public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          try {
              //Obtain Connection
              InitialContext initialContext = new InitialContext();
              javax.sql.DataSource ds = (javax.sql.DataSource) initialContext
                      .lookup("jdbc/mysqldb");
              java.sql.Connection conn = ds.getConnection();
              //business logic
              //redirect
          } finally {
              conn.close();
          }
      }
  }
  </code></pre>
  
  <p>I do think that there is a performance hit on doing this every time.  I am thinking of another way around these on how to retrieve a connection from a connection pool.</p>
  
  <p>I am thinking about using the servlet's <code>init()</code> method but I think that is not optimal.</p>