`

Row was updated or deleted by another transaction (or unsaved-value mapping was

    博客分类:
  • java
阅读更多
org.hibernate.StaleObjectStateException: Row was updated or deleted by another 
transaction (or unsaved-value mapping was incorrect):
 [com.shkco.adsr3.cbm.vo.CustomerAccountInfo#74225]

 

at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2523)
	at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3242)
	at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3140)
	at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3470)
	at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:140)
	at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393)
	at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:385)
	at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:302)
	at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
	at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240)
	at com.shkco.adsr3.framework.dao.SessionWrapper.flush(SessionWrapper.java:209)
	at com.shkco.adsr3.framework.dao.AbstractDAO.flushAndClear(AbstractDAO.java:459)
	at com.shkco.adsr3.framework.service.AbstractBusinessService.flushAndClear(AbstractBusinessService.java:89)

 

使用Evict清除指定缓冲对象即可。

evict(custAcctInfo);

 

evict(custAcct);

 

一、Clear 方法

无论是Load 还是 Get 都会首先查找缓存(一级缓存) 如果没有,才会去数据库查找,调用Clear() 方法,可以强制清除Session缓存。

public void testClear(){  
    Session session =  HibernateUitl.getSessionFactory().getCurrentSession();  
    session.beginTransaction();  
    Teacher t = (Teacher) session.get(Teacher.class, 3);  
    System.out.println(t.getName());  
    session.clear();//这里不clear只会执行一次sql语句,有clear会执行2次  
    Teacher t2 = (Teacher) session.get(Teacher.class, 3);  
    System.out.println(t2.getName());  
    session.getTransaction().commit();  
}  

 

二、Flush 方法

可以强制进行从内存到数据库的同步。例:

  1. publicvoid testFlush(){
  2. Session session =HibernateUitl.getSessionFactory().getCurrentSession();
  3. session.beginTransaction();
  4. Teacher t =(Teacher) session.get(Teacher.class,3);
  5. t.setName("yyy");
  6. session.flush();//有flush会执行2次UPDAE,没有会只执行一次
  7. t.setName("yyyyy");
  8. session.getTransaction().commit();
  9. }

Flush方法是可以设置的,也就是 fulsh 什么时候执行是可以设置的,在session.beginTransaction 前设置 FlushMode

  1. session.setFlushMode(FlushMode.Always|AUTO|COMMIT|NEVER|MANUAL);

这里 FlushMode 有 5 个值可选

Always: 任何代码都会 Flush

AUTO: 默认方式 – 自动

Commit: COMMIT 时

Never: 始终不

MANUAL: 手动方式

设置 FlushMode 有个好处是可以节省开销,比如默认 session 只做查询时,就可以不让他与数据库同步了。

三、Evict 方法

会把指定的缓冲对象进行清除,而 Clear 方法则是把缓冲区内的全部对象清除,但不包括操作中的对象。

注意事项: Flush 方法和 Evict 方法一起使用出现的问题

Flush 方法和 Evict 方法一起使用出现的时候,一定要注意使用的顺序,否知会出现异常。下面的代码就是正确的使用顺序:

 

  1. //因为user的主键生成策略采用的是uuid,所以调用完成save后,
  2. //只是将user纳入到了session的管理
  3. //不会发出insert语句,但是id已经生成,session中existsInDatebase状态为false
  4. session.save(user);
  5.  
  6. //flush后hibernate会清理缓存,会将user对象保存到数据库中
  7. //(强制更新数据库,结果但不一定,因为commit时候可能会出现事物回滚)
  8. //并且设置session中existsInDatebase的状态为true
  9. session.flush();
  10.  
  11. //将user对象从session中逐出,即session的EntityEntries属性中逐出
  12. session.evict(user);
  13.  
  14. //可以成功提交,因为hibernate在清理缓存时,
  15. //在session的insertions集合中无法找到user对象
  16. //所以就不会发出insert语句,也不会更新session中的existsInDatabase的状态
  17. tx.commit();

 

分享到:
评论

相关推荐

    hibernate错误汇总

    错误十:Exception in thread "main" org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) 错误原因:这个异常通常发生在并发...

    wechat-deleted-friends-master

    《微信好友恢复:深入解析“wechat-deleted-friends-master”项目》 在我们的日常生活中,微信作为一款全球广泛使用的社交应用,已经成为了我们联系亲朋好友、进行商务沟通的重要工具。然而,有时由于误操作或者...

    微软内部资料-SQL性能优化3

    In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...

    #DELETED#:#已删除#-开源

    【开源软件】是一种重要的软件开发模式,它遵循开放源代码的原则,允许用户自由地使用、修改、复制和分发源代码。开源软件的兴起源于20世纪80年代末,随着互联网的发展,开源软件的理念逐渐普及,如今已经成为全球...

    acpi控制笔记本风扇转速

    condition was removed by modifying AcpiWalkNamespace to (by default) ignore all temporary namespace entries created during any concurrent control method execution. An additional namespace race ...

    ImpREC 1.7c

    - Updated/corrected plugins and deleted dups v1.6 FINAL (PUBLIC VERSION) --------------------------- - Misc - Finally fixed the bug in the check for adding section (Thanks to Christoph) v1.6 ...

    EF(EntityFramework) 插入或更新数据报错的解决方法

    Entities may have been modified or deleted since entities were loaded. See ...

    Laravel开发-laravel-transaction-observer

    在Laravel中,观察者常用于监听模型事件,如`created`, `updated`, `deleted`等。你可以创建一个观察者类,然后定义要监听的事件方法。以下是一个简单的观察者例子: ```php class UserObserver { public function...

    MySQL 8.0.13设置日期为0000-00-00 00:00:00时出现的问题解决

    刚开始学习数据库操作,今天存数据时发现,保存的时候报错(Error 1292: Incorrect datetime value: '0000-00-00' for column 'deleted_at' at row 1) ,之后就搜索了下原因,是因为MySQL存日期时不允许出现这种格式...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The value of some string flex-properties that began with a parenthese or curly bracket had no apostrophe at the end; that caused an error when reading. (fixed TPropList.SavePropValue for the ...

    project-lvl2-s269:我的应用“差异计算器”

    Property 'common.setting2' was deleted Property 'common.setting3' was updated. From 'true' to complex value Property 'common.setting6.ops' was added with value: 'vops' Property 'common.setting4' was ...

    文件恢复工具

    Empowered by the new unique data recovery technologies, it is the most comprehensive data recovery solution for recovery files from FAT12/16/32, NTFS, NTFS5 (created or updated by Windows 2000/XP/...

    S7A驱动720版本

    - The driver could crash when the driver was stopped (either by hand or when the demo time was expired) and in the same moment the NetLink connection was disturbed. - Improvements on the seamless ...

    eac3to V3.17

    * Blu-Ray subtitle demuxing: PTS value is now written to both PTS + DTS * joining MKV files is now declined with a proper error message * last chapter is now removed, if it's less than 10 seconds from...

    应用Dephi 开发佳能照相机API

    This function delete all the reference or list objects that user has forgotten to delete. Parameters: In: None Out: None Returns: Returns EDS_ERR_OK if successful. In other cases, see ...

    SQLite-Deleted-Records-Parser, 在SQLite数据库中,用于恢复已经删除项目的脚本.zip

    SQLite-Deleted-Records-Parser, 在SQLite数据库中,用于恢复已经删除项目的脚本 SQLite解析器在SQLite数据库中恢复删除条目并将输出放入TSV文件或者文本文件( R ) 中的脚本sqlparse.py的###Usagesqlparse.py -f/...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part2

    Q521783 - Ribbon Form - An AV occurs when restoring a minimized form whose non-client area is painted by the Ribbon if the form was minimized while displaying a hint for its window button Q526309 - ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part1

    Q521783 - Ribbon Form - An AV occurs when restoring a minimized form whose non-client area is painted by the Ribbon if the form was minimized while displaying a hint for its window button Q526309 - ...

    psp学习资料

    The example was updated to a 25 week schedule which assumes that the same planned value is earned each week. • Changed the exercises in lecture 7 Software Design. The new exercises have the class...

    Android代码-android-architecture-components

    Samples may not be fully featured, documented or tested and could be modified or deleted in the future. BasicSample - Shows how to persist data using a SQLite database and Room. Also uses ViewModels ...

Global site tag (gtag.js) - Google Analytics