源码解读 Duplicate entry '' for key 'PRIMARY' 坑

在kafka发送消息到另一个service,service执行sql, 首次或前2次执行正常C操作,但后续2次或以后的操作必然发生以下错误:

### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '93' for key 'PRIMARY'
### The error may exist in com/crossoverjie/kafka/orderconsumer/mapper/StockOrderMapper.java (best guess)
### The error may involve com.crossoverjie.kafka.orderconsumer.mapper.StockOrderMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO stock_order  ( id,sid,name ) VALUES( ?,?,? )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '93' for key 'PRIMARY'
; SQL []; Duplicate entry '93' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '93' for key 'PRIMARY'

2018-05-21 16:38:31.273 [consumer-pool-0] TRACE o.s.t.i.TransactionInterceptor - Completing transaction for [com.crossoverjie.kafka.orderconsumer.service.impl.OrderServiceImpl.createOptimisticOrderUseRedisAndKafka] after exception: java.lang.RuntimeException: 

根据错误提示,说已存在主键id=93这条数据,不能再插入了,正常理解是93是已存在了,为啥新插入id自增的怎么没自动累加呢?

源码解读 Duplicate entry '' for key 'PRIMARY' 坑_第1张图片

源码解读 Duplicate entry '' for key 'PRIMARY' 坑_第2张图片


源码解读 Duplicate entry '' for key 'PRIMARY' 坑_第3张图片

我们的pojo设置了主键id注解

源码解读 Duplicate entry '' for key 'PRIMARY' 坑_第4张图片


去掉就好了

源码解读 Duplicate entry '' for key 'PRIMARY' 坑_第5张图片




你可能感兴趣的:(中间件)