DefaultKeyGenerator,the return id not equals to the insert id
Sharding-JDBC 3.0.0.M2 + MyBatis 3.3 Use DefaultKeyGenerator,mybatis returned object id not equals the id which insert to table. sharding-key: create_time
Hi @sunjiabo, please describe your doubt following the issue template.
mybatis
<insert id="insert" parameterType="com.hexin.msg.model.MessageNew" keyProperty="id"
keyColumn="id" useGeneratedKeys="true">
insert into message_new (mq_id, ....
send_time, consume_time, create_time)
values (#{mqId,jdbcType=VARCHAR}, ...
#{sendTime,jdbcType=TIMESTAMP}, #{consumeTime,jdbcType=INTEGER},
#{createTime,jdbcType=TIMESTAMP},
#{hostip,jdbcType=VARCHAR})
</insert>
<sharding:table-rule logic-table="message_body" actual-data-nodes="hexinMsgDataSource.message_body_2018$->{['01','02','03','04','05','06','07','08','09','10','11','12']}" table-strategy-ref="tableShardingStrategy" generate-key-column-name="id"/>
mysql insert actual id is 237162384471687168 but, mybatis return MessageNew.id is 237162384471687170
I use the my custom ID generator algorithm is correct, for example timestamp. but, use snowflake the return id is incorrect.
I doubt there's more than one row inserted. What's your algorithmExpression? You'd better select * from message_new where id=237162384471687170 to find if the row exists.
I inserted only one row of data. My algorithmExpression is only take the current timestamp, the retured MessageNew.id is equals to the id in mysql table.
If use DefaultKeyGenerator or IPKeyGenerator,it`s incorrect ! The id in mysql table is 237162384471687168, but mybatis returned MessageNew.id is 237162384471687170
select * from message_new_201808 where id = 237162384471687170 It`s empty.
select * from message_new_201808 where id= 237162384471687168 can return the insert row.
Where did I use it wrong?
Any way, show me your config.yaml.
<sharding:standard-strategy id="tableShardingStrategy" sharding-column="send_time"
precise-algorithm-ref="preciseTableShardingAlgorithm" range-algorithm-ref="rangeTableShardingAlgorithm"/>
<sharding:data-source id="shardingDataSource">
<sharding:sharding-rule data-source-names="hexinMsgDataSource" default-data-source-name="hexinMsgDataSource">
<sharding:table-rules>
<sharding:table-rule logic-table="message_new"
actual-data-nodes="hexinMsgDataSource.message_new_2018$->{['01','02','03','04','05','06','07','08','09','10','11','12']}"
table-strategy-ref="tableShardingStrategy" generate-key-column-name="id"/>
<sharding:table-rule logic-table="message_body"
actual-data-nodes="hexinMsgDataSource.message_body_2018$->{['01','02','03','04','05','06','07','08','09','10','11','12']}"
table-strategy-ref="tableShardingStrategy" generate-key-column-name="id"/>
</sharding:table-rules>
<sharding:binding-table-rules>
<sharding:binding-table-rule logic-tables="message_new,message_body"/>
</sharding:binding-table-rules>
</sharding:sharding-rule>
<sharding:props>
<prop key="sql.show">true</prop>
</sharding:props>
</sharding:data-source>
Please try select * from message_new where id=237162384471687170 in all tables.
select * from message_new where id=237162384471687170 empty in other tables,only insert to message_new_201808.
Is there any problem with your test?
Never. Set sql.show: true and put the log here.
I doubt id 237162384471687170 has beed deleted. Insert a row manually direct to message_new_201808 and check what is the next id.
I discovered the cause of the problem. This is a bug of Jsckson. Cannot convert long value correct,if the value is logger than 16 digit. Thank you for your answer !