<%@page import="javax.naming.*" %> simplepool: Servlet DataSource Examples

simplepool
Servlet DataSource Examples

Examples Home

Please read the instructions before trying to run the examples.

Summary A simple database connection pool example using the SimplePool Servlet.
Example 1 Scriptlet Example [source]
Example 2 JSTL Example [source]

Instructions

The SimplePoolServlet is used to create, access and manage the connection pool.

First, make sure that your JDBC driver is available, by placing its components in this webapp's WEB-INF/lib/ directory.

The SimplePoolServlet is configured using a set of required initialization parameters, as show in this webapp's WEB-INF/web.xml under the <servlet/> section:

  • The varName initialization parameter is used to specify the name of the variable which will hold a reference to the DataSource object created by the Servlet.
  • The driver initialization parameter is used to specify the name of the JDBC driver class to be registered. For example: com.mysql.jdbc.Driver.
  • The user initialization parameter is used to specify the database username, if any.
  • The password initialization parameter is used to specify the database password, if any.
  • The jdbcUrl initialization parameter is used to specify the JDBC URL associated with the database. For example: jdbc:mysql://localhost:3306/dbname.
  • The minConns initialization parameter is used to specify the minimum number of connections to start the pool with.
  • The maxConns initialization parameter is used to specify the maximum number of connections to be dynamically created in the pool.
  • The maxConnTime initialization parameter is used to specify the time (in days) between connection resets. The pool manager will perform a basic cleanup at the specified interval.
  • The maxCheckoutSeconds initialization parameter is used to specify the maximum time a connection can be checked out before being recycled. A zero value turns this option off.

Please note that all parameters are required.

Please take a minute to uncomment the <servlet/> declaration and configure the Servlet's initialization parameters in this webapp's WEB-INF/web.xml. Once done, please restart the webapp and/or Tomcat. The examples will be functional upon restart.

Please look at the [source] of the provided examples to understand how to access the connection pool from a scriptlet or using the JSTL SQL tags. The examples demonstrates how to list all of the tables contained in the specified database.