Home   Single Page

コネクションプーリングの設定

コネクションプーリングの設定はJ2EE/Web/Databaseサーバーによって変わります。そのうちのいくつかをここで話します。使用しているサーバーのドキュメントを参考にしてください。

Tomcat 5.5 + MySQL

Tomcat 5.5 でコネクションプーリングを設定するには $TOMCAT_DIR/conf/context.xml [66]を編集し、<context> 要素の下に以下のコンテンツを追加しなければなりません。
インストール・設定によって、パス・ユーザー名など(青い色のところ)変更する必要があります。

<!-- The name you used above, must match _exactly_ here!
    The connection pool will be bound into JNDI with the name    
    "java:/comp/env/jdbc/MyDB"    
-->
<Resource name="jdbc/MyDB" username="someuser" password="somepass" 
    url="jdbc:mysql://localhost:3306/test"     
    auth="Container" defaultAutoCommit="false"     
    driverClassName="com.mysql.jdbc.Driver" maxActive="20"     
    timeBetweenEvictionRunsMillis="60000"     
    type="javax.sql.DataSource" />    
</ResourceParams>

そして、web.xml の中で、以下のように <web-app> の下に以下のコンテンツを追加しなければなりません。

<resource-ref>
<res-ref-name>jdbc/MyDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

JBoss + MySQL

以下の説明は MySQL 5.0 のリファレンスマニュアルの 23.3.4.3 セクションを基にしています。

JBoss でコネクションプーリングを設定するには、deploy( $JBOSS_DIR/server/default/deploy ) と呼ばれるディレクトリーに新しいファイルを追加します。そのファイルの名前は "-ds.xml" で終わらなければなりません。そのファイルは、JBoss に、ファイルを JDBC データソースとしてデプロイすることを伝えます。ファイルは以下のコンテンツを含まなければなりません。その情報はインストール・設定しだいで、変更が必要なところ(青い色)があります。

<datasources>
<local-tx-datasource>
<!-- This connection pool will be bound into JNDI with the name
"java:/MyDB" -->
<jndi-name>MyDB</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>someser</user-name>
<password>somepass</password>

<min-pool-size>5</min-pool-size>

<!-- Don't set this any higher than max_connections on your
MySQL server, usually this should be a 10 or a few 10's
of connections, not hundreds or thousands -->

<max-pool-size>20</max-pool-size>

<!-- Don't allow connections to hang out idle too long,
never longer than what wait_timeout is set to on the
server...A few minutes is usually okay here,
it depends on your application
and how much spikey load it will see -->

<idle-timeout-minutes>5</idle-timeout-minutes>

<!-- If you're using Connector/J 3.1.8 or newer, you can use
our implementation of these to increase the robustness
of the connection pool. -->

<exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>

</local-tx-datasource>
</datasources>

JBoss + PostgreSQL

<datasources>
    <local-tx-datasource>    
    <!-- This connection pool will be bound into JNDI with the name    
     "java:/MyDB" -->    
    <jndi-name>MyDB</jndi-name>    
        
    <!-- jdbc:postgresql://[servername]:[port]/[database name] -->    
    <connection-url>jdbc:postgresql://localhost/test</connection-url>    

    <driver-class>org.postgresql.Driver</driver-class>    
    <user-name>someuser</user-name>    
    <password>somepass</password>    
    <min-pool-size>5</min-pool-size>    
    <max-pool-size>20</max-pool-size>    
    <track-statements>false</track-statements>    
    </local-tx-datasource>    
</datasources>