Row is updated or deleted by another transaction (or Unsaved-value mapping is incorrect)

Source: Internet
Author: User
Tags aop postgres database


I have a Java project that runs on a webserver. I always hit this exception.


I read some documentation, and found that pessimistic locking (or optimistic, but I read that pessimistic are better) is th e best way to prevent this exception.


But I couldn ' t find no clear example that explains it.


The My method is like:


@Transactional
public void test (email email, String Subject) {
Getemailbyid (String ID);
Email.setsubject (Subject);
Updateemail (email);
}
While


Email is a hibernate class (It'll be a table in the database)
Getemailbyid (String ID) is a function "that returns" email (this method isn't annotated with @Transctional)
Updateemail (email): is a how that updates the email.

Note:i use hibernate for Save, Update & As On (example:session.getcurrentSession.save (email))



05-10 18:18:38,120-error-[abstractflushingeventlistener]: Could not synchronize database
Org.hibernate.StaleObjectStateException:Row is updated or deleted by another transaction (or Unsaved-value mapping is I Ncorrect): [Com.achievo.ipo.domain.irf.reimbursement#bc034db9-975d-4460-b779-670ea1b48fa5]
At Org.hibernate.persister.entity.AbstractEntityPersister.check (abstractentitypersister.java:1932)
At Org.hibernate.persister.entity.AbstractEntityPersister.update (abstractentitypersister.java:2576)
At Org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert (abstractentitypersister.java:2476)
At Org.hibernate.persister.entity.AbstractEntityPersister.update (abstractentitypersister.java:2803)
At Org.hibernate.action.EntityUpdateAction.execute (entityupdateaction.java:113)
At Org.hibernate.engine.ActionQueue.execute (actionqueue.java:273)
At Org.hibernate.engine.ActionQueue.executeActions (actionqueue.java:265)
At Org.hibernate.engine.ActionQueue.executeActions (actionqueue.java:185)
At Org.hibernate.event.def.AbstractFlushingEventListener.performExecutions (Abstractflushingeventlistener.java : 321)
At Org.hibernate.event.def.DefaultFlushEventListener.onFlush (defaultflusheventlistener.java:51)
At Org.hibernate.impl.SessionImpl.flush (sessionimpl.java:1216)
At Org.hibernate.impl.SessionImpl.managedFlush (sessionimpl.java:383)
At Org.hibernate.transaction.JDBCTransaction.commit (jdbctransaction.java:133)
At Org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit (hibernatetransactionmanager.java:658 )
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:633)
At com.achievo.ipo.service.impl.batchserviceimpl$ $EnhancerBySpringCGLIB $$6e4ad588.sendletter (<generated>)
At Com.achievo.ipo.service.impl.SendLetterTask.sendLetter (sendlettertask.java:677)
At Com.achievo.ipo.service.batch.ZurichBatchLetterCreator.sendLetter (zurichbatchlettercreator.java:86)
At Com.achievo.ipo.service.impl.BatchServiceImpl.noTransSendPaymentLetter (batchserviceimpl.java:133)
At com.achievo.ipo.service.impl.batchserviceimpl$ $FastClassBySpringCGLIB $$3549dc4a.invoke (<generated>)
At Org.springframework.cglib.proxy.MethodProxy.invoke (methodproxy.java:204)
At Org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint (CglibAopProxy.java:700 )
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:150)
At Org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation ( TRANSACTIONINTERCEPTOR.JAVA:96)
At Org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction ( transactionaspectsupport.java:260)
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:633)
At com.achievo.ipo.service.impl.batchserviceimpl$ $EnhancerBySpringCGLIB $$6e4ad588.notranssendpaymentletter (< generated>)
At Com.achievo.ipo.interfaces.web.controller.invoice.PaymentController.payment_letter_send ( paymentcontroller.java:635)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:498)
At Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod ( handlermethodinvoker.java:176)
At Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod ( annotationmethodhandleradapter.java:440)
At Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle ( annotationmethodhandleradapter.java:428)
At Org.springframework.web.servlet.DispatcherServlet.doDispatch (dispatcherservlet.java:933)
At Org.springframework.web.servlet.DispatcherServlet.doService (dispatcherservlet.java:867)
At Org.springframework.web.servlet.FrameworkServlet.processRequest (frameworkservlet.java:951)
At Org.springframework.web.servlet.FrameworkServlet.doPost (frameworkservlet.java:853)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:643)
At Org.springframework.web.servlet.FrameworkServlet.service (frameworkservlet.java:827)
At Javax.servlet.http.HttpServlet.service (httpservlet.java:723)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:290)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal ( opensessioninviewfilter.java:230)
At Org.springframework.web.filter.OncePerRequestFilter.doFilter (onceperrequestfilter.java:106)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:235)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent (sitemeshfilter.java:129)
At Com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter (sitemeshfilter.java:77)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:235)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:330)
At Org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter (Switchuserfilter.java : 181)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke ( filtersecurityinterceptor.java:118)
At Org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter ( filtersecurityinterceptor.java:84)
At Com.achievo.ipo.interfaces.web.security.CustomFilterSecurityInterceptor.doFilter ( CUSTOMFILTERSECURITYINTERCEPTOR.JAVA:17)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.access.ExceptionTranslationFilter.doFilter (Exceptiontranslationfilter.java : 113)
At Com.achievo.ipo.interfaces.web.security.CustomExceptionTranslationFilter.doFilter ( CUSTOMEXCEPTIONTRANSLATIONFILTER.JAVA:25)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.session.SessionManagementFilter.doFilter (sessionmanagementfilter.java:103)
At Com.achievo.ipo.interfaces.web.security.CustomSessionManagementFilter.doFilter ( CUSTOMSESSIONMANAGEMENTFILTER.JAVA:25)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.session.ConcurrentSessionFilter.doFilter (concurrentsessionfilter.java:125)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Com.achievo.ipo.interfaces.web.security.sso.SSOAuthenticationFilter.doFilter (Ssoauthenticationfilter.java : 103)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter ( securitycontextholderawarerequestfilter.java:154)
At Com.achievo.ipo.interfaces.web.security.CustomSecurityContextHolderAwareRequestFilter.doFilter ( CUSTOMSECURITYCONTEXTHOLDERAWAREREQUESTFILTER.JAVA:17)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter ( abstractauthenticationprocessingfilter.java:199)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter ( BASICAUTHENTICATIONFILTER.JAVA:150)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.authentication.logout.LogoutFilter.doFilter (logoutfilter.java:110)
At Com.achievo.ipo.interfaces.web.security.CustomLogoutFilter.doFilter (customlogoutfilter.java:26)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter ( securitycontextpersistencefilter.java:87)
At Com.achievo.ipo.interfaces.web.security.CustomSecurityContextPersistenceFilter.doFilter ( CUSTOMSECURITYCONTEXTPERSISTENCEFILTER.JAVA:18)
At Org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter (FilterChainProxy.java:342)
At Org.springframework.security.web.FilterChainProxy.doFilterInternal (filterchainproxy.java:192)
At Org.springframework.security.web.FilterChainProxy.doFilter (filterchainproxy.java:160)
At Org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate (delegatingfilterproxy.java:343)
At Org.springframework.web.filter.DelegatingFilterProxy.doFilter (delegatingfilterproxy.java:260)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:235)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Com.achievo.ipo.interfaces.web.filter.LoginFilter.doFilter (loginfilter.java:39)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:235)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (characterencodingfilter.java:88)
At Org.springframework.web.filter.OncePerRequestFilter.doFilter (onceperrequestfilter.java:106)
At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:235)
At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:206)
At Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:233)
At Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:191)
At Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:127)
At Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:103)
At Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:109)
At Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:293)
At Org.apache.coyote.ajp.AjpAprProcessor.process (ajpaprprocessor.java:455)
At Org.apache.coyote.ajp.ajpaprprotocol$ajpconnectionhandler.process (ajpaprprotocol.java:398)
At Org.apache.tomcat.util.net.aprendpoint$worker.run (aprendpoint.java:1777)
At Java.lang.Thread.run (thread.java:745)





Pessimistic locking is generally not recommended and it's very costly in terms to performance on database side. The problem that you have mentioned (the code part) a few things are not clear such as:


If your The code is being accessed by multiple threads as the same time.
How are do you creating session object (not sure if to you are using Spring)?
Hibernate session objects are is not thread-safe. So if there are multiple threads accessing the same sessions and trying to update the same database entity, your code can p Otentially end With an error situation like this.


So what happens this is this than one thread tries to update the same entity, one thread succeeds and the next T Hread goes to commit the data, it sees it already been modified and ends up throwing.


EDIT:


There is a way to use pessimistic locking in Hibernate. Check out this link. But There seems to is some issue with this mechanism. I came across posting a bug in Hibernate (HHH-5275), however. The scenario mentioned in the bug is as follows:


Two threads are reading the same database record; One of those threads should use pessimistic locking thereby blocking the other thread. But Both threads can read the database record causing the test to fail.
This is very close to what for you are facing. "Try this if" does not work, the ' Only way I can ' to ' is ' using Native SQL queries where you can achieve Pessi Mistic locking in Postgres database and SELECT for UPDATE query.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.