当openjpa(版本2.3.0)持久存在已在另一个上下文中修改的实体时,它会抛出一个UndeclaredThrowableException,并将OptimisticLockException作为最深的祖先异常。我的代码和实际例外情况如下。
在通过捕获UndeclaredThrowableException并解析异常链来解决问题之前,我想知道
我的代码是错误还是有一些openjpa怪癖?
我的代码
IT savedEntity = null;
try {
savedEntity = em.merge(entity); // re-attach
this.firePropertyChange(entity, savedEntity);
} catch (OptimisticLockException ole) {
logger.error("Error saving entity [" + entity.toString() + "]", ole);
throw JpaExceptionFactory.createRecordChanged(ole, entity == null ? 0 : ((IDatabaseEntity) entity).getId(),
entity == null ? "null entity" : entity.getClass().getName());
} catch (PersistenceException he) {
logger.error("Error saving entity [" + entity.toString() + "]", he);
throw JpaExceptionFactory.createPersistence(he, entity == null ? 0 : ((IDatabaseEntity) entity).getId(),
entity == null ? "null entity" : entity.getClass().getName());
} catch (Throwable t) {
logger.error("Error saving entity [" + entity.toString() + "]", t);
throw JpaExceptionFactory.createUnknownDatabase(t, entity == null ? 0 : ((IDatabaseEntity) entity).getId(),
entity == null ? "null entity" : entity.getClass().getName());
}
return savedEntity;
异常
2015-03-22 16:03:00,881 | ERROR | nelWorkerThread1 | AbstractEntity | 152 - com.easa.motordb.server.services - 0.0.1.SNAPSHOT | Error saving entity [DcMotor [id=4655, version=4, powerUnits=0, powerValue=20.0, poles=0, manufacturer=test install, rpm=0, frameBase=asdf, frameSuffix=, framePrefix=asdf, model=, armVolts=0.0, shuntVolts=0.0, typeDesc=, armAmp=0.0, windingType=0, interPole=0, coreLength=0.0, coreDiam=0.0, slots=0, bars=0, turnsCoil=, slotPitch=0, commPitch=0, equalizers=0, equalizerPitch=0, shuntCoils=0, shuntCR=0.0, shuntTC1=0.0, shuntTC2=0.0, seriesCoils=0, seriesCR=0.0, seriesTC1=0.0, seriesTC2=0.0, ipCoils=0, ipCR=0.0, ipTC1=0.0, ipTC2=0.0, ctrBar=0, micaBar1=0, micaBar2=0, leadThrow4=, leadThrow5=, leadThrow6=, occurs=0, dataSource=, userName=, createDate=Wed Mar 11 11:00:00 EDT 2015, modifyDate=Sun Mar 22 16:03:00 EDT 2015, armWires=[], shuntWires=[], ipWires=[], seriesWires=[], eqWires=[], origId=0, motorDupKey=, coreUom=null, airGapDensity=0.0]]
java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy22.merge(Unknown Source)
at com.easa.motordb.services.jpa.AbstractEntity.save(AbstractEntity.java:395)
at com.easa.motordb.services.jpa.DcMotorEntity.save(DcMotorEntity.java:126)
at com.easa.motordb.services.jpa.DcMotorEntity.save(DcMotorEntity.java:22)
at Proxyf1547343_5d2d_4cb2_90e9_351499ef7b89.save(Unknown Source)
at Proxyf1547343_5d2d_4cb2_90e9_351499ef7b89.save(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_65]
at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_65]
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_65]
at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_65]
at org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManagerHandler.invoke(JTAEntityManagerHandler.java:185)
... 13 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy36.merge(Unknown Source)
... 18 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_65]
at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_65]
at org.apache.aries.jpa.container.impl.EntityManagerProxyFactory$EMHandler.invoke(EntityManagerProxyFactory.java:31)
... 19 more
Caused by: <openjpa-2.3.0-r422266:1540826 fatal store error> org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock violation was detected when flushing object instance "com.easa.motordb.model.jpa.DcMotor-4655" to the data store. This indicates that the object was concurrently modified in another transaction.
FailedObject: com.easa.motordb.model.jpa.DcMotor-4655
at org.apache.openjpa.kernel.VersionAttachStrategy.compareVersion(VersionAttachStrategy.java:219)
at org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:175)
at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:252)
at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:105)
at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3510)
at org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1214)
at org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:878)
... 24 more