title: Try to make a Connection Pool with Tomcat 6
body: <p>I really am having a nightmare configuring Tomcat to set up a connection pool. I have done a lot of reading of various forums and the documents from Tomcat but am having to ask here as a last resort. This is the first time I have tried to get connections from the container so it's all new to me.</p>
  
  <p>I have been having NameNotFoundException's which only seem to be fixed when I put the context.xml file back from MyApp/META-INF/context.xml to Tomcat 6.0/conf/context.xml, so for some reason it's not seeing the context.xml file in MyApp's META-INF directory. Any ideas?</p>
  
  <p>Now I am getting an SQLNestedException: Cannot create PoolableConnectionFactory (''@'localhost' (using password:YES))</p>
  
  <p>First of all it suprises me that the user is blank because I have specified 'root' in the context.xml. As for not being able to create a PoolableConnectionFactory, I have seen a couple of example context.xml files that had a factory attribute. Do I need this? If so what class should I specify there?</p>
  
  <p>My context.xml is:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;Context&gt;
  
  &lt;!-- Configure a JDBC DataSource for the user database --&gt;
  &lt;Resource name="jdbc/searchdb" 
            type="javax.sql.DataSource" 
            auth="Container" 
            user="root" 
            password="mypassword" 
            driverClassName="com.mysql.jdbc.Driver" 
            url="jdbc:mysql://localhost:3306/search" 
            maxActive="8" 
            maxIdle="4"/&gt;
  
  &lt;!-- Default set of monitored resources --&gt;
  &lt;WatchedResource&gt;WEB-INF/web.xml&lt;/WatchedResource&gt;
  &lt;!--&lt;WatchedResource&gt;META-INF/context.xml&lt;/WatchedResource&gt;--&gt;
  
  &lt;/Context&gt;
  </code></pre>
  
  <p>I have seen a context.xml with a WatchedResource elemnt for the META-INF/context.xml. I tried it but it didn't seem to make a difference and it seems strange to me so I have commented it out. Should I actually be including it?</p>
  
  <p>My test servlet:</p>
  
  <pre><code>package search.web;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.util.*;
  import java.sql.*;
  import javax.sql.*;
  import javax.naming.*;
  import search.model.*;
  public class ConPoolTest extends HttpServlet {
  
      public void doGet(HttpServletRequest request,
                    HttpServletResponse response) 
                    throws IOException, ServletException {
          Context ctx = null;
          DataSource ds = null;
          Connection conn = null;
          try {
              ctx = new InitialContext();
              ds = (DataSource)ctx.lookup("java:comp/env/jdbc/searchdb");
              conn = ds.getConnection();
              if(conn != null) {
                  System.out.println("have a connection from the pool");
              }
          } catch(SQLException e) {
              e.printStackTrace();
          } catch(NamingException e) {
              e.printStackTrace();
          } finally {
              try {
                  if(conn!=null) {
                      conn.close();
                  }
              } catch(SQLException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>I look forward to your suggestions.</p>
  
  <p>Many thanks</p>
  
  <p>Joe</p>
  
  <p>PS I would have also listed the stack trace, but for some reason it is showing up in the console, but not in the logs.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Now that I look at the error message again I'm wondering what it is that is denying me access. I assumed that it was the database, but is itactually the container? Do I need to set up some sort of authentication in the tomcatusers.xml file?</p>