zhyyu opened a new issue, #8560:
URL: https://github.com/apache/shardingsphere/issues/8560
## Bug Report
### Which version of ShardingSphere did you use?
```
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.1.1</version>
</dependency>
```
### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
- ShardingSphere-JDBC
### Expected behavior
- get complete schema data (so can use keyGenerate)
- insert row using generated key
### Actual behavior
- schema tableMeta don't have generated key (when using mysql dataSource is
ok, but h2 database wrong)
- sql exception : org.springframework.dao.DataIntegrityViolationException:
PreparedStatementCallback; SQL [insert into test_snow_flake (name) values (?)];
NULL not allowed for column "ID"; SQL statement:
### Reason analyze (If you can)
### Steps to reproduce the behavior, such as: SQL to execute, sharding rule
configuration, when exception occur etc.
- /resouces/schema.sql
```
CREATE TABLE `test_snow_flake0`
(
`id` bigint,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `test_snow_flake1`
(
`id` bigint,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
);
```
- dataSource config
```
@Bean
public DataSource shardingJdbcSnowFlakeDataSource() throws SQLException {
// 配置真实数据源
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 配置第一个数据源
// HikariDataSource dataSource1 = getMysqlDataSource();
HikariDataSource dataSource1 = getH2DataSource();
dataSourceMap.put("test_schema_1", dataSource1);
// 配置 test_snow_flake 表规则
TableRuleConfiguration testTableTableRuleConfig = new
TableRuleConfiguration("test_snow_flake","test_schema_1.test_snow_flake${0..1}");
// 自定义分片规则
StandardShardingStrategyConfiguration
standardShardingStrategyConfiguration = new
StandardShardingStrategyConfiguration("name", new PreciseShardingAlgorithm() {
@Override
public String doSharding(Collection availableTargetNames,
PreciseShardingValue shardingValue) {
String name = (String) shardingValue.getValue();
if (StringUtils.isEmpty(name)) {
return (String)
availableTargetNames.stream().findFirst().get();
}
if (name.startsWith("a")) {
return "test_snow_flake0";
}
return "test_snow_flake1";
}
});
testTableTableRuleConfig.setTableShardingStrategyConfig(standardShardingStrategyConfiguration);
// 自定义分布式主键
testTableTableRuleConfig.setKeyGeneratorConfig(new
KeyGeneratorConfiguration("SNOWFLAKE", "id"));
// 配置分片规则
ShardingRuleConfiguration shardingRuleConfig = new
ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(testTableTableRuleConfig);
// 获取数据源对象
Properties properties = new Properties();
properties.setProperty("sql.show", "true");
DataSource dataSource =
ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,
properties);
return dataSource;
}
private HikariDataSource getMysqlDataSource() {
HikariDataSource dataSource1 = new HikariDataSource();
dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/test_schema_1");
dataSource1.setUsername("root");
dataSource1.setPassword("xxxx");
return dataSource1;
}
private HikariDataSource getH2DataSource() {
HikariDataSource dataSource1 = new HikariDataSource();
dataSource1.setDriverClassName("org.h2.Driver");
dataSource1.setJdbcUrl("jdbc:h2:mem:test_schema_1");
dataSource1.setUsername("sa");
return dataSource1;
}
```
- test case
```
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSnowFlakeDAOTest {
@Autowired
private TestSnowFlakeDAO testSnowFlakeDAO;
@Test
public void testInsert() {
TestSnowFlakeDAO.TestSnowFlakeRow testSnowFlakeRow = new
TestSnowFlakeDAO.TestSnowFlakeRow();
testSnowFlakeRow.setName("name0");
Integer insertCount = testSnowFlakeDAO.insert(testSnowFlakeRow);
assertEquals(insertCount, Integer.valueOf(1));
TestSnowFlakeDAO.TestSnowFlakeRow testSnowFlakeRow2 = new
TestSnowFlakeDAO.TestSnowFlakeRow();
testSnowFlakeRow2.setName("aname0");
testSnowFlakeDAO.insert(testSnowFlakeRow2);
}
}
```
### Example codes for reproduce this issue (such as a github link).
[github repo](https://github.com/zhyyu/learn-sharding-jdbc)
--
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]