wsYdd commented on issue #7701:
URL:
https://github.com/apache/shardingsphere/issues/7701#issuecomment-1067921243
@Minerva-Li
Config datasource and create ShardingDataSource , start Application meet
this problem.
ShardingSphere version is 4.1.1.
`@Configuration
public class MysqlDataSourceConfig {
public static final String DATASOURCE_NAME = "DataSource";
@Value("${mysql.datasource.url}")
private String connUrl;
@Value("${mysql.datasource.username}")
private String connUser;
@Value("${mysql.datasource.password}")
private String connPassword;
@Primary
@Bean(name = DATASOURCE_NAME)
public DataSource mysqlDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(connUrl);
dataSource.setUsername(connUser);
dataSource.setPassword(connPassword);
dataSource.setDbType(DbType.mysql);
// skip other config
dataSource.init();
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("ds", dataSource);
TableRuleConfiguration tOrderConfiguration = new
TableRuleConfiguration("t_order", "ds.t_order_${0..1}");
tOrderConfiguration.setTableShardingStrategyConfig(new
InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
KeyGeneratorConfiguration keyGeneratorConfiguration = new
KeyGeneratorConfiguration("SNOWFLAKE", "order_id");
tOrderConfiguration.setKeyGeneratorConfig(keyGeneratorConfiguration);
ShardingRuleConfiguration shardingRuleConfiguration = new
ShardingRuleConfiguration();
shardingRuleConfiguration.getTableRuleConfigs().add(tOrderConfiguration);
return ShardingDataSourceFactory.createDataSource(dataSourceMap,
shardingRuleConfiguration, new Properties());
}
@Bean(name = "mysqlTransactionManager")
public DataSourceTransactionManager mysqlTransactionManager() throws
SQLException {
return new DataSourceTransactionManager(mysqlDataSource());
}
}`
Then load before application start, it don't work.
`
@SpringBootApplication
@EnableAsync
@EnableScheduling
@MapperScan(basePackages = {"XXXXXXX"})
@EnableAutoConfiguration(exclude = {ElasticsearchAutoConfiguration.class,
RestClientAutoConfiguration.class})
public class StartEngine {
public static void main(String[] args) {
NewInstanceServiceLoader.register(SQLParserConfiguration.class);
SpringApplication.run(StartEngine.class, args);
}
}`
--
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]