打开文档导航
R2D1 / 文档

JDBC IndexStore

JdbcIndexStore 将 blocking JDBC database 连接到 R2D1 的 asynchronous IndexStore SPI。它只保存 document identifier 和 indexed field projection,authoritative serialized document 仍在所选 DocumentStore 中。

支持的 database

built-in dialect 根据精确的 JDBC product name 选择。其他 product 会在 schema mutation 前失败。三个 JDBC driver 不会打包进 module,由应用的 DataSource 提供所选 driver。

Database支持的 topology
H2persistent embedded file、TCP server
HSQLDBpersistent embedded file、HSQL server
SQLite仅 local persistent embedded file

创建

传入应用拥有的 DataSource 和用于 blocking JDBC work 的 bounded execution resource。

implementation("dev.nexcraft:r2d1-jdbc:<version>")

DataSource 与 execution 所有权

JdbcIndexStore 不拥有或关闭 DataSource 和 JdbcExecution。JdbcExecution.create(...) 创建的 executor 由创建者关闭,JdbcExecution.using(...) 不会关闭 caller-owned executor。

DataSource dataSource = applicationDataSource;

try (JdbcExecution execution = JdbcExecution.create(4, 64)) {
  JdbcIndexStore indexes = new JdbcIndexStore(dataSource, execution);
  // Pass indexes and indexes::initialize to PersistenceCollectionFactory.
}

Execution mode

默认是 platform thread。Java 25 或更高版本可以显式选择 virtual thread,并且不会静默 fallback。maxConcurrency 和 maxPending 限制 operation admission,不会选择 database topology、改变 physical connection 数量或替代 connection pool。

JdbcExecutionConfig config =
    new JdbcExecutionConfig(JdbcExecutionMode.VIRTUAL_THREAD, 100, 500);
JdbcExecution execution = JdbcExecution.create(config);

Schema 与 SQLite 边界

每个 collection 映射为带有 document_id primary key 和每个 @Index 字段 required column 的 table。支持的 logical value 是 String、long/Long、double/Double、boolean/Boolean;写入时 indexed value 必须 non-null。

SQLite 只支持 local persistent file database,不是 remote SQLite service。单个 JdbcIndexStore instance 会协调写入,但其他 instance 或外部 process 仍遵循 SQLite locking。