Transaction conflict after invoke flush method on entityManager

Hi

In my case I need invoke flush method on entityManager before accept transaction. When I invoke this method I get error like below. When I removeentityManager.flush(); transaction is accept normally. I update entity Documents once on the end of process. Where can I search solving this problem ?

Caused by: org.eclipse.persistence.exceptions.OptimisticLockException: 
Exception Description: The object [com.company.erp.entity.documents.Documents-27005 [detached]] cannot be merged because it has changed or been deleted since it was last read. 
Class> com.company.erp.entity.documents.Documents
	at org.eclipse.persistence.exceptions.OptimisticLockException.objectChangedSinceLastMerge(OptimisticLockException.java:152) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:578) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3569) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:387) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3529) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:553) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]

Hi any suggestion. Does flush method accept transaction ?

Hi Andrzej,

The flush() method works in the same transaction, it just saves all entities changed currently in memory for this transaction to the database by executing SQL updates.

OptimisticLockException usually occurs when an SQL update for an entity does not hit the record because the record has a different version value than the entity instance in memory. Usually it is caused by some business logic of your application.