6.9. JDBC 连接工厂
Data Grid 提供不同的 ConnectionFactory 实施,供您连接到数据库。您可以将 JDBC 连接与 SQL 缓存存储搭配使用,以及基于 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 服务器配置的一部分创建受管数据源,以优化 JDBC 数据库连接的连接池和性能。然后,您可以在缓存中指定受管数据源的 JDNI 名称,它将为您的部署集中 JDBC 连接配置。
先决条件
将数据库驱动程序复制到 Data Grid Server 安装中的
server/lib目录中。提示使用带有 Data Grid 命令行界面(CLI)的
install命令,将所需的驱动程序下载到server/lib目录中,例如:install org.postgresql:postgresql:42.4.3
流程
- 打开 Data Grid Server 配置以进行编辑。
-
在
。data-sources部分添加新的数据源 -
使用
name属性或字段唯一标识数据源。 使用
jndi-name属性或字段为数据源指定 JNDI 名称。提示您可以使用 JNDI 名称在 JDBC 缓存存储配置中指定数据源。
-
将
true设置为statistics属性或字段的值,以通过/metrics端点启用数据源的统计信息。 提供 JDBC 驱动程序详细信息,用于定义如何在
connection-factory部分中连接到数据源。-
使用
driver属性或字段指定数据库驱动程序的名称。 -
使用 url 属性或字段指定 JDBC 连接
url。 -
使用
凭证。用户名和密码属性或字段指定 - 根据需要提供任何其他配置。
-
使用
-
定义 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:15.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 服务器时,您可以将 JNDI 名称添加到基于 JDBC 的缓存存储配置中。
先决条件
- 使用托管数据源配置 Data Grid 服务器。
流程
- 打开缓存配置进行编辑。
-
将
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 连接池。
| 属性 | 描述 |
|---|---|
|
| 池应保留的初始连接数。 |
|
| 池中的最大连接数。 |
|
| 池应保留的最小连接数。 |
|
|
在抛出异常前等待连接时的最大时间(毫秒)。如果创建新连接需要很长时间,则这永远不会抛出异常。默认值为 |
|
|
后台验证运行之间的时间(毫秒)。持续时间为 |
|
|
闲置的时间比这个时间更长的时间(以毫秒为单位指定)会在获取前验证(foreground 验证)。持续时间为 |
|
| 在几分钟内,连接必须处于闲置状态才能被删除。 |
|
| 在泄漏警告前,必须保留连接的时间(毫秒)。 |
6.9.2. 使用 Agroal 属性配置 JDBC 连接池 复制链接链接已复制到粘贴板!
您可以使用属性文件为基于 JDBC 字符串的缓存存储配置池连接工厂。
流程
使用
org.infinispan.agroalllowedRegistries 属性指定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将 Data Grid 配置为使用带有
properties-file属性或PooledConnectionFactoryConfiguration.propertyFile ()方法的属性文件。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")