RaigorJiang commented on issue #12703:
URL:
https://github.com/apache/shardingsphere/issues/12703#issuecomment-927265494
@wangwangwangBoy
Let me talk about raw java first, I verified the problem:
1. Your bean `DruidDataSource` is declared as @Primary, so Mybatis will not
get the beans created by shardingsphere when using DataSource.
2. You can create SqlSessionFactoryBean by yourself and specify the bean
constructed using shardingsphere, so that your goal can be achieved.
sample code :
```java
@Bean("sharding")
public DataSource dataSource() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(0);
dataSourceMap.put("my_test_db", createDataSource());
......
```
```java
@Bean
public SqlSessionFactoryBean mysqlSessionFactory(@Qualifier("sharding")
DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new
SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new
PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return sqlSessionFactoryBean;
}
```
My test log shows that the custom algorithm has been used:
```
### Cause: java.lang.UnsupportedOperationException: No available tables
according input time:202007] with root cause
java.lang.UnsupportedOperationException: No available tables according input
time:202007
at
com.mine.shardingjdbc.myconfig.TableShardingAlgorithm.doSharding(TableShardingAlgorithm.java:51)
~[classes/:na]
at
org.apache.shardingsphere.sharding.route.strategy.type.standard.StandardShardingStrategy.doSharding(StandardShardingStrategy.java:68)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.strategy.type.standard.StandardShardingStrategy.doSharding(StandardShardingStrategy.java:57)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.routeTables(ShardingStandardRoutingEngine.java:214)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.route0(ShardingStandardRoutingEngine.java:194)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.routeByShardingConditionsWithCondition(ShardingStandardRoutingEngine.java:114)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.routeByShardingConditions(ShardingStandardRoutingEngine.java:107)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.getDataNodes(ShardingStandardRoutingEngine.java:84)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine.route(ShardingStandardRoutingEngine.java:69)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.ShardingSQLRouter.createRouteContext(ShardingSQLRouter.java:61)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.sharding.route.engine.ShardingSQLRouter.createRouteContext(ShardingSQLRouter.java:47)
~[shardingsphere-sharding-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor.route(PartialSQLRouteExecutor.java:62)
~[shardingsphere-infra-route-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.infra.route.engine.SQLRouteEngine.route(SQLRouteEngine.java:52)
~[shardingsphere-infra-route-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.infra.context.kernel.KernelProcessor.route(KernelProcessor.java:54)
~[shardingsphere-infra-context-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.infra.context.kernel.KernelProcessor.generateExecutionContext(KernelProcessor.java:46)
~[shardingsphere-infra-context-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.createExecutionContext(ShardingSpherePreparedStatement.java:363)
~[shardingsphere-jdbc-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.execute(ShardingSpherePreparedStatement.java:271)
~[shardingsphere-jdbc-core-5.0.0-beta.jar:5.0.0-beta]
at
org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
~[mybatis-3.4.6.jar:3.4.6]
at
org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
~[mybatis-3.4.6.jar:3.4.6]
at
org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
~[mybatis-3.4.6.jar:3.4.6]
at
org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
~[mybatis-3.4.6.jar:3.4.6]
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]