You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/10/11 13:32:42 UTC
[GitHub] [shardingsphere] baozhalelele commented on issue #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?
baozhalelele commented on issue #21492:
URL: https://github.com/apache/shardingsphere/issues/21492#issuecomment-1274693328
Thank you for your kindly answer.
Finally, I used reflection to update the `ContextManager` and it seemed to work.
For referenceļ¼
```
ShardingSphereDataSource shardingSphereDataSource = (ShardingSphereDataSource) dataSource;
Class<ShardingSphereDataSource> shardingSphereDataSourceClass = ShardingSphereDataSource.class;
Field field = shardingSphereDataSourceClass.getDeclaredField("contextManager");
field.setAccessible(true);
ContextManager contextManager = (ContextManager) field.get(shardingSphereDataSource);
HikariDataSource newHikariDataSource = new HikariDataSource();
newHikariDataSource.setDriverClassName(tenantDataSource.getDriverClassName());
// ....
Map<String, DataSource> dataSourceMap = contextManager.getDataSourceMap(SHARDING_LOGIC_SCHEMA);
dataSourceMap.put(tenantInfo.getTenantName(), newHikariDataSource);
ShardingSphereResource newResource = new ShardingSphereResource(dataSourceMap);
ShardingSphereDatabase shardingSphereDatabase = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(SHARDING_LOGIC_SCHEMA);
Class<ShardingSphereDatabase> shardingSphereDatabaseClass = ShardingSphereDatabase.class;
Field resource = shardingSphereDatabaseClass.getDeclaredField("resource");
resource.setAccessible(true);
resource.set(shardingSphereDatabase, newResource);
field.set(shardingSphereDataSource, contextManager);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org