Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

MySQL查询报Duplicate entry '' for key 'PRIMARY错, 没错, 查询报的

image.png

日志是一个商机信息跟着一个同步工程信息, 异常中的那个1588162468514不知道是哪里来的, 商机信息和同步工程信息的表里都没有这个值. 报错的地方只有商机信息, 同步工程信息没有打印出来, 这俩日志中间是有一个查询操作的:

image.png

下面是repository中的方法:

image.png

然后是异常信息和代码中的位置:

image.png
image.png

完整的异常:
SQL Error: 1062, SQLState: 23000

2020/11/2 下午4:25:412020-11-02 16:25:41.611 ERROR 1 --- [nio-8221-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry '1588162468514' for key 'PRIMARY'

2020/11/2 下午4:25:412020-11-02 16:25:41.629 ERROR 1 --- [nio-8221-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

2020/11/2 下午4:25:41

2020/11/2 下午4:25:41com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1588162468514' for key 'PRIMARY'

2020/11/2 下午4:25:41    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]

2020/11/2 下午4:25:41    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.2.0.jar!/:na]

2020/11/2 下午4:25:41    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar!/:na]

2020/11/2 下午4:25:41    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3171) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3686) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1440) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.query.internal.NativeQueryImpl.beforeQuery(NativeQueryImpl.java:261) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1503) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1553) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]

2020/11/2 下午4:25:41    at com.faujor.project.service.impl.ProjAssociationServiceImpl.asyncBusiOppoForAsso(ProjAssociationServiceImpl.java:209) ~[classes!/:0.0.1]

2020/11/2 下午4:25:41    at com.faujor.project.service.impl.ProjAssociationServiceImpl$$FastClassBySpringCGLIB$$d32c2c23.invoke(<generated>) ~[classes!/:0.0.1]

2020/11/2 下午4:25:41    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at com.faujor.project.service.impl.ProjAssociationServiceImpl$$EnhancerBySpringCGLIB$$3d4a5acc.asyncBusiOppoForAsso(<generated>) ~[classes!/:0.0.1]

2020/11/2 下午4:25:41    at com.faujor.project.controller.ProjAssociationController.asyncBusiOppoForAsso(ProjAssociationController.java:666) ~[classes!/:0.0.1]

2020/11/2 下午4:25:41    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]

2020/11/2 下午4:25:41    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]

2020/11/2 下午4:25:41    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

2020/11/2 下午4:25:41    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]

2020/11/2 下午4:25:41    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]

2020/11/2 下午4:25:41    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]

2020/11/2 下午4:25:41    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar!/:9.0.13]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

由于给的信息不多,不过依照hibernate的事务处理性质,我给予以下几个点来梳理哈问题,题主可以根据这些点去check一下

* 首先这个报错Duplicate entry '1588162468514' for key 'PRIMARY'是数据库报出来的

image.png
你去给数据库执行sql插入操作,但是已有主键存在,所以sql报错

* 那为啥明明是查询操作,却执行了插入操作呢?

image.png
从堆栈上也可以看到,开始确实是list()操作,但是关键就是出在了这个list()操作之前,需要进行一次flush

* hibernateflush会把当前Session开启的事务中所有登记的操作全部发给数据库执行一遍(但不提交事务,这是与commit的区别),并且按照insert,update...顺序去执行

image.png

所以此时我猜想,你的方法ProjAssociationServiceImpl.asyncBusiOppoForAsso应该是有个事务@Transactional的(就算没有,asyncBusiOppoForAsso再外层的方法估计有,总之肯定被包到一个事务里了)

* 因为有了事务,恰好你的查询方法ProjAssociation findByBusiCode(String busiCode)使用了NativeQueryImpl,而NativeQueryImpl执行beforeQuery时要先判断一下是否需要flush,也就是方法NativeQueryImpl.shouldFlush

image.png

* 因此执行flush的话,ProjAssociationServiceImpl.asyncBusiOppoForAsso中所有在findByBusiCode之前登记的insert都要执行了。那这里面有2条以上sql主键重复问题,当然就会报错,虽然不是你查询的问题,但是恰好这个查询相当于提前执行了一次flush

结合以上的点,那要检查的东西就很简单了,去找找在asyncBusiOppoForAsso内(或外层事务包裹范围内),findByBusiCode之前做过的save()操作,并且着重检查save()时的实体的主键生成策略。

因为没有其他信息,我下面只是举个例子

User user1 = new User();
user1.setName("测试1");
userRepository.save(user1);

User user2 = new User();
user2.setName("测试2");
userRepository.save(user2);

Student student = repository.findByName(name);

虽然最后只是执行一个查询,但是前面有两个Usersave操作,如果User的主键生成策略导致最终user1user2主键都是相同的,那repository.findByName执行时会触发一次flush,那此时之前登记的user1user2insert操作就会一起执行一遍,导致最终报错

以上就是我的看法吧,不过没有复现你的问题,只能说提供一些参考意见,希望有所帮助~那。。。拜了个拜~(* ̄rǒ ̄)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...