java – OptimisticLockException当使用JPA merge()
|
我有一个休息的应用程序,其中一个资源可以更新.以下是负责实现此任务的两种方法:
> updateWithRelatedEntities(String,Store):接收通过反序列化PUT请求实体构造的id和新对象Store,在新对象上设置版本(用于乐观锁定),并在事务中调用更新. public Store updateWithRelatedEntities(String id,Store newStore) {
Store existingStore = this.get(id);
newStore.setVersion(existingStore.getVersion());
em.getTransaction().begin();
newStore = super.update(id,newStore);
em.getTransaction().commit();
return newStore;
}
> update(String,T):进行更新的通用方法.检查ids匹配并执行合并操作. public T update(String id,T newObj) {
if (newObj == null) {
throw new EmptyPayloadException(type.getSimpleName());
}
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof Class) {
superclass = ((Class) superclass).getGenericSuperclass();
}
Class<T> type = (Class<T>) (((ParameterizedType) superclass).getActualTypeArguments()[0]);
T obj = em.find(type,id);
if (!newObj.getId().equals(obj.getId())) {
throw new IdMismatchException(id,newObj.getId());
}
return em.merge(newObj);
}
问题是这个调用:T obj = em.find(type,id);触发数据库中存储对象的更新,这意味着我们在触发合并时获得OptimisticLockException(因为版本现在不同). 为什么会发生这种情况?实现这一点的正确方法是什么? 我不想将属性从newStore复制到existingStore,并使用existingStore进行合并 – 我认为这将解决乐观锁定问题. 此代码未在应用程序服务器上运行,并且我没有使用JTA. 编辑: 解决方法
我看不到你的实体从您添加的代码,但我相信你失去了一些关键点与乐观锁定> @Version注释版本字段.
如果您的实体上有这个字段,那么容器应该能够执行合并过程而没有问题.请看看 Optimistic Locking也很好的文章 don’t break optimistic locking (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
