Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ConcurrentModificationException prior to updating entity.
ConcurrentModificationException prior to updating entity. [message #506072] Tue, 05 January 2010 20:22 Go to next message
beamso is currently offline beamsoFriend
Messages: 7
Registered: January 2010
Junior Member
Hi.

I'm attempting to update an existing J2EE application from using Toplink on glassfish v2.1.1 to using EclipseLink on glassfish v3. The application is using bean-managed persistence with explicit transactions.

When creating/updating entities, JPA queries may be executed in the transaction prior to EntityManager.persist() or EntityManager.merge() being called on the object being created/updated. This code flow worked perfectly under Toplink. But under EclipseLink I'm getting ConcurrentModificationExceptions.

I've done some googling on the exception and the first point made was making sure that the EntityManager wasn't accessed from multiple threads. The EntityManager is stored in a ThreadLocal and is only accessed on the one thread.

Any ideas on what's wrong here would be great.

Following is the stacktrace:

Caused by: java.util.ConcurrentModificationException
	at java.util.IdentityHashMap$IdentityHashMapIterator.nextIndex(IdentityHashMap.java:715)
	at java.util.IdentityHashMap$KeyIterator.next(IdentityHashMap.java:804)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:606)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.writeChanges(RepeatableWriteUnitOfWork.java:352)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:696)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.performPreQueryFlush(EJBQueryImpl.java:1182)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:404)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:669)
	at net.beamso.project.persistence.controllers.JpaDao.getFirstResultFromQuery(JpaDao.java:95)
Re: ConcurrentModificationException prior to updating entity. [message #506546 is a reply to message #506072] Thu, 07 January 2010 22:17 Go to previous messageGo to next message
beamso is currently offline beamsoFriend
Messages: 7
Registered: January 2010
Junior Member
I don't mean to reply to myself but I worked it partially out.

My JPA entities are annotated with javax.validation annotations, and I had code to perform validation, both on the entity as a standalone object and to compare it with data already stored in the database.

JPA v2 picks up the annotations and was running through the validation at a pre-persist stage. It seems like the pre-persist stage is during the transaction but after the SQL has been called (the entity gets an id via a sequence, and the entity had it's id set).

By turning off the automatic validation in the persistence.xml my code is working as expected.

Is there some limit as to what code you can run during the automatic validation stages?
Re: ConcurrentModificationException prior to updating entity. [message #511541 is a reply to message #506072] Mon, 01 February 2010 23:08 Go to previous messageGo to next message
Frank Sauer is currently offline Frank SauerFriend
Messages: 14
Registered: July 2009
Junior Member
keep in mind that that exception is not related to persistence at all. What this means is that while you are iterating over a collection, that collection is being modified as well. My guess is that something is being removed from it without using the iterator's remove() method.
Re: ConcurrentModificationException prior to updating entity. [message #511557 is a reply to message #511541] Tue, 02 February 2010 00:42 Go to previous messageGo to next message
beamso is currently offline beamsoFriend
Messages: 7
Registered: January 2010
Junior Member
I should have come back and replied to this thread earlier.

Turns out you can set the FlushMode of the transaction at runtime. This transaction now runs successfully when the FlushMode is set to FlushModeType.COMMIT. I think that queries I was doing during validation confused EclipseLink.

I can assure you that I wasn't removing something from a collection inside a for loop, otherwise the error would have popped up when using executing the same code using Toplink.
Re: ConcurrentModificationException prior to updating entity. [message #511697 is a reply to message #511557] Tue, 02 February 2010 15:01 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Hello,

From the sounds of it, the problem was that an event was getting triggered during a flush or commit. That event fired a query which because of the flushMode setttings, caused the transaction to again flush. This caused the exception since it was already being flushed, and EclipseLink internals started operating on a collection being used in the initial flush/commit.

While this situation is not a good one to be in, this should have been fixed in bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=256277 depending on the version being used.

Best regards,
Chris
Previous Topic:javax.naming.NoInitialContextException using "generate tables from entities"
Next Topic:stack overflow with subquery
Goto Forum:
  


Current Time: Sun Dec 21 10:08:10 GMT 2014

Powered by FUDForum. Page generated in 0.02317 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software