6.9. JDBC 连接工厂

数据网格提供了不同的 ConnectionFactory 实施,可让您连接到数据库。您可以使用带有 SQL 缓存存储和 JDBC 字符串的缓存存储的 JDBC 连接。

连接池

连接池适合独立数据网格部署,基于 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");

managed datasources

数据源连接适用于应用服务器等受管环境。

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 服务器配置的一部分创建受管 datasources,以优化 JDBC 数据库连接的连接池和性能。然后,您可以在缓存中指定受管数据源的 JDNI 名称,这会集中部署的 JDBC 连接配置。

先决条件

  • 将数据库驱动程序复制到 Data Grid Server 安装中的 server/lib 目录中。

流程

  1. 打开 Data Grid Server 配置进行编辑。
  2. 将新的 数据源添加到 data- sources 部分。
  3. 通过 name 属性或字段唯一标识数据源。
  4. 使用 jndi-name 属性或字段为数据源指定 JNDI 名称。

    提示

    您可以使用 JNDI 名称在 JDBC 缓存存储配置中指定数据源。

  5. true 设置为 statistics 属性或字段的值,以通过 /metrics 端点为数据源启用统计。
  6. 提供 JDBC 驱动程序详细信息,以定义如何在 connection-factory 部分中定义数据源连接。

    1. 使用 driver 属性或字段指定数据库驱动程序的名称。
    2. 使用 url 属性或字段指定 JDBC 连接 url
    3. 使用 用户名和密码 属性或字段指定凭证。
    4. 根据需要提供任何其他配置。
  7. connection-pool 部分中,定义 Data Grid Server 节点池并重复使用与连接池调整属性的连接。
  8. 保存对您的配置的更改。

验证

使用 Data Grid 命令行界面(CLI)测试数据源连接,如下所示:

  1. 启动 CLI 会话。

    bin/cli.sh
  2. 列出所有数据源,并确认您创建的数据源可用。

    server datasource ls
  3. 测试数据源连接。

    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 服务器时,您可以将 JNDI 名称添加到基于 JDBC 的缓存存储配置中。

先决条件

  • 使用受管理数据源配置 Data Grid 服务器。

流程

  1. 打开缓存配置进行编辑。
  2. data-source 元素或字段添加到基于 JDBC 的缓存存储配置中。
  3. 将受管数据源的 JNDI 名称指定为 jndi-url 属性的值。
  4. 根据情况配置基于 JDBC 的缓存存储。
  5. 保存对您的配置的更改。
缓存配置中的 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 配置中为 managed datasources 调优 JDBC 连接池。

属性描述

initial-size

池应有的初始连接数。

max-size

池中的最大连接数。

min-size

池应拥有的最低连接数。

blocking-timeout

在引发异常前,在等待连接时以毫秒为单位进行阻止的最大时间(毫秒)。如果创建新连接需要较长的时间内,这不会抛出异常。默认值为 0, 表示调用将无限期等待。

background-validation

后台验证运行间隔的时间(毫秒)。0 持续时间表示禁用这个功能。

validate-on-acquisition

在获取前,连接闲置时间超过这个值(以毫秒为单位指定)在被验证前进行验证。0 持续时间表示禁用这个功能。

idle-removal

在删除连接前,连接必须闲置时间(以分钟为单位)。

leak-detection

在泄漏警告前,连接必须保留以毫秒为单位。