6.1.7. Database Connection Management Handler

This handler manages the database connection on the thread for use by the subsequent handler and library.

For details of database access, see Database Access (JDBC Wrapper).

Important

Transaction Control Handler has to be configured to use this handle. Since the transaction control is not implemented if the transaction control handler is not configured, all subsequent changes to the database will be discarded.

This handler performs the following process.

  • Get a database connection
  • Release the database connection

The process flow is as follows.

../../../../_images/DbConnectionManagementHandler_flow.png

6.1.7.2. Module list

<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-core-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-common-jdbc</artifactId>
</dependency>

6.1.7.4. Configure the connection destination of the database

This handler gets the database connection using the factory class (ConnectionFactory implementation class) configured in the connectionFactory property.

Refer to the following configuration file example to configure the factory class in the connectionFactory property.

<!-- Database connection management handler -->
<component class="nablarch.common.handler.DbConnectionManagementHandler">
  <property name="connectionFactory" ref="connectionFactory" />
</component>

<!-- Configuration of the factory class that acquires the database connection object -->
<component name="connectionFactory"
    class="nablarch.core.db.connection.BasicDbConnectionFactoryForDataSource">
  <!-- Property configuration is omitted -->
</component>

Important

For details on the factory class to obtain the database connection object, see Connection configuration for the database.

6.1.7.5. Use Multiple Database Connections (Transactions) in an Application

There may be cases where one application requires multiple database connections. In this case, multiple handlers are configured on the handler queue to manage the situation.

This handler manages the database connection object on the thread with a database connection name. Database connection names in a thread must be unique.

The database connection name is configured in the connectionName property of this handler. If the configuration to connectionName is omitted, this connection will become the default database connection and can be used easily. For this reason, configure the most frequently used database connection as the default, and assign arbitrary names to other database connections.

A configuration example for the database connection name is shown below.

<!-- Factory configuration to get the database connection is omitted -->

<!-- Configure the default database connection  -->
<component class="nablarch.common.handler.DbConnectionManagementHandler">
  <property name="connectionFactory" ref="connectionFactory" />
</component>

<!-- Register the database connection with the name userAccessLog -->
<component class="nablarch.common.handler.DbConnectionManagementHandler">
  <property name="connectionFactory" ref="userAccessLogConnectionFactory" />
  <property name="connectionName" value="userAccessLog" />
</component>

The following is an example of database access from an application with the handler settings mentioned above. For details on how to use the database access components, see Database Access (JDBC Wrapper).

Use the default database connection

An argument is not required to be specified when calling DbConnection#getConnection. If an argument is not specified, the default database connection is returned automatically.

AppDbConnection connection = DbConnectionContext.getConnection();
Use the userAccessLog database connection

Use DbConnection#getConnection(String) and specify the database connection name in the argument. The database connection name must match the value configured in the connectionName property.

AppDbConnection connection = DbConnectionContext.getConnection("userAccessLog");