Principle:APIJSON APIJSON Demo Multi Datasource Routing
| Knowledge Sources | |
|---|---|
| Domains | Database, Architecture, Configuration |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Architectural pattern that enables a single application to route database operations to multiple datasources (MySQL, PostgreSQL, Redis, Elasticsearch, Kafka) at runtime based on request-level datasource selection, without separate connection management per data store.
Description
Multi-Datasource Routing allows APIJSON to query different database systems from a single endpoint. At startup, all configured datasources are registered in a static map with their connection details (URL, schema, database type, version, credentials). At request time, the @datasource JSON keyword selects which datasource to use, and the framework routes the connection lookup accordingly.
This solves:
- Polyglot persistence: Different data types suit different stores (relational for transactions, Redis for caching, Elasticsearch for search, Kafka for streaming).
- Environment isolation: Test, production, and online datasources can coexist with explicit routing.
- Dynamic scaling: New datasources can be added at runtime without code changes to controllers.
The routing mechanism uses Spring's AbstractRoutingDataSource pattern: a dynamic routing datasource delegates to target datasources based on a lookup key. APIJSON extends this by maintaining a dataSourceMap with per-datasource metadata (database type, schema, version) used for SQL dialect generation.
Usage
Use this principle when:
- An application needs to access multiple database systems (MySQL + Redis + Elasticsearch) from one codebase.
- You want per-request datasource selection rather than per-service or per-module separation.
- Different datasources need different SQL dialect handling (MySQL vs. PostgreSQL syntax).
Theoretical Basis
Startup (DynamicDataSource.run):
1. initJdbcDataSource():
For each Spring-managed datasource:
- Extract URL, schema, database type, credentials
- Query DB version (SELECT version())
- Store DynamicDataSourceConfig in dataSourceMap
2. initRedisCluster() / initRedisSingle():
- Create Redis executors for cluster/single modes
- Store with database=DATABASE_REDIS in dataSourceMap
Request Time (DemoSQLExecutor.getConnection):
1. config.getDatasource() → datasource name (from @datasource keyword)
2. key = datasource + "-" + config.getDatabase()
3. Lookup connection in connection map by key
4. If miss: resolve DataSource bean from Spring context
- "DRUID" → druidDataSource bean
- "DRUID-TEST" → druidTestDataSource bean
- "DRUID-ONLINE" → druidOnlineDataSource bean
5. Get connection from pool, store in map
Datasource types supported:
- JDBC (MySQL, PostgreSQL, Oracle, etc.): Routed via Druid connection pool
- Redis: Routed via Jedis-based RedisExecutor (cluster, sentinel, or single mode)
- Elasticsearch: Routed via ESOptions configuration
- Kafka: Routed via KafkaSimpleProducer
Related Pages
Implemented By
- Implementation:APIJSON_APIJSON_Demo_DemoDataSourceConfig
- Implementation:APIJSON_APIJSON_Demo_DynamicDataSource
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor_BigData
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor_ES
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor_Kafka
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor_RediSQL
- Implementation:APIJSON_APIJSON_Demo_DemoSQLExecutor_SimpleDemo
- Implementation:APIJSON_APIJSON_Demo_DynamicJdbcDataSource
- Implementation:APIJSON_APIJSON_Demo_DynamicDataSource_Kafka
- Implementation:APIJSON_APIJSON_Demo_DemoSQLConfig_ES
- Implementation:APIJSON_APIJSON_Demo_JedisClusterUtil
- Implementation:APIJSON_APIJSON_Demo_RediSQLClient
- Implementation:APIJSON_APIJSON_Demo_JedisBuildData
- Implementation:APIJSON_APIJSON_Demo_RedisBuildData
- Implementation:APIJSON_APIJSON_Demo_RedisDataSource
- Implementation:APIJSON_APIJSON_Demo_DataBuildResultSet