当前使用版本(必填,否则不予处理)
mybatisplus 3.3.0
该问题是如何引起的?(确定最新版也有问题再提!!!)
5个实例,使用IdType.ASSIGN_ID插入数据,会出现主键冲突
重现步骤(如果有就写完整)
实体类里面主键使用注解 @TableId(type = IdType.ASSIGN_ID)
使用Docker swarm部署XXL-JOB任务执行程序,replicas设置为5,即跑5个实例,不过只有1个机器,所以其实都是跑在1台机上
调度任务启动,5个实例,每个实例跑500条数据,插入日志,然后就报错了
操作的代码:
// 插入合并日志
MergedHistoryLog log = new MergedHistoryLog();
log.setTableName("CORP_BASE_INFO");
log.setPkMerging(saveSid);
log.setPkMerged(CollUtil.join(otherSids, ","));
log.setCreated(DateTime.now());
mergedHistoryLogService.save(log);
报错信息
`org.springframework.dao.DuplicateKeyException:
Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique const
The error may exist in cn/com/it2000/executor/gateway/dao/MergedHistoryLogDao.java (best guess)
The error may involve cn.com.it2000.executor.gateway.dao.MergedHistoryLogDao.insert-Inline
The error occurred while setting parameters
SQL: INSERT INTO MERGED_HISTORY_LOG ( sid, table_name, pk_merging, pk_merged, created ) VALUES ( ?, ?, ?
Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (DC_PRO.PK_MERGED_HI
; ORA-00001: unique constraint (DC_PRO.PK_MERGED_HISTORY_LOG) violated ; nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (DC_PRO.P
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLException
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExc
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.ja
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
at com.sun.proxy.$Proxy72.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:271)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:60)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
at com.sun.proxy.$Proxy87.insert(Unknown Source)
at sun.reflect.GeneratedMethodAccessor306.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.`
Comment From: lzb6666
3077 参考这个
Comment From: tyronego
mybatisplus如何手动指定long workerId, long dataCenterId两个呢?我在配置文件增加随机数是否可以避免呢 @lzb6666
Comment From: lzb6666
注入这个 DefaultIdentifierGenerator bean的时候设置上面两个值