mysql高并发下主键冲突
事情是這樣的,博主剛剛寫(xiě)完一個(gè)小項(xiàng)目,由于使用的量比較大,所以做了一下壓測(cè),在插入數(shù)據(jù)的時(shí)候報(bào)出了如下錯(cuò)誤 :
[http-bio-****-exec-67] DEBUG c.j.m.a.d.A.insertAppUser!selectKey - ==> Parameters:?
org.springframework.dao.DuplicateKeyException:?
### Error updating database.? Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '236533' for key 'PRIMARY'
很顯然高并發(fā)下主鍵沖突了,開(kāi)始漫長(zhǎng)的找尋原因。最終發(fā)現(xiàn)
insert id="insertCustomerLog" parameterType="java.util.Map" useGeneratedKeys="true">
<!-- <selectKey resultType="java.lang.Long" keyProperty="dto.id" order="BEFORE">
? ? ? ? ? ? select auto_increment from information_schema.tables where table_name='***' and table_schema=schema();
</selectKey> -->
insert into ****
主鍵是查詢(xún)后在插入的時(shí)候是先查詢(xún)的。這樣高并發(fā)下就有了。間隙。
最終解決??useGeneratedKeys="true"? ?keyProperty="id"? ?加上這個(gè)? 去掉
<!-- <selectKey resultType="java.lang.Long" keyProperty="dto.id" order="BEFORE">
? ? ? ? ? ? select auto_increment from information_schema.tables where table_name='***' and table_schema=schema();
</selectKey> -->
?雖然都在一個(gè)事物里。都是還會(huì)發(fā)生查詢(xún)間隙這個(gè)問(wèn)題 并發(fā)200
總結(jié)
以上是生活随笔為你收集整理的mysql高并发下主键冲突的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 东北大米为什么好?
- 下一篇: 海南热带海洋学院食堂有几个,食物怎么样?