shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

DefaultKeyGenerator,the return id not equals to the insert id

Open sunjiabo opened this issue 8 years ago • 12 comments

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

sunjiabo avatar Aug 16 '18 13:08 sunjiabo

Hi @sunjiabo, please describe your doubt following the issue template.

tuohai666 avatar Aug 17 '18 02:08 tuohai666

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

sunjiabo avatar Aug 17 '18 02:08 sunjiabo

I use the my custom ID generator algorithm is correct, for example timestamp. but, use snowflake the return id is incorrect.

sunjiabo avatar Aug 17 '18 02:08 sunjiabo

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.

tuohai666 avatar Aug 17 '18 03:08 tuohai666

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?

sunjiabo avatar Aug 17 '18 04:08 sunjiabo

Any way, show me your config.yaml.

tuohai666 avatar Aug 17 '18 04:08 tuohai666

<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>

sunjiabo avatar Aug 17 '18 04:08 sunjiabo

Please try select * from message_new where id=237162384471687170 in all tables.

tuohai666 avatar Aug 17 '18 04:08 tuohai666

select * from message_new where id=237162384471687170 empty in other tables,only insert to message_new_201808.

Is there any problem with your test?

sunjiabo avatar Aug 17 '18 05:08 sunjiabo

Never. Set sql.show: true and put the log here.

tuohai666 avatar Aug 17 '18 06:08 tuohai666

I doubt id 237162384471687170 has beed deleted. Insert a row manually direct to message_new_201808 and check what is the next id.

tuohai666 avatar Aug 17 '18 06:08 tuohai666

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 !

sunjiabo avatar Aug 17 '18 07:08 sunjiabo