Giter VIP home page Giter VIP logo

How to resolve problem that java.sql.SQLException: Please check your sharding conditions 'ShardingConditions(conditions=[ShardingCondition(values=[t_ent_order_item.order_id = 2], startIndex=0)], to avoid same record in table 't_ent_order_item' routing to multiple data nodes. about shardingsphere HOT 7 CLOSED

JianfeiMa avatar JianfeiMa commented on August 21, 2024
How to resolve problem that java.sql.SQLException: Please check your sharding conditions 'ShardingConditions(conditions=[ShardingCondition(values=[t_ent_order_item.order_id = 2], startIndex=0)], to avoid same record in table 't_ent_order_item' routing to multiple data nodes.

from shardingsphere.

Comments (7)

JianfeiMa avatar JianfeiMa commented on August 21, 2024

My configuration :
java.util.Properties propertiesForTEntOrderItem = new java.util.Properties();
propertiesForTEntOrderItem.setProperty("algorithm-expression", "t_ent_order_item_${order_id % 2}");
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration algorithmConfigurationForTEntOrderItem = new org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration("INLINE", propertiesForTEntOrderItem);
stringAlgorithmConfigurationMap.put("t_ent_order_item_line", algorithmConfigurationForTEntOrderItem);

ShardingTableRuleConfiguration shardingTableRuleConfiguration = new ShardingTableRuleConfiguration("t_ent_order_item", "ds_${0..3}.t_ent_order_item_${[0, 1, 2, 3, 4, 5, 6, 7]}");
shardingTableRuleConfiguration.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "snowflake"));
shardingTableRuleConfiguration.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_ent_order_item_line"));

from shardingsphere.

fuzuchang avatar fuzuchang commented on August 21, 2024

"t_ent_order_item", "ds_${0..3}.t_ent_order_item_${[0, 1, 2, 3, 4, 5, 6, 7]}",是要分 8个表 , 算法algorithm-expression=t_ent_order_item_${order_id % 8} 必须是 order_id % 8

from shardingsphere.

JianfeiMa avatar JianfeiMa commented on August 21, 2024

我把算法表达式改成propertiesForTEntOrderItem.setProperty("algorithm-expression", "t_ent_order_item_${order_id % 8}");

from shardingsphere.

JianfeiMa avatar JianfeiMa commented on August 21, 2024

还是不行,报同样的错误

from shardingsphere.

JianfeiMa avatar JianfeiMa commented on August 21, 2024

PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_ent_order_item(ent_id, region_code, good_id, good_name, order_id) VALUES (?, ?, ?, ?, ?);");
preparedStatement.setInt(1, 2024);
preparedStatement.setString(2, "GD");
preparedStatement.setString(3, "abc123");
preparedStatement.setString(4, "研究ShardingSphere");
preparedStatement.setInt(5, 16);
int result = preparedStatement.executeUpdate();
System.out.println("打印插入结果->" + result);
我通过这段代码插入一条记录然后就抛出如下异常:
java.sql.SQLException: Please check your sharding conditions 'ShardingConditions(conditions=[ShardingCondition(values=[t_ent_order_item.order_id = 16], startIndex=0)], sqlStatementContext=org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext@600428ad, rule=org.apache.shardingsphere.sharding.rule.ShardingRule@5342032a, subqueryContainsShardingCondition=true)' to avoid same record in table 't_ent_order_item' routing to multiple data nodes.
at org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException.toSQLException(ShardingSphereSQLException.java:76)
at org.apache.shardingsphere.infra.exception.dialect.SQLExceptionTransformEngine.toSQLException(SQLExceptionTransformEngine.java:54)
at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.executeUpdate(ShardingSpherePreparedStatement.java:367)
at com.example.shardingsphere.ShardingsphereApplication.main(ShardingsphereApplication.java:57)

from shardingsphere.

strongduanmu avatar strongduanmu commented on August 21, 2024

Hi @JianfeiMa, can you modify the issue title for search friendly?

from shardingsphere.

JianfeiMa avatar JianfeiMa commented on August 21, 2024

问题原因是:
当分片表规则配置的实际数据节点是这样demo_ds_${0,1}.t_order_${[0, 1]}的时候,最终会拆分成笛卡尔乘积,所以在查询的时候会分别查询两个数据的两个表,但是当插入数据的时候就抛出以上错误,当我改成demo_ds_${0}.t_order_${[0, 1]}再次插入数据的时候就不会抛出异常了;如果要路由多个数据库的时候又该如何配置呢?

from shardingsphere.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.