I am working on a project and due to certain issue I am changing pessimistic locking to optimistic locking. While doing so i am getting an error while updating the entity in the code as i tried it as a standalone application so there is no chance that two threads are updating it simultaneously. I checked the value of version in the different part of the code. Its showing it as 0. While calling flush or commit its giving an excepion that it is updated to 1.
Note: I already added @version int version_id field in entity for optimisticlocking.
The code is as below:
WorkItem tmp_workItem=entityManager.find(WorkItem.class , workItem.getEntityKey());
logger.info("Before merge"+ tmp_workItem.getVersion());
entityManager.merge(workItem);
tmp_workItem=entityManager.find(WorkItem.class , workItem.getEntityKey());
logger.info("After merge"+tmp_workItem.getVersion()+" "+workItem.getVersion());
//logger.info(entityManager.getLockMode(WorkItem.class).toString());
entityManager.flush();
logger.info("After flush"+tmp_workItem.getVersion());
response = true;
This code is throwing an exception :
getVersion : 1
] cannot be updated because it has changed or been deleted since it was last read.
Class> com.csg.ib.cobra.domain.WorkItem Primary Key> [9553]
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:549)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithPreBuiltChangeSet(UnitOfWorkImpl.java:1559
The Values of version in logger are :
before merge : 0 0 after merge : 0 0 before flush: 0 0
Then how it can get incresed by 1 while calling entityManger.flush()
)