当前使用版本 java8 + spring-boot:2.7.18 + mybatis-plus:3.5.7
当前环境信息 例如: Java8 + Mysql8.0
描述bug现象
提供问题复现步骤 使用默认自动生成 ID 的方式,生产环境报主键冲突错误,初步考虑是不是因为在并发环境下产生的,本地没出现
提供完整堆栈日志(可选)
提供问题复现工程(可选) 请尽量提供复现工程,减少大家排错的时间.
Comment From: miemieYaho
https://github.com/baomidou/mybatis-plus/issues/6055
Comment From: phial3
mybatis-plus配置如下:
mybatis-plus:
mapper-locations: "classpath*:/mapper/*.xml"
check-config-location: true
global-config:
enable-sql-runner: true
db-config:
id-type: assign_id
logic-delete-field: "deleted"
logic-delete-value: true
logic-not-delete-value: false
insert-strategy: always
update-strategy: not_null
where-strategy: not_null
sequence:
worker-id: ${WORKER_ID:${random.int(31)}}
datacenter-id: ${DATACENTER_ID:${random.int(31)}}
preferred-networks: "${PREFERRED_NETWORKS:127.0.0.1}"
ignored-interfaces: "${IGNORED_INTERFACES:lo,docker0}"
已经配置了workId和datacenterId,可以看到生效了,并且只有一个实例,为什么今天还是发现有主键重复问题
Comment From: phial3
mybatis-plus配置如下:
yaml mybatis-plus: mapper-locations: "classpath*:/mapper/*.xml" check-config-location: true global-config: enable-sql-runner: true db-config: id-type: assign_id logic-delete-field: "deleted" logic-delete-value: true logic-not-delete-value: false insert-strategy: always update-strategy: not_null where-strategy: not_null sequence: worker-id: ${WORKER_ID:${random.int(31)}} datacenter-id: ${DATACENTER_ID:${random.int(31)}} preferred-networks: "${PREFERRED_NETWORKS:127.0.0.1}" ignored-interfaces: "${IGNORED_INTERFACES:lo,docker0}"已经配置了workId和datacenterId,可以看到生效了,并且只有一个实例,为什么今天还是发现有主键重复问题
@miemieYaho @akuma @asdfgh @clevertension 异常日志如下:
### SQL: INSERT INTO t_process_history ( id, xxxxxxxxx ) VALUES ( ?, xxxxxxxxx )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '1811943373620334594' for key 't_process_history.PRIMARY'
; Duplicate entry '1811943373620334594' for key 't_process_history.PRIMARY'; nested exception is java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '1811943373620334594' for key 't_process_history.PRIMARY'
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:244)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:439)
at com.sun.proxy.$Proxy113.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:152)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy274.insert(Unknown Source)
Comment From: miemieYaho
你是不是用的同一个对象循环插入?
Comment From: phial3
你是不是用的同一个对象循环插入?
不会是同一个对象,插入是按照事件机制,每次产生一个事件都会重新 new 一个对象
Comment From: miemieYaho
提供复现工程
Comment From: userlillk
老哥解决了吗,今天我也出现同样问题。上线了比较长的时间,第一次出现,暂时还无法复现
Comment From: JasonWeng9
同样出现这样的问题, 单机情况下默认雪花算法出现重复id, 不是这里的问题, https://github.com/baomidou/mybatis-plus/issues/6055
duplicate key 'PRIMARY'; nested exception is java.sql.BatchUpdateException: 数据库已经设置自增, 一个batch插入500条记录