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 2022/07/26 01:15:57 UTC

[GitHub] [shardingsphere] PeiMouRen opened a new issue, #19541: [jdbc]how to use Distributed Primary Key

PeiMouRen opened a new issue, #19541:
URL: https://github.com/apache/shardingsphere/issues/19541

   spring boot config:
   ```
   rules:
         sharding:
           tables:
             t_order: 
               actual-data-nodes: ds$->{0..1}.t_order_$->{1..31}
               database-strategy: 
                 standard:
                   sharding-column: phone 
                   sharding-algorithm-name: database-class-base 
               table-strategy: 
                 standard:
                   sharding-column: logtime
                   sharding-algorithm-name: table-class-base
               key-generate-strategy: 
                 column: order_id 
                 key-generator-name: snowflake 
           sharding-algorithms:
             database-class-base:
               type: CLASS_BASED
               props:
                 strategy: STANDARD
                 algorithmClassName: com.lhx.shardingjdbcdemo.MyShardingAlgorithm
             table-class-base:
               type: CLASS_BASED
               props:
                 strategy: STANDARD
                 algorithmClassName: com.lhx.shardingjdbcdemo.MyShardingAlgorithm11
           key-generators:
             snowflake:
               type: SNOWFLAKE 
   ```
   How do I specify the primary key column value when I insert a piece of data using MyBatis?
   example:
       `insert into t_order(order_id, logtime, phone) values (#{orderId}, #{logtime}, #{phone})`
       How do I get #{orderId} with the Snowflake algorithm


-- 
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: notifications-unsubscribe@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1196206428

   ![image](https://user-images.githubusercontent.com/46043280/181150138-5bd96f9a-c51b-42c7-bf07-61aca97f8e1d.png)
   This method is not called when executing INSERT, can someone help to solve this problem?


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] TeslaCN commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198816071

   I modify the example https://github.com/apache/shardingsphere/tree/master/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example to adapt PostgreSQL and run it. But the key generator worked.
   
   Changes:
   ```
   wuweijie@wuweijie-ubuntu:~/projects/shardingsphere/examples (branch: master!)
   $ git diff | cat
   diff --git a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/AddressMapper.xml b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/AddressMapper.xml
   index 2ade012b8c3..690a8481fe9 100644
   --- a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/AddressMapper.xml
   +++ b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/AddressMapper.xml
   @@ -24,7 +24,7 @@
        </resultMap>
    
        <update id="createTableIfNotExists">
   -        CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id));
   +        CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT primary key, address_name VARCHAR(100) NOT NULL);
        </update>
        
        <update id="truncateTable">
   diff --git a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderItemMapper.xml b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderItemMapper.xml
   index 48aae459c00..d605bc541ec 100644
   --- a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderItemMapper.xml
   +++ b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderItemMapper.xml
   @@ -26,7 +26,7 @@
        </resultMap>
        
        <update id="createTableIfNotExists">
   -        CREATE TABLE IF NOT EXISTS t_order_item (order_item_id BIGINT AUTO_INCREMENT, order_id BIGINT, user_id INT NOT NULL, status VARCHAR(50) , PRIMARY KEY (order_item_id));
   +        CREATE TABLE IF NOT EXISTS t_order_item (order_item_id BIGINT primary key, order_id BIGINT, user_id INT NOT NULL, status VARCHAR(50));
        </update>
        
        <update id="truncateTable">
   diff --git a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderMapper.xml b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderMapper.xml
   index d32c53c0296..040b2d08b29 100644
   --- a/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderMapper.xml
   +++ b/examples/example-core/example-spring-mybatis/src/main/resources/META-INF/mappers/OrderMapper.xml
   @@ -26,7 +26,7 @@
        </resultMap>
    
        <update id="createTableIfNotExists">
   -        CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id));
   +        CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT primary key, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50));
        </update>
        
        <update id="truncateTable">
   diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/pom.xml b/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/pom.xml
   index 0a2d2a1125a..158661be90f 100644
   --- a/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/pom.xml
   +++ b/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/pom.xml
   @@ -48,5 +48,10 @@
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
   +        <dependency>
   +            <groupId>com.h2database</groupId>
   +            <artifactId>h2</artifactId>
   +            <version>1.4.200</version>
   +        </dependency>
        </dependencies>
    </project>
   diff --git a/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties b/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
   index 1630ddc75b2..b06120ed98c 100644
   --- a/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
   +++ b/examples/shardingsphere-jdbc-example/single-feature-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
   @@ -17,17 +17,17 @@
    
    spring.shardingsphere.datasource.names=ds-0,ds-1
    
   -spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
   +spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:postgresql://localhost:5432/demo_ds_0
    spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
   -spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
   -spring.shardingsphere.datasource.ds-0.username=root
   -spring.shardingsphere.datasource.ds-0.password=
   +spring.shardingsphere.datasource.ds-0.driver-class-name=org.postgresql.Driver
   +spring.shardingsphere.datasource.ds-0.username=postgres
   +spring.shardingsphere.datasource.ds-0.password=postgres
    
   -spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
   +spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:postgresql://localhost:5432/demo_ds_1
    spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
   -spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
   -spring.shardingsphere.datasource.ds-1.username=root
   -spring.shardingsphere.datasource.ds-1.password=
   +spring.shardingsphere.datasource.ds-1.driver-class-name=org.postgresql.Driver
   +spring.shardingsphere.datasource.ds-1.username=postgres
   +spring.shardingsphere.datasource.ds-1.password=postgres
    
    spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
    spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database-inline
   ```


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1194880756

   Hi @TeslaCN 
   This is the result of not specifying a primary key
   ![image](https://user-images.githubusercontent.com/46043280/180905369-891f0d7c-b3ab-479a-b0d4-659bf992e643.png)
   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1197938176

   **Environment:**
   jdk:1.8
   spring boot:2.6.4
   shardingsphere-jdbc:5.1.2
   postgresql:12.6
   ---
   **spring boot config:**
   ```yaml
   spring:
     shardingsphere:
       database:
         name:
       mode:
         type: Standalone
         repository:
           type: File
           props:
             path: .shardingsphere
         overwrite: true
       datasource:
         names: ds0, ds1
         ds0:
           type: com.zaxxer.hikari.HikariDataSource
           driver-class-name: org.postgresql.Driver
           jdbc-url: jdbc:postgresql://localhost:5432/shard1?stringtype=unspecified
           username: root
           password: 123
         ds1:
           type: com.zaxxer.hikari.HikariDataSource
           driver-class-name: org.postgresql.Driver
           jdbc-url: jdbc:postgresql://localhost:5432/shard2?stringtype=unspecified
           username: root
           password: 123
       rules:
         sharding:
           tables:
             t_order: 
               actual-data-nodes: ds$->{0..1}.t_order_$->{0..1}
               database-strategy:
                 standard:
                   sharding-column: user_id
                   sharding-algorithm-name: database-inline
               table-strategy:
                 standard:
                   sharding-column: order_id 
                   sharding-algorithm-name: t-order-inline
               key-generate-strategy: 
                 column: order_id 
                 key-generator-name: snowflake
           sharding-algorithms:
             database-inline:
               type: INLINE 
               props:
                 algorithm-expression: ds$->{user_id % 2}
             t-order-inline:
               type: INLINE
               props:
                 algorithm-expression: t_order_$->{order_id % 2}
           key-generators:
             snowflake: 
               type: SNOWFLAKE 
       props:
         sql-show: true
   
   
   ```
   ---
   **DDL:**
   ```sql
   # ds0
   create table t_order_0(order_id bigint, user_id int, remark varchar(200));
   create table t_order_1(order_id bigint, user_id int, remark varchar(200));
   # ds1
   create table t_order_0(order_id bigint, user_id int, remark varchar(200));
   create table t_order_1(order_id bigint, user_id int, remark varchar(200));
   ```
   ---
   **sql:**
   ```java
   
   ...
      @Insert("insert into t_order(user_id, remark) values ( #{userId}, #{remark})")
       void add4(@Param("userId") int userId, @Param("remark") String remark);
   ...
   
   orderDao.add4(100, "test");
   ```
   ---
   **error:**
   Because no order_id was generated.
   this error disappeared when I switched the data source to mysql.
   ```
   org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
   ### Error updating database.  Cause: java.lang.IllegalStateException: Insert statement does not support sharding table routing to multiple data nodes.
   ### The error may exist in com/lhx/shardingjdbcdemo/dao/OrderDao.java (best guess)
   ### The error may involve com.lhx.shardingjdbcdemo.dao.OrderDao.add4-Inline
   ### The error occurred while setting parameters
   ### SQL: insert into t_order(user_id, remark) values ( ?, ?)
   ```
   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1195055930

   ![image](https://user-images.githubusercontent.com/46043280/180937600-8a14c784-aa0b-4851-b129-e486b4d88a7c.png)
   As you can see, the configuration is read, but it does not take effect。


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] TeslaCN commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198735681

   Thanks for your input, we will check this.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] strongduanmu commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198841046

   @PeiMouRen Yes, currently ShardingSphere only supports specifying schema in SQL, otherwise the default public schema is used. You can try to specify schema in SQL to solve this problem.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198846170

   ok. thank you.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198837229

   I think the problem has to do with the schema of PG,because my table is placed under the new schema, instead of the default public schema, we can see someinformation with DEBUG, the `generatedKeyContext`is null,because the `shema` does't contain any tables.
   ![image](https://user-images.githubusercontent.com/46043280/181675110-31c3350f-5632-4a0c-bcf5-3d5b40a4fcf1.png)
   
   ![image](https://user-images.githubusercontent.com/46043280/181675036-27635b92-9023-49f1-b7e3-b9dfb7c283ed.png)
   ![image](https://user-images.githubusercontent.com/46043280/181675462-1b5dc2be-3683-44e8-9586-0f3374899818.png)
   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] TeslaCN commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1194961783

   Could you try putting a breakpoint into `org.apache.shardingsphere.sharding.rule.ShardingRule` and checking the configurations?


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] TeslaCN commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
TeslaCN commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1194866726

   Hi @PeiMouRen 
   You don't need to specify `orderId`. Just
   ```sql
   insert into t_order (logtime, phone) values (#{logtime}, #{phone})
   ```


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] strongduanmu commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198845621

   > ok,the problem is solved when i specifying the schema in sql!!! May I ask again if ShardingSphere plans to make adjustments to this problem?
   
   If there are no other higher priority requirements, we plan to provide support for PostgreSQL `search path` in the next release.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen closed issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen closed issue #19541: [jdbc]how to use Distributed Primary Key
URL: https://github.com/apache/shardingsphere/issues/19541


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198843528

   ok,the problem is solved when i specifying the schema in sql!!!
   May I ask again if ShardingSphere plans to make adjustments to this problem?
   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] PeiMouRen commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
PeiMouRen commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198839992

   When I put the table into the default schema(`public`), distributed primary keys were generated normally. 


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] strongduanmu commented on issue #19541: [jdbc]how to use Distributed Primary Key

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on issue #19541:
URL: https://github.com/apache/shardingsphere/issues/19541#issuecomment-1198823360

   Hi @PeiMouRen, can you check your config file in user home `.shardingsphere` dirctory? It may be caused by old config.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org