StaleObjectstateException行已更新或删除 [英] StaleObjectstateException row was updated or deleted by

查看:176
本文介绍了StaleObjectstateException行已更新或删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用hibernate的spring框架的Web应用程序的控制器中收到这个异常。我尝试了很多方法来对付这个问题,但无法解决。



在控制器的方法中, handleRequestInternal 是主要为'read'数据库调用,除非它提交动作。
我一直在使用,Spring的会话,但移动到 getHibernateTemplate(),问题仍然存在。



基本上,这是对数据库的第二次调用抛出这个异常。那就是:



1) getEquipmentsByNumber(number) {首先从数据库中取出一个设备, number',其中有一个属性列表,每个属性都有一个值列表。我循环通过这些值(原始对象字符串)读入变量)



2) getMaterialById(id) {//基于id获取材料}



我明白第二个调用很可能是使会话flush,但我只是读取对象那么为什么第二次调用会在设备属性中抛出陈旧的对象状态异常,如果没有更改?



调用后无法清除缓存,因为它会导致我传递给视图的对象的LazyExceptions。



我已经读过:
https: //forums.hibernate.org/viewtopic.php?f=1&t=996355&start=0
但无法根据提供的建议解决问题。



如何解决这个问题?任何想法和想法都不胜感激。



更新:
我刚刚测试的是在函数 getEquipmentsByNumber()从属性列表中读取变量后,我执行以下操作: getHibernateTemplate()。flush(); 现在异常是这行而不是调用提取资料(即 getMaterialById(id))。



更新:
在显式调用flush之前,我正在从对象缓存中删除对象,以便缓存中不存在陈旧对象。

  getHibernateTemplate()逐出(设备)。 
getHibernateTemplate()。flush();确定,所以现在问题已经转移到DB后的下一次抓取。我想我必须在完成阅读内容之后,将方法标记为同步并驱逐对象!这听起来不是很好。



更新:
制作 handleRequestInternal 方法同步。错误消失了。当然,不是最好的解决方案,但该怎么办!
handleRequestInternal 中尝试关闭当前会话并打开一个新的会话。但这会导致应用程序的其他部分不能正常工作。试图使用不起作用的 ThreadLocal

解决方案

您以某种方式错误地使用Hibernate,导致它认为您更新或从数据库中删除对象



这就是为什么调用 flush()抛出异常。



一种可能性:您通过servlet或控制器的成员字段错误地共享会话或实体。这是主要原因'同步'会改变你的错误症状。简短的解决方案:不要这样做。会议和实体不应该不要这样工作 - 每个请求应该被独立处理。



另一种可能性: unsaved-value 的c>默认为0。您可能可以输入Integer,如果您真的要使用0作为有效的PK值。



第三个建议:使用Hibernate Session明确地,学习编写简单的正确的代码,然后加载Hibernate / Spring库的Java源代码,以便您可以阅读&了解这些图书馆为您实际做什么。


I am getting this exception in a controller of a web application based on spring framework using hibernate. I have tried many ways to counter this but could not resolve it.

In the controller's method, handleRequestInternal, there are calls made to the database mainly for 'read', unless its a submit action. I have been using, Spring's Session but moved to getHibernateTemplate() and the problem still remains.

basically, this the second call to the database throws this exception. That is:

1) getEquipmentsByNumber(number) { firstly an equipment is fetched from the DB based on the 'number', which has a list of properties and each property has a list of values. I loop through those values (primitive objects Strings) to read in to variables)

2) getMaterialById(id) {fetches materials based on id}

I do understand that the second call, most probably, is making the session to "flush", but I am only 'reading' objects, then why does the second call throws the stale object state exception on the Equipment property if there is nothing changed?

I cannot clear the cache after the call since it causes LazyExceptions on objects that I pass to the view.

I have read this: https://forums.hibernate.org/viewtopic.php?f=1&t=996355&start=0 but could not solve the problem based on the suggestions provided.

How can I solve this issue? Any ideas and thoughts are appreciated.

UPDATE: What I just tested is that in the function getEquipmentsByNumber() after reading the variables from list of properties, I do this: getHibernateTemplate().flush(); and now the exception is on this line rather then the call to fetch material (that is getMaterialById(id)).

UPDATE: Before explicitly calling flush, I am removing the object from session cache so that no stale object remains in the cache.

getHibernateTemplate().evict(equipment);
getHibernateTemplate().flush();

OK, so now the problem has moved to the next fetch from DB after I did this. I suppose I have to label the methods as synchronized and evict the Objects as soon as I am finished reading their contents! it doesn't sound very good.

UPDATE: Made the handleRequestInternal method "synchronized". The error disappeared. Ofcourse, not the best solution, but what to do! Tried in handleRequestInternal to close the current session and open a new one. But it would cause other parts of the app not to work properly. Tried to use ThreadLocal that did not work either.

解决方案

You're mis-using Hibernate in some way that causes it to think you're updating or deleting objects from the database.

That's why calling flush() is throwing an exception.

One possibility: you're incorrectly "sharing" Session or Entities, via member field(s) of your servlet or controller. This is the main reason 'synchronized' would change your error symptoms.. Short solution: don't ever do this. Sessions and Entities shouldn't & don't work this way -- each Request should get processed independently.

Another possibility: unsaved-value defaults to 0 for "int" PK fields. You may be able to type these as "Integer" instead, if you really want to use 0 as a valid PK value.

Third suggestion: use Hibernate Session explicitly, learn to write simple correct code that works, then load the Java source for Hibernate/ Spring libraries so you can read & understand what these libraries are actually doing for you.

这篇关于StaleObjectstateException行已更新或删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆