Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Recovering from optimistic lock exception

Unfortunately it's a JPA spec. requirement:
3.1.1 EntityManager Interface
states that:

"Runtime exceptions thrown by the methods of the EntityManager interface other than the Lock-TimeoutException will cause the current transaction to be marked for rollback."

On 8/1/2013 11:03 AM, Dennis Fuglsang wrote:
I would like to automatically handle optimistic lock exceptions through
user defined merge policies however while testing my logic in a simple
test  case I encounter an exception when committing the transaction.
If a merge fails are you not able to attempt to merge again in the same
transaction?

_javax.persistence.RollbackException_: Transaction rolled back because
transaction was set to RollbackOnly.

             at
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(_EntityTransactionImpl.java:92_)

             at
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(_EntityTransactionImpl.java:63_)

             EntityManager em = /createEntityManager/();

*try*{

                 em.getTransaction().begin();

                 mergeInstance(em, entity_b);

                 em.getTransaction().commit();

             } *catch*(Exception p) {

                 Assert./fail/(p.getMessage());

             } *finally*{

*if*(em.isOpen()) {

                     em.close();

                 }

             }

*private**void*mergeInstance(EntityManager em, InstanceImpl entity) {

*int*retryCount = 2;

*int*cnt = 0;

         InstanceImpl localEntity = entity;

*while*(cnt < retryCount) {

*try*{

                 localEntity = em.merge(localEntity);

*return*;

             } *catch*(OptimisticLockException e) {

                 cnt++;

                 System./out/.println("\n>>> Optimistic Lock Exception
encountered, retry number "+cnt);

                 String sql = "SELECT e FROM InstanceImpl e WHERE e.uuid
IN ('"//$NON-NLS-1$

                     + localEntity.getUUID().toString() + "')";
//$NON-NLS-1$

                 Query query = em.createQuery(sql);

                 query.setHint(QueryHints./CACHE_USAGE/,
CacheUsage./DoNotCheckCache/);

// Merge local data with latest repository data

                 InstanceImpl repoEntity =
(InstanceImpl)query.getSingleResult();

                 localEntity.setOptLock(repoEntity.getOptLock());

             }

         }

     }

--
Oracle <http://www.oracle.com/>
Dennis Fuglsang | Consulting Member Technical Staff
Phone: +1 314 682 0497 <tel:+1%20314%20682%200497> | Mobile: +1 314 607
8087 <tel:+1%20314%20607%208087>
OracleOracle SOA Governance - Enterprise Repository
424 South Woods Mill Road | Chesterfield, MO 63017

Green Oracle <http://www.oracle.com/commitment>

	

Oracle is committed to developing practices and products that help
protect the environment



_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top