Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA optimistic lock problem  RSS feed

 
Manish mnnit
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Following is the code i wrote for updating into database using JPA.

package com.iflex.locking;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.LockModeType;
import javax.persistence.Query;

import com.iflex.jpa.Liability;

public class TestLock {

private EntityManager em = null;
private EntityManagerFactory emf = null;

public static void main(String[] args){
TestLock tLock = new TestLock();
tLock.updateLiab();
}

public void updateLiab(){
em = JPAEntityManager.getInstance().getEM();
Liability liab = null ;
Class c;
try {

em.getTransaction().begin();
Query query = em.createNamedQuery ("Liability.findAByLiabid");
query.setParameter ("liabId", "gmsliab");
List results = query.getResultList ();
if(results != null && results.size() > 0)
liab = (Liability)results.get(0);
System.out.println(liab.getLiabNo() + " "+ liab.getLiabName()+" "+liab.getLiabCcy()+" "
+liab.getVersion());

liab.setLiabCcy("ABP");
em.merge(liab);
em.getTransaction().commit();

}
catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}


When i am trying to run above code following exception is coming ---
javax.persistence.RollbackException: javax.persistence.OptimisticLockException: Exception [TOPLINK-5006] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.OptimisticLockException
Exception Description: The object [com.iflex.jpa.Liability@49cf9f] cannot be updated because it has changed or been deleted since it was last read.
Class> com.iflex.jpa.Liability Primary Key> [713, 0]
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:109)
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:45)
at com.iflex.locking.TestLock.updateLiab(TestLock.java:45)
at com.iflex.locking.TestLock.main(TestLock.java:19)
Caused by: javax.persistence.OptimisticLockException: Exception [TOPLINK-5006] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.OptimisticLockException
Exception Description: The object [com.iflex.jpa.Liability@49cf9f] cannot be updated because it has changed or been deleted since it was last read.
Class> com.iflex.jpa.Liability Primary Key> [713, 0]
at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:359)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1112)
at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:82)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:842)
at oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:90)
... 3 more
Caused by: Exception [TOPLINK-5006] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.OptimisticLockException
Exception Description: The object [com.iflex.jpa.Liability@49cf9f] cannot be updated because it has changed or been deleted since it was last read.
Class> com.iflex.jpa.Liability Primary Key> [713, 0]
at oracle.toplink.essentials.exceptions.OptimisticLockException.objectChangedSinceLastReadWhenUpdating(OptimisticLockException.java:136)
at oracle.toplink.essentials.descriptors.VersionLockingPolicy.validateUpdate(VersionLockingPolicy.java:711)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(DatabaseQueryMechanism.java:1172)
at oracle.toplink.essentials.queryframework.UpdateObjectQuery.executeCommitWithChangeSet(UpdateObjectQuery.java:76)
at oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:283)
at oracle.toplink.essentials.queryframework.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:67)
at oracle.toplink.essentials.queryframework.DatabaseQuery.execute(DatabaseQuery.java:609)
at oracle.toplink.essentials.queryframework.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:536)
at oracle.toplink.essentials.queryframework.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:123)
at oracle.toplink.essentials.queryframework.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:95)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2218)
at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:937)
at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:894)
at oracle.toplink.essentials.internal.sessions.CommitManager.commitChangedObjectsForClassWithChangeSet(CommitManager.java:294)
at oracle.toplink.essentials.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:180)
at oracle.toplink.essentials.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:2638)
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1030)
at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:357)
... 7 more

Can somebody help me on this ???

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!