Database ConfigurationResin 3.0
Resin 3.0

Web Applications
Servlets and Filters
Admin (JMX)
EJB 3.0


Resin provides a robust and tested connection pool that is used to obtain connections to databases.

  1. Basic Configuration
  2. Core Concepts
    1. Connection
    2. Connection Pool
    3. DataSource
    4. Driver
    5. Transaction
    6. Distributed Transaction
  3. Core Configuration
    1. database
  4. Driver Configuration
    1. driver
    2. Choosing a driver class for <type>
      1. JCA drivers
      2. JDBC 2.0 - ConnectionPoolDataSource
      3. JDBC 2.0 - XADataSource
      4. JDBC 1.0 - Driver
    3. Set driver properties with init-param
  5. Pooling Configuration
  6. Reliability Configuration
  7. Obtaining and using a database connection
    1. Getting the DataSource
    2. Getting a Connection
  8. See also

Basic Configuration

A basic <database> configuration specifies the following:

  • The JNDI name where the configured DataSource will be stored
  • The database driver's main class
  • The driver-specific url for the database.
  • Any user and password information.

<database jndi-name='jdbc/test_mysql'>
  <driver type="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">

This <database> will configure a javax.sql.DataSource and store it in JNDI at java:comp/env/jdbc/test_mysql. To use the data source, follow the database use pattern in the DataSource tutorial.

Sample <database> configurations are available in the thirdparty driver page.

Although some deployments will specify driver and connection pool parameters, the default values will be fine for most applications.

Core Concepts



An established channel of communication between a client and a server. The client and the server may be on separate machines, on the same machine, or even running in the same JVM. Often the connection is established using TCP/IP as the transport of communication.

A database connection is used to allow the Java program, running in a JVM, to communicate with a database server.

Connection Pool

connection pool

A set of connections maintained so that the connections can be reused when there is a future need for the conneciton.

Connection pools are used to reduce the overhead of using a database. Establishing a connection to the database is a costly operation. A connection pool keeps a pool of open connections, each connection can be used for a time as needed, and then released back to the pool. A connection that has been released back to the pool can then be reused.

Connection pooling is especially important in server applications. The overhead of opening a new connection for each new client request is too costly. Instead, the database pool allows for a connection to be opened once and then reused for many requests.



A JDBC term (and interface name) used for a factory that is used to obtain connections.

Resin provides an implementation of DataSource. Resin's implementation of DataSource is a connection pool.



An implemetation of a defined interface that hides the details of communication with a device or other resource, such as a database.

A Driver provides an interface and is responsible for the communication with the database. Every different database (i.e Oracle, MySQL) has their own means of enabling communication from the client (in this case Resin and you applications) and the database. The Driver provides a common interface that hides the details of that communication.



A transaction is used to mark a group of operations and provide a guarantee that all of the operations happen, or none of them happen. Transactions protect the integrity of the database.

Transactions are especially important in server applications where many threads of processing may be interacting with the database at the same time.

For a simple example, imagine a set of operations that reads a value, calculates a new value, and then updates the database.

Simple set of database operations
read value A=1 
calculate  A=A+1 
update     A=2

read value A=2 
calculate  A=A+1 
update     A=3

Imagine if one thread is performing this operation, and in the middle of this read/calculate/update, another thread performs an update. The data that the first thread obtained from the read and is using for the calculation and update is no longer valid.

2 Threads with a conflicting set of database operations
Thread 1                 Thread 2
--------                 --------
read value A=1           read value A=1
calculate  A=A+1         calculate A=A+1
                         update A=2
update     A=2

Placing the read/calculate/update operations in a transactions guarantees that only one thread can perform those operations at a time, if a second thread comes along and tries to perform the operation, it will have to wait for the first thread to finish before it can begin.

2 Threads and a database protected with transactions

Thread1                Thread 2
-------                --------
read value A=1         
calculate  A=A+1       (tries to read A, but has to wait for thread 1)
update     A=2
                       read value A=2
                       calculate A=A+1
                       update A=3

Distributed Transaction

distributed transaction

A distributed transaction is a transaction that involves more than one connection.

If the guarantees that transactions apply need to apply to operations that occur on two databases within the same transaction, distributed transactions are needed.

If A and B in the following example are in two different databases, then a distributed transaction is needed:

Simple set of database operations
read value db1.A=1 
read value db2.B=99
calculate  A=A+1 
calculate  B=B-A 
update     db1.A=2
update     db2.B=97

Distributed transactions are rarely needed, and few databases really support them.

Core Configuration


Resin 3.0

child of: server, host-default, host, web-app-default, web-app

Configure a database resource, which is a database pool that manages and provides connections to a database.

jndi-nameJNDI name to store the pool under. Servlets, jsp, and other java code use this name. The path is relative to java:comp/env
driverConfigure the database driver.
backup-driverConfigure a backup driver.
max-connectionsPooling parameter - maximum number of allowed connections20
max-idle-timePooling parameter - maximum time an idle connection is kept in the pool30 sec
max-active-timePooling parameter - maximum time a connection allowed to be active 6 hours
max-pool-timePooling parameter - maximum time a connection is kept in the pool24 hours
connection-wait-timePooling parameter - how long to wait for an idle connection (Resin 1.2.3)10 minutes
max-overflow-connectionsPooling parameter - how many "overflow" connection are allowed if the connection wait times out.0
ping-tableReliability parameter - The database table used to "ping", checking that the connection is still live.n/a
pingReliability parameter - test for live connections before allocating them from the pool.false
ping-intervalReliability parameter - how often to ping for ping-on-idle60s
prepared-statement-cache-sizeA cache that holds prepared statements, a reused prepared statement avoids the overhead of the driver making the prepared statement0
spyA debugging aid, if true, generate info level log events that reveal the SQL that is used with the connections.false

All times default to seconds, but can use longer time periods:

Time suffixes

The class that corresponds to <database> is class com.caucho.sql.DBPool

Driver Configuration


Resin 3.0

child of: database

Configure a database driver. The driver is a class provided by the database vendor, it is responsible for the communication with the database.

The jar file with the driver in it can be placed in WEB-INF/lib, although it is often best to place your datbase driver's jar file in $RESIN_HOME/lib/local/, which makes the driver available to all of your web applications.

Examples of common driver configurations are in Third-party Database Configuration .

The class that corresponds to <driver> is class com.caucho.sql.DriverConfig

typeThe Java class name of the database driver.
urlThe driver specific database url.
userThe username to give the database driver.
passwordThe password to give the database driver.
init-paramSet driver specific properties not known to Resin.

Choosing a driver class for <type>

Database vendors usually provide many different classes that are potential candidates for type. The JDBC api has developed over time, and is now being replaced by the more general JCA architecture. The driver you choose depends on the options the vendor offers, and whether or not you need distributed transactions.

JCA drivers

JCA is replacing JDBC as the API for database drivers. JCA is a much more flexible approach that defines an API that can be used for any kind of connection, not just a connection to a database. If a database vendor provides a JCA interface, it is the best one to use.

A JCA driver implements ManagedConnectionFactory. When you specify such a class for type, Resin will notice that it is a JCA driver and take advantage of the added functionality that the JCA interface provides.

The same JCA driver is used for both non-distributed and distributed transactions

JDBC 2.0 - ConnectionPoolDataSource

JDBC 2.0 defined the interface ConnectionPoolDataSource. A ConnectionPoolDataSource is not a connection pool, but it does provide some extra information that helps Resin to pool the connection more effectively.

A driver that implements ConnectionPoolDataSource is better than a JDBC 1.0 driver that implements Driver.

JDBC 2.0 - XADataSource

JDBC 2.0 defined the interface XADataSource for connections that can participate in distributed transactions. A distributed transaction is needed when transactions involve multiple connections. For example, with two different database backends, if the guarantees that transactions apply need to apply to operations that occur on both databases within the same transaction, distributed transactions are needed.

Distributed transactions are rarely needed, and few databases really support them. Some vendors will provide XADataSource drivers even though the database does not really support distributed transactions. Often, XADataSource drivers are slower than their ConnectionPoolDataSource counterparts.

XADataSource should only be used if distributed transactions are really needed, and can probably be safely ignored for most applications.

JDBC 1.0 - Driver

Driver is the original JDBC interface, and is the least desirable kind of driver to use. Resin can still pool database connections using these drivers, but it will not be as efficient as the newer drivers.

Set driver properties with init-param

init-param is used to set properties of the database driver that are specific to the driver and are not generic enough for resin to provide a named configuration tag.

For example, MySQL drivers accept the useUnicode parameter, if true the driver will use Unicode character encodings when handling strings.


    <init-param useUnicode="true"/>

Pooling Configuration

Pooling configuration controls the behaviour of Resin's pooling of database connections. For most applications and databases the only needed change is to increase the max-connections value to meet high demand. Other pooling parameters have defaults that are based on our years of experience with many different databases in many different applications. Changes from the defaults should only be done in response to specific problems, and with a good understanding of how pooling works.

Reliability Configuration

Resin's database pool can test if the pooled database connection is still alive by configuring a ping query. This is typically only necessary if the pooling parameters are changed from their default values.

If the pool is configured with a long max-idle-time the database connection may become stale if the database is restarted, or if the database is configured with a shorter connection timeout value than the configuration of the Resin pool. Normally when a database connection is returned to the pool it will wait there until the next request or the idle-time expires. If the database goes down in the meantime or closes the connection, the connection will become stale. The ping configuration can test the database connection.

When pinging, Resin's DBPool will test a table specified with the ping-table parameter before returning the connection to the application. If the ping fails, the connection is assumed to be no good and a different connection from the pool is returned. For a ping-table of my_table, Resin will use a query like the following:

SELECT 1 FROM my_table

You can test the database reliability using the following steps:

  1. Configure the database with ping-table and ping.
  2. Execute some servlet that queries the database.
  3. Restart the database server.
  4. Execute another servlet that queries the database.

Obtaining and using a database connection

Getting the DataSource

The DataSource is a factory that is used to obtain a connection. The DataSource is obtained using the <jndi-name> specified when configuring the database resource.

Ideally, the JNDI lookup of DataSource is done only once, the DataSource obtained from the lookup can be stored in a member variable or other appropriate place. The stored DataSource can then be used each time a connection is needed. If it is not stored, there will be an impact on performance from having to do the lookup each time you want to get a connection.

Obtaining a DataSource
public class .... {
  private final static String DATASOURCE_NAME = "jdbc/test";
  DataSource _pool;


  void init()
    try {
      Context env = (Context) new InitialContext().lookup("java:comp/env");

      _pool = (DataSource) env.lookup(DATASOURCE_NAME);

      if (_pool == null)
        throw new ServletException("`" + DATASOURCE_NAME + "' is an unknown DataSource");
    } catch (NamingException e) {
      throw new ServletException(e);


Getting a Connection

A connection is obtained from the DataSource. The connection is used as needed, and then released with a call to close() so that Resin knows it is available for a subsequent request.

It is very important that the close() is always called, even if there as an exception. Without the close(), Resin's database pool can loose connections. If you fail to close() a connection, Resin does not know that it is available for reuse, and cannot allocate it for another request. Eventually, Resin may run out of connections.

Always put a close() in a finally block, to guarantee that it is called.

The following example shows the use of a finally block that contains the close(). Because the close() is in a finally block, it will happen even if the code using the connection throws an exception.

Getting a connection from the DataSource

    Connection conn = null;
    try {
      conn = pool.getConnection();

      Statement stmt = conn.createStatement();

      ResultSet rs = stmt.executeQuery(" ... ");


    } catch (SQLException e) {
      throw new ServletException(e);
    } finally {
      try {
        if (conn != null)
      } catch (SQLException e) {

See also

Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.