Closed gs-liut closed 3 months ago
Hi @gs-liut, can you config sharding rule for pay_log?
@strongduanmu yes,
ShardingDataSourceConfiguration.java:
public class ShardingDataSourceConfiguration {
private static final String DATABASE_NAME = "gold_coin_logic_db";
@Bean
public DataSource dataSource(DataSourceMapProperties properties) throws SQLException {
return ShardingSphereDataSourceFactory.createDataSource(DATABASE_NAME, createModeConfiguration(), createDataSourceMap(properties), createRuleConfiguration(), createProperties());
}
private ModeConfiguration createModeConfiguration() {
return new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("JDBC", new Properties()));
}
private Collection<RuleConfiguration> createRuleConfiguration() {
ShardingRuleConfiguration configuration = new ShardingRuleConfiguration();
configuration.getAutoTables().add(getPayLogTableRuleConfiguration());
Properties payLogProps = new Properties();
payLogProps.setProperty("sharding-count", "40");
configuration.getShardingAlgorithms().put("pay_log_hash", new AlgorithmConfiguration("HASH_MOD", payLogProps));
Collection<RuleConfiguration> collection = new LinkedList<>();
collection.add(configuration);
return collection;
}
private ShardingAutoTableRuleConfiguration getPayLogTableRuleConfiguration() {
ShardingAutoTableRuleConfiguration configuration = new ShardingAutoTableRuleConfiguration("pay_log", "ds_${0..9}");
configuration.setShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "pay_log_hash"));
return configuration;
}
private Properties createProperties() {
Properties properties = new Properties();
properties.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "false");
properties.setProperty(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY.getKey(), "4");
return properties;
}
private Map<String, DataSource> createDataSourceMap(DataSourceMapProperties properties) {
Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(properties.getDsMap().size());
properties.dsMap.forEach((k, v) -> {
v.setPoolName(properties.getPoolName() + k);
v.setAutoCommit(properties.getAutoCommit());
v.setReadOnly(properties.getReadOnly());
v.setMaximumPoolSize(properties.getMaxPoolSize());
v.setIdleTimeout(properties.getIdleTimeout());
v.setMaxLifetime(properties.getMaxLifetime());
dataSourceMap.put(k, v);
});
return dataSourceMap;
}
@Data
@Component
@ConfigurationProperties(prefix = "sharding")
@PropertySource(value = {"classpath:config/sharding-ds-${spring.profiles.active:dev}.yml"}, encoding = "UTF-8", factory = YamlPropertySourceFactory.class)
public static class DataSourceMapProperties {
private String poolName;
private Boolean autoCommit, readOnly;
private Integer maxPoolSize, idleTimeout, maxLifetime;
private LinkedHashMap<String, HikariDataSource> dsMap;
}
}
Spring JPA config:
spring:
jpa:
show-sql: false
open-in-view: true
database-platform: org.hibernate.dialect.PostgreSQLDialect
The issue involves other third-party dependencies, but our focus is solely on ShardingSphere itself. Since ShardingSphere implements the JDBC interface, standard applications should be functional. We wish to allocate more effort towards enhancing the current version, and therefore will no longer handle such issues. Please read the documentation or provide more effective information when submitting an issue.
Bug Report
Which version of ShardingSphere did you use?
5.3.2
Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
ShardingSphere-JDBC
Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
ShardingDataSourceConfiguration.java:
Spring JPA config:
SQL:
select p1_0.id,p1_0.activities_id from pay_log p1_0 offset ? rows fetch first ? rows only
pom.xml
RepositoryTest
repository.findAll(PageRequest.of(0,10));
throws an exception: