Grails错误:行被另一个事务更新或删除(或未保存的值映射不正确)

时间:2013-11-11 14:15:11

标签: java hibernate spring-mvc grails grails-2.0

我有以下Grails控制器操作:

def index() {
   def facebookUser = FacebookUser.findByUid(facebookUserId, [lock: true])
   facebookContext.user.exchangeToken()    

   FacebookUser.withTransaction {
     facebookUser.lock()
     facebookUser.accessToken = accessToken
     facebookUser.accessTokenExpires = new Date(facebookContext.user.tokenExpirationTime)
     facebookUser.save(flush: true, failOnError: true)
   } // end withTransaction

}

有时我会收到以下错误:

org.hibernate.StaleObjectStateException
Message
Executing action [success] of controller [test.FacebookController] caused exception: Runtime error executing action 
Caused by
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [test.FacebookUser#180]

Caused by HibernateOptimisticLockingFailureException: Object of class [test.FacebookUser] with identifier [180]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [test.FacebookUser#180]
->>  683 | convertHibernateAccessException in org.springframework.orm.hibernate3.SessionFactoryUtils
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    412 | convertHibernateAccessException in org.springframework.orm.hibernate3.HibernateAccessor
|    411 | doExecute . . . . . . . . . . . in org.springframework.orm.hibernate3.HibernateTemplate
|    374 | executeWithNativeSession        in     ''
|    663 | lock . . . . . . . . . . . . .  in     ''
|     -1 | call                            in org.springframework.orm.hibernate3.HibernateOperations$lock
|    864 | lock . . . . . . . . . . . . .  in org.codehaus.groovy.grails.orm.hibernate.HibernateGormInstanceApi
|     -1 | lock                            in test.FacebookUser
|    419 | doCall . . . . . . . . . . . .  in test.FacebookController$_success_closure5
|     -1 | invoke                          in sun.reflect.GeneratedMethodAccessor2580
|     43 | invoke . . . . . . . . . . . .  in sun.reflect.DelegatingMethodAccessorImpl
|    606 | invoke                          in java.lang.reflect.Method
|     90 | invoke . . . . . . . . . . . .  in org.codehaus.groovy.reflection.CachedMethod
|    233 | doMethodInvoke                  in groovy.lang.MetaMethod
|   1085 | invokeMethod . . . . . . . . .  in groovy.lang.MetaClassImpl
|   1110 | invokeMethod                    in groovy.lang.ExpandoMetaClass
|    909 | invokeMethod . . . . . . . . .  in groovy.lang.MetaClassImpl
|    411 | call                            in groovy.lang.Closure
|     51 | invokeCustom . . . . . . . . .  in org.codehaus.groovy.runtime.ConvertedClosure
|     82 | invoke                          in org.codehaus.groovy.runtime.ConversionHandler
|     -1 | doInTransaction . . . . . . . . in com.sun.proxy.$Proxy57
|    130 | execute                         in org.springframework.transaction.support.TransactionTemplate
|     -1 | invoke . . . . . . . . . . . .  in sun.reflect.GeneratedMethodAccessor1546
|     43 | invoke                          in sun.reflect.DelegatingMethodAccessorImpl
|    606 | invoke . . . . . . . . . . . .  in java.lang.reflect.Method
|    189 | invoke                          in org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite
|     53 | call . . . . . . . . . . . . .  in org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite
|    116 | call                            in org.codehaus.groovy.runtime.callsite.AbstractCallSite
|    687 | withTransaction . . . . . . . . in org.grails.datastore.gorm.GormStaticApi
|     -1 | withTransaction                 in test.FacebookUser
|    418 | success . . . . . . . . . . . . in test.FacebookController
...

我知道我必须使用的Error: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)这样的答案你可以使用悲观锁定,这就是我做了两次。

在我的情况下,我该怎么做才能防止出现此错误?

0 个答案:

没有答案