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 07:32:20 UTC

[GitHub] [shardingsphere] baozhalelele opened a new issue, #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?

baozhalelele opened a new issue, #21492:
URL: https://github.com/apache/shardingsphere/issues/21492

   ## Question
   
   Hi, I am using ShardingJdbc 5.1.2 and mybatis-plus.
   I want to create a new datasource dynamically and it could be add to ShardingSphereDataSource for management.
   When in ShardingJdbc 4.x it has a way to realize such as below:
   
   ```
   ShardingDataSource shardingDataSource = (ShardingDataSource)dataSource;
   Map<String, DataSource> dataSourceMap = shardingDataSource.getDataSourceMap();
   ShardingRule shardingRule = shardingDataSource.getRuntimeContext().getRule();
       
   HikariDataSource newHikariDataSource = new HikariDataSource();
   newHikariDataSource.setDriverClassName(tenantDataSource.getDriverClassName());
   // ....
   
   dataSourceMap.put(tenantDataSource.getTenantName(),hikariDataSourceNew);
   shardingRule.getShardingDataSourceNames().getDataSourceNames().add(tenantDataSource.getTenantName());
   ```
   
   But in ShardingJdbc 5.1.2 it seems that I could not add a new datasource for ShardingJdbc directly.
   I would appreciate if there is any proposal to me.Thank you. 


-- 
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.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] huang714669 commented on issue #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?

Posted by GitBox <gi...@apache.org>.
huang714669 commented on issue #21492:
URL: https://github.com/apache/shardingsphere/issues/21492#issuecomment-1275443890

   Thank you for you guys, it's helpful for me with the reflection solution!


-- 
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


[GitHub] [shardingsphere] zhaojinchao95 commented on issue #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?

Posted by GitBox <gi...@apache.org>.
zhaojinchao95 commented on issue #21492:
URL: https://github.com/apache/shardingsphere/issues/21492#issuecomment-1274258279

   @baozhalelele Hi, you can use `ShardingSphere-Proxy` &` ShardingSphere-JDBC` Hybrid deployment. `ShardingSphere-JDBC` can perceive new datasource when you execut[ DistSQL RDL ADD RESOURCE](https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/distsql/syntax/rdl/resource-definition/) in the `ShardingSphere-Proxy`


-- 
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


[GitHub] [shardingsphere] baozhalelele closed issue #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?

Posted by GitBox <gi...@apache.org>.
baozhalelele closed issue #21492: Is there any way to add a new datasource to ShardingSphere dynamically so that it could be managed by ShardingSphereDataSource?
URL: https://github.com/apache/shardingsphere/issues/21492


-- 
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


[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?

Posted by GitBox <gi...@apache.org>.
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