执行简单的insert操作INSERT statement must contains sharding value 异常

cavlinwang 发布于 2018/06/28 16:16
阅读 842
收藏 0

你好, 基于spring+mybatis+mysql, 有32个分库, 没有分表; 执行简单的insert操作,报Error updating database.  Cause: com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException: INSERT statement must contains sharding value 异常;

 

以下是异常信息:

16:06:07.290 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
16:06:07.295 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@25c1f5ee] was not registered for synchronization because synchronization is not active
16:06:07.326 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
16:06:07.326 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection@5fb8dc01] will not be managed by Spring
16:06:07.358 [main] DEBUG c.v.a.u.d.A.insertSelective - ==>  Preparing: insert into adp_order ( order_sn ) values ( ? ) 
16:06:07.358 [main] DEBUG c.v.a.u.d.A.insertSelective - ==> Parameters: 111111111113(String)
16:06:07.358 [main] DEBUG c.d.d.r.s.parser.SQLParserFactory - Logic SQL: insert into adp_order
     ( order_sn ) 
     values ( ? )
16:06:07.441 [main] DEBUG c.d.d.r.s.parser.SQLParseEngine - Parsed SQL result: SQLParsedResult(routeContext=RouteContext(tables=[Table(name=adp_order, alias=Optional.absent())], sqlStatementType=null, sqlBuilder=null), conditionContexts=[ConditionContext(conditions={})], mergeContext=MergeContext(orderByColumns=[], groupByColumns=[], aggregationColumns=[], limit=null, executorEngine=null))
16:06:07.441 [main] DEBUG c.d.d.r.s.parser.SQLParseEngine - Parsed SQL: INSERT INTO [Token(adp_order)] (order_sn) VALUES (?)
16:06:07.441 [main] DEBUG c.d.d.r.s.parser.SQLParserFactory - Logic SQL: insert into adp_order
     ( order_sn ) 
     values ( ? )
16:06:07.441 [main] DEBUG c.d.d.r.s.parser.SQLParseEngine - Parsed SQL result: SQLParsedResult(routeContext=RouteContext(tables=[Table(name=adp_order, alias=Optional.absent())], sqlStatementType=null, sqlBuilder=null), conditionContexts=[ConditionContext(conditions={})], mergeContext=MergeContext(orderByColumns=[], groupByColumns=[], aggregationColumns=[], limit=null, executorEngine=null))
16:06:07.441 [main] DEBUG c.d.d.r.s.parser.SQLParseEngine - Parsed SQL: INSERT INTO [Token(adp_order)] (order_sn) VALUES (?)
16:06:07.457 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@25c1f5ee]
16:06:07.457 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
16:06:07.457 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@141e5bef testClass = AdpOrderServiceImplTest, testInstance = com.vipshop.adp.unionsearch.service.impl.AdpOrderServiceImplTest@3b96c42e, testMethod = testInsertAdpOrderByModel@AdpOrderServiceImplTest, testException = org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException: INSERT statement must contains sharding value
### The error may involve com.vipshop.adp.unionsearch.dao.AdpOrderMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: insert into adp_order      ( order_sn )       values ( ? )
### Cause: com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException: INSERT statement must contains sharding value, mergedContextConfiguration = [MergedContextConfiguration@5f9be66c testClass = AdpOrderServiceImplTest, locations = '{classpath:applicationContext-test.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{test}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
16:06:07.457 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@141e5bef testClass = AdpOrderServiceImplTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5f9be66c testClass = AdpOrderServiceImplTest, locations = '{classpath:applicationContext-test.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{test}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].
16:06:07.457 [Thread-8] INFO  o.s.c.s.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@58be6e8: startup date [Thu Jun 28 16:05:03 CST 2018]; root of context hierarchy
16:06:07.457 [Thread-8] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'sqlSessionFactory'

 

 

加载中
0
c
cavlinwang

跟踪代码发现执行流程如下:

1. SQLParseEngine.parse()方法里面 visitor.setParameters(parameters)语句:

SQLASTOutputVisitor.setParameters()设置了inputParameters=[111111111113], 而parameters为设置值, 即为null;  

2. MySQLInsertVisitor.visit()方法执行addCondition()操作, 获取参数是通过SQLASTOutputVisitor.getParameters(); 但其实获取的是parameters属性,而不是inputParameters属性; 导致不能add任何Condition; 

返回顶部
顶部