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

simplepool
JNDI DataSource Examples

Examples Home

Please read the instructions before trying to run the examples.

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

Instructions

JNDI 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 JNDI DataSource is defined as naming resource in the current webapp context, as shown in this minimal server configuration file.

The JNDI DataSource is configured using the following parameters:

  • The varName 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 parameter is used to specify the name of the JDBC driver class to be registered. For example: com.mysql.jdbc.Driver.
  • The user parameter is used to specify the database username, if any.
  • The password parameter is used to specify the database password, if any.
  • The jdbcUrl parameter is used to specify the JDBC URL associated with the database. For example: jdbc:mysql://localhost:3306/dbname.
  • The minConns parameter is used to specify the minimum number of connections to start the pool with.
  • The maxConns parameter is used to specify the maximum number of connections to be dynamically created in the pool.
  • The maxConnTime 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 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 incorporate the JDNI DataSource example in your $CATALINE_HOME/conf/server.xml. Once done, please restart 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.