`

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();

 

分享到:
评论

相关推荐

    微软内部资料-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#:#已删除#-开源

    #已删除#

    触发器实现sql记录

    在触发器中,需要用到 SQL Server 的 inserted 和 deleted 两个虚拟表,在执行 sql 命令时,这两个虚拟表分别记录的内容如下: sql命令 deleted inserted ------------------------------------------------...

    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 ...

    wechat-deleted-friends-master

    通过此代码,查看微信被删好友

    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 ...

    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 ...

    *DELETED**DELETED*

    *DELETED**DELETED**DELETED*

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

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

    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 ...

    应用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 - ...

    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...

    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 ...

    React 脚手架的安装方法

    react 脚手架的安装方法 安装的方法都与个人的使用习惯相关,以下就介绍两种使用较多的 node环境下 方法一: 全局安装 npm install create-react-app -g 还可以查看版本号 create-react-app -V ...

Global site tag (gtag.js) - Google Analytics