Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Getting OptimisticLockException in multithreaded environment

Getting an OptimisticLockException is kind of the whole point of locking. 
This exception means that to users or threads tried to update the same
object at the same time, the exception is preventing one thread clobbering
the others changes.  Normally you must return the error to the user, and
have them redo their changes.

If you do not care about one thread clobbering anothers changes, then don't
use locking.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Locking
in paticular,
http://en.wikibooks.org/wiki/Java_Persistence/Locking#Handling_optimistic_lock_exceptions

and,

http://wiki.eclipse.org/Introduction_to_EclipseLink_Application_Development_(ELUG)#Locking


Sampathi Peruka wrote:
> 
> Hi,
> 
> While trying to update the object in multi threaded environment, we are 
> getting following exceptions.
> 
> 1.  cannot be updated because it has changed or been deleted since it was
> last read.Class> x.y.domain.order.OrderItem Primary Key> [1091718]
>     at
> org.eclipse.persistence.exceptions.OptimisticLockException.objectChangedSinceLastReadWhenUpdating(OptimisticLockException.java:137)
>     at
> org.eclipse.persistence.descriptors.VersionLockingPolicy.validateUpdate(VersionLockingPolicy.java:766)
>     at
> org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(DatabaseQueryMechanism.java:1148)
>     at
> org.eclipse.persistence.queries.UpdateObjectQuery.executeCommitWithChangeSet(UpdateObjectQuery.java:84)
>     at
> org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:290)
>     at
> org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
>     at
> org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:666)
>     at
> org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:585)
>     at
> org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:114)
>     at
> org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:86)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2588)
>     at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1178)
>     at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1162)
>     at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1108)
>     at
> org.eclipse.persistence.internal.sessions.CommitManager.commitChangedObjectsForClassWithChangeSet(CommitManager.java:297)
>     at
> org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:178)
>     at
> org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3129)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1242)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1331)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitRootUnitOfWork(UnitOfWorkImpl.java:1173)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commit(UnitOfWorkImpl.java:951)
> 
> 2.  cannot be merged because it has changed or been deleted since it was
> last read. {3}Class> x.domain.order.OrderItem
>     at
> org.eclipse.persistence.exceptions.OptimisticLockException.objectChangedSinceLastMerge(OptimisticLockException.java:145)
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:543)
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:249)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeClone(UnitOfWorkImpl.java:3075)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeClone(UnitOfWorkImpl.java:3050)
> 
> The following logic is used to update the object.
> 
> Code creating the above exception
> 
> private void update(Object obj, int depth) throws DataWriteException,
> OptimisticLockException
>     {
>         AbcSynchronizationListener csl = new AbcSynchronizationListener();
>         Transaction trans = null;
>        
>        
>         ( (DomainObject)obj).setLastModUser( this.getCallerName() );
>         ( (DomainObject)obj).setLastModDate(new DateTime());
>        
>         UnitOfWork unitOfWork = (UnitOfWork) getSession();
>         Object clone = null;
>         try
>         {
>             clone = unitOfWork.readObject(obj);
>            
>             switch(depth)
>             {
>             case DEPTH_MEDIUM:
>        
>                 unitOfWork.mergeClone(obj);
>                 break;
>             case DEPTH_DEEP:
>        
>                 unitOfWork.deepMergeClone(obj);
>                 break;
>             default:
>                 throw new IllegalArgumentException("depth " + depth + " is
> not supported.");
>             }
>            
>            
>             csl.addObject(obj);
>            
>             unitOfWork.commit();
>        
>             trans = tm.getTransaction();
>             if ( trans != null )
>             {
>                 trans.registerSynchronization(csl);
>             }
>         }
>         catch ( DatabaseException dexc )
>         {
>             throw new DataWriteException(dexc,"DatabaseException while
> updating object in DAS", x.service.da.DataAccess.class);
>         }
>         catch ( org.eclipse.persistence.exceptions.OptimisticLockException
> oexc )
>         {
>             throw new
> OptimisticLockException(oexc,"OptimisticLockException while updating
> object in DAS", x.service.da.DataAccess.class);
>         }
>         catch(Exception e)
>         {
>             throw new DataWriteException(e,"Error in update method :
> ",this.getClass());
>         }
>     }
> 
> Could you let us know how to resolve this issue ?.
> 
> FYI : We disabled the eclipselink cache.
> Thanks,
> Sampathi Peruka.
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Getting-OptimisticLockException-in-multithreaded-environment-tp21683695p21685083.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top