パッケージ org.apache.commons.dbcp.jdbc2pool

このパッケージはデータベースコネクションプールの機能を提供する DataSource を実装した1つの public クラスを持ちます。

参照:
          説明

クラスの概要
Jdbc2PoolDataSource J2EE環境でのデプロイに適切なプーリングを行う DataSource です。
 

パッケージ org.apache.commons.dbcp.jdbc2pool の説明

このパッケージはデータベースコネクションプールの機能を提供する DataSource を実装した1つの public クラスを持ちます。 以下に2通りの使用例を示します。 1つは JNDI システム内にデプロイする方法です。 もう1つは一般的な java のコードでプールを初期化する単純な例です。

This package contains one public class which is a DataSource implementation which provides a database connection pool. Below are a couple of usage examples. One shows deployment into a JNDI system. The other is a simple example initializing the pool using common java code.

JNDI

ほとんどの J2EE コンテナはリソースを JNDI 内にデプロイする何らかの方法を提供しています。 その方法はコンテナごとに違いますが、 JNDI 経由でリソースが利用可能になれば アプリケーションはコンテナから独立した方法でリソースにアクセスすることができます。 以下の例は catalina(Tomcat3.3以降) にデプロイする方法を示します。

Most J2EE containers will provide some way of deploying resources into JNDI. The method will vary among containers, but once the resource is available via JNDI, the application can access the resource in a container independent manner. The following example shows deployment into catalina.

server.xml 内の の配下に以下の記述を追加してください:

In server.xml, the following would be added to the <Context> for your webapp:


 <Resource name="jdbc/bookstore" auth="Container"
            type="org.apache.commons.dbcp.jdbc2pool.Jdbc2PoolDataSource"/>
  <ResourceParams name="jdbc/bookstore">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.jdbc2pool.Jdbc2PoolDataSource</value>
    </parameter>
    <parameter>
      <name>dataSourceName</name><value>java:comp/env/jdbc/bookstoreCPDS</value>
    </parameter>
    <parameter>
      <name>defaultMaxActive</name><value>30</value>
    </parameter>

  </ResourceParams>

web.xml に以下の記述を追加してください。 これらの要素の順番はサーブレット定義のDTD宣言に従わなくてはいけないことに注意してください:

In web.xml. Note that elements must be given in the order of the dtd described in the servlet specification:

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the server.xml file.
  </description>
  <res-ref-name>
    jdbc/bookstore
  </res-ref-name>
  <res-type>
    org.apache.commons.dbcp.jdbc2pool.Jdbc2PoolDataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

Catalina は java:comp/env ネームスペース内にも同様に設定された全てのオブジェクトをデプロイします。 そのため dataSourceName パラメータに渡される JNDI パスは cpdsadapterのサンプルでデプロイされた ConnectionPoolDataSource にも有効です。

Catalina deploys all objects configured similarly to above within the java:comp/env namespace. So the JNDI path given for the dataSourceName parameter is valid for a ConnectionPoolDataSource that is deployed as given in the cpdsadapter example

以下の手順でアプリケーション内で DataSource を利用することができます:

The DataSource is now available to the application as shown below:


    Context ctx = new InitialContext();                
    DataSource ds = (DataSource)
        ctx.lookup("java:comp/env/jdbc/bookstore");
    Connection con = null;
    try
    {
        con = ds.getConnection();
        ... 
        use the connection
        ...
    }
    finally
    {
        if (con != null)
            con.close();
    }

複数の getConnection() リクエストのために DataSource への参照を保持することができます。 また、 DataSource アプリケーションコード内の他のパーツから参照することもできます。 Jdbc2PoolDataSource は異なる参照毎のプールの状態を保持することができます。 この振る舞いは他の実装とは異なるかもしれません。 これは BasicDataSource の振る舞いとは対照的で、 最大100個のコネクションを持つことのできるプールに2つの参照があった場合、 2つのプールは独立してコネクションを管理し、トータルで200個のコネクションを使用することができることになります。 Jdbc2PoolDataSource で独立したコネクションのセットを扱いたい場合には、 異なるインスタンスを異なる名前でデプロイしてください。

The reference to the DataSource could be maintained, for multiple getConnection() requests. Or the DataSource can be looked up in different parts of the application code. Jdbc2PoolDataSource will maintain the state of the pool between different lookups. This behavior may be different among in other implementations. It is in contrast to the behavior of {@link org.apache.commons.dbcp.BasicDataSource}, where two lookups of a pool set to a maximum of 100 connections will lead to two independent sets of connections for a total of 200. With Jdbc2PoolDataSource, you are expected to deploy different instances under different names, if you wish to have independent sets of connections.

Without JNDI

コネクションプーリングはJ2EE環境で動作しないアプリケーションにとっても有用です。 Jdbc2PoolDataSource はそのようなシンプルな環境でも利用することが可能です。 以下に示す例では DriverAdapterCPDS を使用していますが、どんな CPDS でも適用することが可能です。

Connection pooling is useful in applications regardless of whether they run in a J2EE environment and Jdbc2PoolDataSource can be used within a simpler environment. The example below shows DriverAdapterCPDS used as the source, though any CPDS is applicable.


public class Pool
{
    public static DataSource ds;

    static
    {
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver("org.gjt.mm.mysql.Driver");
        cpds.setUrl("jdbc:mysql://localhost:3306/bookstore");
        cpds.setUser("foo");
        cpds.setPassword(null);

        Jdbc2PoolDataSource tds = new Jdbc2PoolDataSource();
        tds.setConnectionPoolDataSource(cpds);
        tds.setDefaultMaxActive(10);
        tds.setDefaultMaxWait(50);

        ds = tds;
    }
}

このクラスはコネクションが必要になったときにいつでも使用することができます:

This class can then be used wherever a connection is needed:

    Connection con = null;
    try
    {
        con = Pool.ds.getConnection();
        ... 
        use the connection
        ...
    }
    finally
    {
        if (con != null)
            con.close();
    }

翻訳者:
日置 聡
翻訳状況:
初稿(校正者募集中)
翻訳更新日:
2003/08/31


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation