9B4.Multiple DataSource
9B4.Multiple DataSource
Wings uses single data source by default, when there are multiple data sources, the ideas and considerations of using Jooq.
9B4.1.SpringBoot Manually
Features: Manual control, control every operation.
If you need to use jOOQ with multiple data sources, you should create your own DSLContext for each one. See JooqAutoConfiguration for more details.
In particular, JooqExceptionTranslator and SpringTransactionProvider can be reused to provide similar features to what the auto-configuration does with a single DataSource.
According toConfigure jOOQ with Two DataSources, Just set multiple DSLContext.
However, you cannot use the default Dao injection, because it uses the @Primary data source by default. operations on data.
- Use DSLContext to manipulate data
- first new Dao, then setConfiguration(dsl.configuration())
9B4.2.Jooq Automatically
Features: Batch control via the thread context, CURD type, SQL parameters.
void start(ExecuteContext ctx); Overridable attributes in ExecuteContext:
ExecuteContext.connectionProvider(ConnectionProvider): The connection provider used for execution. This may be particularly interesting if a Query was de-serialised and is thus lacking the underlying connection
Switch datasource at execution time using ExecuteListener, the steps are as follows
- Define the ConnectionProvider for multiple datasources
- Implement ExecuteListener.start(ExecuteContext)
- Set the execution datasource ExecuteContext.connectionProvider()
9B4.3.ShardingSphere Automatically
Features: Switch data sources according to the idea of R/W separation.
Automatically switch data at runtime without changing the code, using ShardingSphere configuration.