6.9. JDBC 接続ファクトリー
Data Grid は、データベースへの接続を可能にするさまざまな ConnectionFactory 実装を提供します。SQL キャッシュストアと JDBC 文字列ベースのキャッシュストアで JDBC 接続を使用します。
接続プール
接続プールは、スタンドアロンの Data Grid デプロイメントに適していますが、Agroal をベースにしています。
XML
<distributed-cache>
<persistence>
<connection-pool connection-url="jdbc:h2:mem:infinispan;DB_CLOSE_DELAY=-1"
username="sa"
password="changeme"
driver="org.h2.Driver"/>
</persistence>
</distributed-cache>
JSON
{
"distributed-cache": {
"persistence": {
"connection-pool": {
"connection-url": "jdbc:h2:mem:infinispan_string_based",
"driver": "org.h2.Driver",
"username": "sa",
"password": "changeme"
}
}
}
}
YAML
distributedCache:
persistence:
connectionPool:
connectionUrl: "jdbc:h2:mem:infinispan_string_based;DB_CLOSE_DELAY=-1"
driver: org.h2.Driver
username: sa
password: changeme
ConfigurationBuilder
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.persistence()
.connectionPool()
.connectionUrl("jdbc:h2:mem:infinispan_string_based;DB_CLOSE_DELAY=-1")
.username("sa")
.driverClass("org.h2.Driver");
管理データソース
データソース接続は、アプリケーションサーバーなどの管理環境に適しています。
XML
<distributed-cache>
<persistence>
<data-source jndi-url="java:/StringStoreWithManagedConnectionTest/DS" />
</persistence>
</distributed-cache>
JSON
{
"distributed-cache": {
"persistence": {
"data-source": {
"jndi-url": "java:/StringStoreWithManagedConnectionTest/DS"
}
}
}
}
YAML
distributedCache:
persistence:
dataSource:
jndiUrl: "java:/StringStoreWithManagedConnectionTest/DS"
ConfigurationBuilder
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.persistence()
.dataSource()
.jndiUrl("java:/StringStoreWithManagedConnectionTest/DS");
セキュアな接続
単純な接続ファクトリーは呼び出しごとにデータベース接続を作成し、テスト環境または開発環境での使用を目的としています。
XML
<distributed-cache>
<persistence>
<simple-connection connection-url="jdbc:h2://localhost"
username="sa"
password="changeme"
driver="org.h2.Driver"/>
</persistence>
</distributed-cache>
JSON
{
"distributed-cache": {
"persistence": {
"simple-connection": {
"connection-url": "jdbc:h2://localhost",
"driver": "org.h2.Driver",
"username": "sa",
"password": "changeme"
}
}
}
}
YAML
distributedCache:
persistence:
simpleConnection:
connectionUrl: "jdbc:h2://localhost"
driver: org.h2.Driver
username: sa
password: changeme
ConfigurationBuilder
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.persistence()
.simpleConnection()
.connectionUrl("jdbc:h2://localhost")
.driverClass("org.h2.Driver")
.username("admin")
.password("changeme");
関連情報
6.9.1. 管理対象データソースの設定
Data Grid Server 設定の一部として管理対象データソースを作成し、JDBC データベース接続の接続プールとパフォーマンスを最適化します。その後、キャッシュ内の管理対象データソースの JDNI 名を指定して、デプロイメントの JDBC 接続設定を一元化できます。
前提条件
-
データベースドライバーを、Data Grid Server インストールの
server/libディレクトリーにコピーします。
手順
- Data Grid Server 設定を開いて編集します。
-
data-sourcesセクションに新しいdata-sourceを追加します。 -
name属性またはフィールドでデータソースを一意に識別します。 jndi-name属性またはフィールドを使用してデータソースの JNDI 名を指定します。ヒントJNDI 名を使用して、JDBC キャッシュストア設定でデータソースを指定します。
-
trueをstatistics属性またはフィールドの値に設定し、/metricsエンドポイント経由でデータソースの統計を有効にします。 connection-factoryセクションのデータソースへの接続方法を定義する JDBC ドライバーの詳細を提供します。-
driver属性またはフィールドを使用して、データベースドライバーの名前を指定します。 -
url属性またはフィールドを使用して、JDBC 接続 URL を指定します。 -
usernameおよびpassword属性またはフィールドを使用して、認証情報を指定します。 - 必要に応じて他の設定を指定します。
-
-
Data Grid Server ノードが接続をプールして再利用する方法を、
connection-poolセクションの接続プール調整プロパティーで定義します。 - 変更を設定に保存します。
検証
以下のように、Data Grid コマンドラインインターフェイス (CLI) を使用して、データソース接続をテストします。
CLI セッションを開始します。
bin/cli.sh
すべてのデータソースを一覧表示し、作成したデータソースが利用できることを確認します。
server datasource ls
データソース接続をテストします。
server datasource test my-datasource
管理対象データソースの設定
XML
<server xmlns="urn:infinispan:server:13.0">
<data-sources>
<!-- Defines a unique name for the datasource and JNDI name that you
reference in JDBC cache store configuration.
Enables statistics for the datasource, if required. -->
<data-source name="ds"
jndi-name="jdbc/postgres"
statistics="true">
<!-- Specifies the JDBC driver that creates connections. -->
<connection-factory driver="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/postgres"
username="postgres"
password="changeme">
<!-- Sets optional JDBC driver-specific connection properties. -->
<connection-property name="name">value</connection-property>
</connection-factory>
<!-- Defines connection pool tuning properties. -->
<connection-pool initial-size="1"
max-size="10"
min-size="3"
background-validation="1000"
idle-removal="1"
blocking-timeout="1000"
leak-detection="10000"/>
</data-source>
</data-sources>
</server>
JSON
{
"server": {
"data-sources": [{
"name": "ds",
"jndi-name": "jdbc/postgres",
"statistics": true,
"connection-factory": {
"driver": "org.postgresql.Driver",
"url": "jdbc:postgresql://localhost:5432/postgres",
"username": "postgres",
"password": "changeme",
"connection-properties": {
"name": "value"
}
},
"connection-pool": {
"initial-size": 1,
"max-size": 10,
"min-size": 3,
"background-validation": 1000,
"idle-removal": 1,
"blocking-timeout": 1000,
"leak-detection": 10000
}
}]
}
}
YAML
server:
dataSources:
- name: ds
jndiName: 'jdbc/postgres'
statistics: true
connectionFactory:
driver: "org.postgresql.Driver"
url: "jdbc:postgresql://localhost:5432/postgres"
username: "postgres"
password: "changeme"
connectionProperties:
name: value
connectionPool:
initialSize: 1
maxSize: 10
minSize: 3
backgroundValidation: 1000
idleRemoval: 1
blockingTimeout: 1000
leakDetection: 10000
6.9.1.1. JNDI 名を使用したキャッシュの設定
管理対象データソースを Data Grid Server に追加するとき、JNDI 名を JDBC ベースのキャッシュストア設定に追加できます。
前提条件
- 管理対象データソースを使用した Data Grid Server の設定
手順
- キャッシュ設定を開いて編集します。
-
data-source要素またはフィールドを JDBC ベースのキャッシュストア設定に追加します。 -
管理対象データソースの JNDI 名を
jndi-url属性の値として指定します。 - JDBC ベースのキャッシュストアを適宜設定します。
- 変更を設定に保存します。
キャッシュ設定の JNDI 名
XML
<distributed-cache>
<persistence>
<jdbc:string-keyed-jdbc-store>
<!-- Specifies the JNDI name of a managed datasource on Data Grid Server. -->
<jdbc:data-source jndi-url="jdbc/postgres"/>
<jdbc:string-keyed-table drop-on-exit="true" create-on-start="true" prefix="TBL">
<jdbc:id-column name="ID" type="VARCHAR(255)"/>
<jdbc:data-column name="DATA" type="BYTEA"/>
<jdbc:timestamp-column name="TS" type="BIGINT"/>
<jdbc:segment-column name="S" type="INT"/>
</jdbc:string-keyed-table>
</jdbc:string-keyed-jdbc-store>
</persistence>
</distributed-cache>
JSON
{
"distributed-cache": {
"persistence": {
"string-keyed-jdbc-store": {
"data-source": {
"jndi-url": "jdbc/postgres"
},
"string-keyed-table": {
"prefix": "TBL",
"drop-on-exit": true,
"create-on-start": true,
"id-column": {
"name": "ID",
"type": "VARCHAR(255)"
},
"data-column": {
"name": "DATA",
"type": "BYTEA"
},
"timestamp-column": {
"name": "TS",
"type": "BIGINT"
},
"segment-column": {
"name": "S",
"type": "INT"
}
}
}
}
}
}
YAML
distributedCache:
persistence:
stringKeyedJdbcStore:
dataSource:
jndi-url: "jdbc/postgres"
stringKeyedTable:
prefix: "TBL"
dropOnExit: true
createOnStart: true
idColumn:
name: "ID"
type: "VARCHAR(255)"
dataColumn:
name: "DATA"
type: "BYTEA"
timestampColumn:
name: "TS"
type: "BIGINT"
segmentColumn:
name: "S"
type: "INT"
6.9.1.2. 接続プールのチューニングプロパティー
Data Grid Server 設定で、管理対象データソースの JDBC 接続プールを調整できます。
| プロパティー | 説明 |
|---|---|
|
| プールが保持する最初の接続数。 |
|
| プールの最大接続数。 |
|
| プールが保持する必要のある接続の最小数。 |
|
|
例外が発生する前に、接続を待機している間にブロックする最大時間 (ミリ秒単位)。新しい接続の作成に非常に長い時間がかかる場合は、これによって例外が出力されることはありません。デフォルトは |
|
|
バックグラウンド検証の実行の間隔 (ミリ秒単位)。期間 |
|
|
ミリ秒単位で指定された、この時間より長いアイドル状態の接続は、取得される前に検証されます (フォアグラウンド検証)。期間 |
|
| 削除される前の接続がアイドル状態でなくてはならない時間 (分単位)。 |
|
| リーク警告の前に接続を保持しなければならない時間 (ミリ秒単位)。 |
6.9.2. Agroal プロパティーを使用した JDBC 接続プールの設定
プロパティーファイルを使用して、JDBC 文字列ベースのキャッシュストアにプールされた接続ファクトリーを設定できます。
手順
以下の例のように、
org.infinispan.agroal.*プロパティーで JDBC 接続プール設定を指定します。org.infinispan.agroal.metricsEnabled=false org.infinispan.agroal.minSize=10 org.infinispan.agroal.maxSize=100 org.infinispan.agroal.initialSize=20 org.infinispan.agroal.acquisitionTimeout_s=1 org.infinispan.agroal.validationTimeout_m=1 org.infinispan.agroal.leakTimeout_s=10 org.infinispan.agroal.reapTimeout_m=10 org.infinispan.agroal.metricsEnabled=false org.infinispan.agroal.autoCommit=true org.infinispan.agroal.jdbcTransactionIsolation=READ_COMMITTED org.infinispan.agroal.jdbcUrl=jdbc:h2:mem:PooledConnectionFactoryTest;DB_CLOSE_DELAY=-1 org.infinispan.agroal.driverClassName=org.h2.Driver.class org.infinispan.agroal.principal=sa org.infinispan.agroal.credential=sa
properties-file属性またはPooledConnectionFactoryConfiguration.propertyFile()メソッドでプロパティーファイルを使用するように Data Grid を設定します。XML
<connection-pool properties-file="path/to/agroal.properties"/>
JSON
"persistence": { "connection-pool": { "properties-file": "path/to/agroal.properties" } }YAML
persistence: connectionPool: propertiesFile: path/to/agroal.propertiesConfigurationBuilder
.connectionPool().propertyFile("path/to/agroal.properties")
関連情報