15271944243 opened a new issue, #24608:
URL: https://github.com/apache/shardingsphere/issues/24608
When I use custom HintShardingAlgorithm, sometimes I don't want to set the
sharding value,and I want Sharding-JDBC to still execute my custom
HintShardingAlgorithm
My custom HintShardingAlgorithm is named DynamicDatasourceShardingAlgorithm;
I have 2 datasource called ipspace0,ipspace1;
I use annotations(@ShardingDataSource) to mark methods,
- When a method has no @ShardingDataSource,it accesses ipspace0,eg:
TaskRecordManager.queryUserTaskRecord
- When a method has a @ShardingDataSource,it accesses ipspace1,eg:
TaskRecordManager.queryUserTaskRecordV2
The code is as follows,I found that when executing methods without
@ShardingDataSource,sharding-jdbc is not executed My custom
HintShardingAlgorithm(DynamicDatasourceShardingAlgorithm),because My code are
not performed `HintManager.addDatabaseShardingValue("user_task_record", 1L);`
so, How can sharding-jdbc executed DynamicDatasourceShardingAlgorithm when
HintManager set none sharding value
version: 4.1.1
```
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
```
properties:
```
spring.shardingsphere.datasource.names=ipspace0,ipspace1
spring.shardingsphere.sharding.default-data-source-name=ipspace0
spring.shardingsphere.datasource.ipspace0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ipspace0.url=jdbc:trace:mysql://xxx.xxx.xx.xx:xxxx/...
...
spring.shardingsphere.datasource.ipspace1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ipspace1.url=jdbc:trace:mysql://xxx.xxx.xx.xx:xxxx/...
...
spring.shardingsphere.sharding.tables.user_task_record.actual-data-nodes=ipspace$->{0..1}.user_task_record_$->{0..99}
spring.shardingsphere.sharding.tables.user_task_record.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.user_task_record.table-strategy.inline.algorithm-expression=user_task_record_$->{Long.valueOf(user_id)
% 100}
spring.shardingsphere.sharding.tables.user_task_record.database-strategy.hint.algorithm-class-name=xxx.DynamicDatasourceShardingAlgorithm
...
spring.shardingsphere.props.sql.show=true
```
```
public class DynamicDatasourceShardingAlgorithm implements
HintShardingAlgorithm<Long> {
private static final String DEFAULT_DATASOURCE = "ipspace0";
private static final String DATASOURCE_PREFIX = "ipspace";
@Override
public Collection<String> doSharding(Collection<String>
availableTargetNames,
HintShardingValue<Long>
shardingValue) {
List<String> result = new ArrayList<>();
if (shardingValue == null || shardingValue.getValues() == null ||
shardingValue.getValues().isEmpty()) {
result.add(DEFAULT_DATASOURCE);
} else {
Long num = new ArrayList<>(shardingValue.getValues()).get(0);
result.add(DATASOURCE_PREFIX + num);
}
return result;
}
}
```
```
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ShardingDataSource {
}
```
```
@Slf4j
@Component
@Aspect
public class ShardingAop {
@Pointcut("@annotation(xxx.ShardingDataSource)")
public void pointcut() {
}
@Around("pointcut()")
public Object shardingHint(ProceedingJoinPoint joinPoint) throws
Throwable {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.addDatabaseShardingValue("user_task_record", 1L);
Object[] args = joinPoint.getArgs();
return joinPoint.proceed(args);
} catch (Throwable throwable) {
log.error("shardingHint exception error", throwable);
throw throwable;
}
}
}
```
```
@Component
@Slf4j
public class TaskRecordManager {
@Autowired
private UserTaskRecordDOMapper userTaskRecordDOMapper;
public List<UserTaskRecordDO> queryUserTaskRecord(TaskRecordQueryDTO
queryDTO) {
if (queryDTO == null) {
return null;
}
List<UserTaskRecordDO> userTaskRecordDOS =
userTaskRecordDOMapper.selectByCondition(queryDTO);
LOGGER.debug("queryUserTaskRecord queryDTO:{},
userTaskRecordDOS:{}.", queryDTO, userTaskRecordDOS);
return userTaskRecordDOS;
}
@ShardingDataSource
public List<UserTaskRecordDO> queryUserTaskRecordV2(TaskRecordQueryDTO
queryDTO) {
if (queryDTO == null) {
return null;
}
List<UserTaskRecordDO> userTaskRecordDOS =
userTaskRecordDOMapper.selectByCondition(queryDTO);
LOGGER.debug("queryUserTaskRecord queryDTO:{},
userTaskRecordDOS:{}.", queryDTO, userTaskRecordDOS);
return userTaskRecordDOS;
}
}
```
--
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]