近日TE网络因版本升级而导致很多BUG,我们会尽快让网站恢复稳定运行,非常抱歉给大家带来不便。
职业课程 > 员工信息系统 7 章、员工信息模块的开发
客服QQ:1206116161
Crow

新建人员信息的时候,乐观锁问题,无法更新人员信息里的居住地址

Crow 于 2018-05-16 13:29:13 发起求助
(34)
(1)
举报

Could not synchronize database state with session - [ERROR] [2018-05-16 13:25:06,432] 

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [edu.wz.ces.model.UniversityBasic#5]

at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1714)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2357)

at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)

at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)

at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)

at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)

at edu.wz.ces.service.impl.UniversityServiceImpl$$EnhancerByCGLIB$$2b9d0d92.saveStudentInfo(<generated>)

at edu.wz.ces.controller.UniversityStudentConroller.saveStudentInfo(UniversityStudentConroller.java:43)

at edu.wz.ces.controller.UniversityStudentConroller$$FastClassByCGLIB$$78010061.invoke(<generated>)

at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

at edu.wz.ces.intercept.WzMethodIntercept.invoke(WzMethodIntercept.java:16)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)

at edu.wz.ces.controller.UniversityStudentConroller$$EnhancerByCGLIB$$37df58fb.saveStudentInfo(<generated>)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)

at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)

at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [edu.wz.ces.model.UniversityBasic] with identifier [5]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [edu.wz.ces.model.UniversityBasic#5]

at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:683)

at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:794)

at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:665)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)

at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)

at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)

at edu.wz.ces.service.impl.UniversityServiceImpl$$EnhancerByCGLIB$$2b9d0d92.saveStudentInfo(<generated>)

at edu.wz.ces.controller.UniversityStudentConroller.saveStudentInfo(UniversityStudentConroller.java:43)

at edu.wz.ces.controller.UniversityStudentConroller$$FastClassByCGLIB$$78010061.invoke(<generated>)

at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

at edu.wz.ces.intercept.WzMethodIntercept.invoke(WzMethodIntercept.java:16)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)

at edu.wz.ces.controller.UniversityStudentConroller$$EnhancerByCGLIB$$37df58fb.saveStudentInfo(<generated>)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)

methodName=edu.wz.ces.exception.ExceptionIntercept@aa6c437|exception - [INFO] [2018-05-16 13:25:06,475] 

arg[0] -> org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [edu.wz.ces.model.UniversityBasic] with identifier [5]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [edu.wz.ces.model.UniversityBasic#5] - [INFO] [2018-05-16 13:25:06,475] 

return: edu.wz.ces.exception.ExceptionIntercept@aa6c437|exception - JSONReturn [head=false, body=服务器异常!] - [INFO] [2018-05-16 13:25:06,475] 

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)

at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)

at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [edu.wz.ces.model.UniversityBasic#5]

at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1714)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2357)

at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)

at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)

at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)

... 59 more


  • Crow
    Crow - 05月16日 回复(0)

    universityBasicDao.save(basic); // 保存学生基本信息

    UniversityDetail detail = conversionStudentDetail(null, dto, basic.getUniId());

    universityDetailDao.save(detail); // 保存学生详细信息

     basic.setUniCensusRegister(analyzeAddress(dto.getRegister(), basic.getUniId(), AddressType.REGISTER, null, true)); // 解析户籍地址

      basic.setUniCurrentAddress(analyzeAddress(dto.getCurrent(), basic.getUniId(), AddressType.CURRENT, null, true)); // 解析现居住地址


  • Crow
    Crow - 05月16日 回复(0)

    这个过程是先保存学生信息才能得到学生ID,然后有了学生ID才能插入地址信息到地址表,然后才能得到地址ID,得到地址ID才能更新学生信息中的地址为得到的地址ID

  • _Kiro
    _Kiro - 05月17日 回复(0)

    你是不是手动设置了乐观锁的值

    _Kiro
    Crow - 05月17日 回复(0) @ _Kiro

    并没有手动设置乐观锁,尝试了几个测试,确认是最后提交事务的时候出错的。因为在返回JSON之前,我从数据库查询了对应ID的值,是已经插入到数据库了,出错之后我查看了数据库的ID自增变化,是有+1,但是并没有相应数据,看上去就是回滚了

    _Kiro
    Crow - 05月17日 回复(0) @ _Kiro

    从打印的对象的toString的信息对比,在save方法被调用以后,乐观锁的值没有变过,不知道为什么就是会有回滚情况发生。我是尽量仿照原项目代码做的,这段代码基本上和源代码一样,原项目在我机器上跑得没有问题

  • _Kiro
    _Kiro - 05月20日 回复(0)

    你打个断点看看具体报错在那一行,在这一行上面找问题,save方法应该不会存在问题吧,

    你再把你建表的脚本贴出来看看

  • _Kiro
    _Kiro - 05月20日 回复(0)

    你生成实体对象是用的我视频中演示的那种方式吗,再把实体对象贴出来看看

  • Crow
    Crow - 前天 11:35 回复(0)

    这个save()确实没问题,就是save以后再去修改这个对象的值,然后return的时候提交事务就会报这个错。目前已经用了别的方法解决了问题,就是再去DAO写一个update方法,然后save以后调用这个方法

  • Crow
    Crow - 前天 11:35 回复(0)

    不明白的是,原项目能用,我自己的项目不能用

  • _Kiro
    _Kiro - 12小时前 回复(0)

    你在控制台上打印save之后的对象,看看乐观锁里面是不是NULL,如果是NULL那就看看你实体类里面的乐观锁注解有没有写错

扫码关注TE官方微博 扫码关注TE官方微博
回复X
举报X