The County - Developer's Site

Home

Architecture

Database

JoCC Paper

Lilac Docs

Manage Test Server

NSF Stuff

Technical
Standards

Tools

Prototype

Old Papers

Old Prototypes

Sample Servlet - Pool Test

This servlet uses the Protomatter database connection pooling classes. Much of the code comes from combining an example from the Protomatter people with code generated by the NetBeans IDE with code from another test application. You can run the application at https://lilac.sba.oakland.edu/county_test/servlet/pool_test

 * pool_test.java
 *
 * Created on March 18, 2002, 5:13 PM
 */
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.sql.*;
      import java.util.*;
      import com.protomatter.jdbc.pool.*;
      /**
 *
 * @author  mathieso
 * @version
 */
      public class pool_test extends HttpServlet {
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    /** Destroys the servlet.
     */
    public void destroy() {
    }
    /** Processes requests for both HTTP GET and POST methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
 	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;

         	try {

        //Initialize the pool
        Class.forName("com.protomatter.jdbc.pool.JdbcConnectionPoolDriver").newInstance();

        // initialization params are kept in a Hashtable
        Hashtable args = new Hashtable();

        // the underlying driver -- in this case, the MySQL driver.
        args.put("jdbc.driver", "org.gjt.mm.mysql.Driver");

        // the URL to connect the underlyng driver with the server
        String connectionURL = "jdbc:mysql://localhost:3306/county_test";
        args.put("jdbc.URL", connectionURL);

        // these are properties that get passed
        // to DriverManager.getConnection(...)

        Properties jdbcProperties = new Properties();
        jdbcProperties.put("user", "<see kieran>");
        jdbcProperties.put("password", "<see kieran>");
        args.put("jdbc.properties", jdbcProperties);

        // a statement that is guaranteed to work
        // if the connection is working.
        args.put("jdbc.validityCheckStatement", "SELECT 1 FROM DUAL");

        // If this is specified, a low-priority thread will
        // sit in the background and refresh this pool every
        // N seconds.  In this case, it's refreshed every two minutes.
        args.put("pool.refreshThreadCheckInterval", new Integer(120));

        // the initial size of the pool.
        args.put("pool.initialSize", new Integer(5));

        // the maximum size the pool can grow to.
        args.put("pool.maxSize", new Integer(10));

        // each time the pool grows, it grows by this many connections
        args.put("pool.growBlock", new Integer(2));

        // between successive connections, wait this many milliseconds.
        args.put("pool.createWaitTime", new Integer(2000));

        // finally create the pool and we're ready to go!
        JdbcConnectionPool countyPool
            = new JdbcConnectionPool("countyPool", args);

        //Get a connection from the pool
        String url = "jdbc:protomatter:pool:countyPool";
        connection = DriverManager.getConnection(url);

         statement = connection.createStatement();
         resultSet = statement.executeQuery(
             "SELECT p_id, handle FROM person;"); 
         while (resultSet.next()) {
              out.println(
                 "<tr><td>"
                 + resultSet.getString("p_id")
                 + "</td><td>"
                 + resultSet.getString("handle")
                 + "</td></tr>"); 
         }
         out.println("</table>");
         if (resultSet != null) {
                 resultSet.close();
            }

	} catch (ClassNotFoundException e) {
		out.println(
		"Couldn't find the database driver: " 
		+ e.getMessage()); 
	} catch (InstantiationException e) {
		out.println(e.getMessage());
	} catch (IllegalAccessException e) {
		out.println(e.getMessage());
	} catch (SQLException e) {
		out.println("SQL Problem : " + e.getMessage());
		out.println("SQL State   : " + e.getSQLState());
		out.println("Vendor Error: " + e.getErrorCode());
	} catch (Exception e) {
		out.println(e.getMessage());
	} finally {
		try {
	  		if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			out.println(e.getMessage());
		}
	}
	out.println("<h2>End</h2>");
	out.println("</body>");
	out.println("</html>");
         out.close();
    }
    

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);

    }

    /** Returns a short description of the servlet.
     */

    public String getServletInfo() {
        return "Short description";

    }
      }


Contact Kieran Mathieson (mathieso@oakland.edu) with questions or comments.