calling persist on an object more than once

#1

results in an error (613) when the transaction is commited:

[ObjectDB 2.2.4] javax.persistence.RollbackException
Failed to commit transaction: Attempt to reuse an existing primary key value (common.model.UserSession:13) (error 613)
at com.objectdb.jpa.EMImpl.commit(EMImpl.java:271)
at javax.jdo.Transaction$commit.call(Unknown Source)

 

I submit that there are a number of problems with this:

a) the error message is not helpful

b) it's thrown at commit(), which may be in a different part of the codebase from the persist()

c) it's not at all clear to me why this shouldn't be idempotent

 

ie: if there really is a convincing reason for this feature, then it might be helpful to: a) provide a better help message (ie: 'you called persist more than once on object xxxx'), and b) to throw the exception at the second persist, rather than waiting for commit.

Thoughts?

 

#2

This is a request of JPA regarding the persistpersist(entity)EntityManager's methodMake an instance managed and persistent.See JavaDoc Reference Page... method:

Throws:
EntityExistsException - if the entity already exists. (If the entity already exists, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.)

The right way to update an entity doesn't include invocation of persist again.

It is difficult to distinguish between persisting an existing entity again and persisting a new entity with existing ID (primary key), so the error message is more general. There might be a place for improvement here. Notice that the error message indicates the entity class (common.model.UserSession) and the ID (13), so it is not that bad.

Throwing the exception on persist could be more friendly but very inefficient, since usually persist does access the database, so checking for existing entity in the database is only performed on commit or flush.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login