You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/09/20 12:48:09 UTC

[GitHub] [incubator-shardingsphere] redwolf2019 opened a new issue #3075: sharding fully support with JOOQ?

redwolf2019 opened a new issue #3075: sharding fully support with JOOQ?
URL: https://github.com/apache/incubator-shardingsphere/issues/3075
 
 
   ### Question
   - When I try to use JOOQ with Sharding-JDBC insert one record, but actually the SQL execute twice. Did I write the wrong configuration code?
   
   ### Jar verson
   - JOOQ:3.11.11
   - sharding-jdbc-core:4.0.0-RC2
   
   ### ShardingJDBC code
   ```
      public DataSource shardingDataSource() throws SQLException {
           ShardingRuleConfiguration cfg = new ShardingRuleConfiguration();
           cfg.getTableRuleConfigs().add(getDeviceTableCfg());
   
           HikariConfig hikariConfig = new HikariConfig();
           hikariConfig.setDriverClassName(dataSourceProperties.getDriverClassName());
           hikariConfig.setJdbcUrl(dataSourceProperties.getUrl());
           hikariConfig.setUsername(dataSourceProperties.getUsername());
           hikariConfig.setPassword(dataSourceProperties.getPassword());
           HikariDataSource dataSource = new HikariDataSource(hikariConfig);
   
           Map<String, DataSource> result = new HashMap<>();
           result.put("ds", dataSource);
   
           return ShardingDataSourceFactory.createDataSource(result, cfg, new Properties());
       }
   
       private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
           return new KeyGeneratorConfiguration("SNOWFLAKE", "id");
       }
   
       private TableRuleConfiguration getDeviceTableCfg() {
           TableRuleConfiguration result = new TableRuleConfiguration("device", "ds.device_${0..1}");
           result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
           return result;
       }
   ```
   
   ### JOOQ code
   ```
      public DSLContext shardingDslContest() {
           org.jooq.Configuration configuration = new DefaultConfiguration()
                   .set( shardingDataSource())
                   .set(new Settings().withRenderSchema(false)
                           .withExecuteLogging(true)
                           .withRenderFormatted(false))
                   .set(SQLDialect.MYSQL);
           return DSL.using(configuration);
       }
   ```
   
   ### SQL code
   ```
   import static example.generator.tables.Device.DEVICE;
   
   
      void test(){
           DeviceRecord record = new DeviceRecord();
           record.setCode(UUID.randomUUID().toString());
           record.setName(UUID.randomUUID().toString());
           sharding.insertInto(DEVICE).set(record).execute();
       }
   ```
   
   ### LogicTable
   ```
   create table if not exists device
   (
     id          bigint       not null
       primary key,
     code        varchar(64)  not null,
     name        varchar(64)  not null,
     description varchar(255) null,
     constraint device_code_uindex
       unique (code),
     constraint device_name_uindex
       unique (name)
   );
   ```
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services