Implementation:Apache Shardingsphere JDBCRepository Persist
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Standalone, JDBC |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
JDBCRepository is a JDBC-backed standalone persist repository using HikariDataSource for hierarchical key-value storage in a relational database table.
Description
JDBCRepository implements StandalonePersistRepository and stores metadata as key-value pairs with parent-child relationships in a single database table. On init, it creates a HikariDataSource from configured JDBC properties (URL, username, password, provider) and executes the provider-specific CREATE TABLE SQL loaded via JDBCRepositorySQLLoader. The query method retrieves a value by exact key match. getChildrenKeys lists child keys under a parent path, extracting the immediate child segment and sorting in reverse order. isExisted checks for key presence. persist recursively creates parent path entries before inserting the leaf value, or updates if the key already exists. update sets a new value for an existing key. delete removes all entries matching the key prefix pattern. The repository supports H2 (in-memory), MySQL, and other JDBC-compatible backends via pluggable SQL providers.
Usage
Use this repository when running ShardingSphere in standalone mode with persistent storage requirements. Configure with type: JDBC and provide JDBC URL, username, password, and provider properties. It is suitable for production standalone deployments requiring durable metadata storage.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: JDBCRepository.java
- Lines: 1-235
Signature
@Slf4j
public final class JDBCRepository implements StandalonePersistRepository {
public void init(final Properties props)
public String query(final String key)
public List<String> getChildrenKeys(final String key)
public boolean isExisted(final String key)
public void persist(final String key, final String value)
public void update(final String key, final String value)
public void delete(final String key)
public void close()
public String getType()
}
Import
import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| props | Properties | Yes | JDBC connection properties (jdbc_url, username, password, provider) |
| key | String | Yes | Hierarchical path key (e.g., "/metadata/my_db/rules/sharding") |
| value | String | Yes | Data value to persist or update |
Outputs
| Name | Type | Description |
|---|---|---|
| queryResult | String | Value stored at the key, or empty string if not found |
| childrenKeys | List<String> | Immediate child key names under the parent path |
| isExisted | boolean | Whether the key exists in the repository |
Usage Examples
// Initialize with H2 in-memory database
Properties props = new Properties();
props.setProperty("jdbc_url", "jdbc:h2:mem:config;DB_CLOSE_DELAY=-1");
props.setProperty("provider", "H2");
JDBCRepository repository = new JDBCRepository();
repository.init(props);
// Persist hierarchical data
repository.persist("/metadata/my_db/rules/sharding/tables/t_order", yamlContent);
// Query data
String value = repository.query("/metadata/my_db/rules/sharding/tables/t_order");
// List children
List<String> tables = repository.getChildrenKeys("/metadata/my_db/rules/sharding/tables");
// Clean up
repository.close();