`
sillycat
  • 浏览: 2551704 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Trouble Shooting on Hibernate StaleObjectStateException

 
阅读更多
Trouble Shooting on Hibernate StaleObjectStateException

I am getting StaleObjectStateException on grails 1.3.7.
Stale Object State Exception
Error Message
2013-03-05 16:26:06,140 [http-bio-8080-exec-35] ERROR com.sillycat.xxx.ErrorController  - Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.sillycat.xxx.Device#3015]
org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.sillycat.xxx.Device#3015]
at Skipped non-sillycat elements.(:86)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.sillycat.xxx.Device#3015]
at Skipped non-sillycat elements.(:1)
at com.sillycat.xxxx.events.EventService.saveEvent(EventService.groovy:93)
at com.sillycat.xxx.events.GeoFenceEntryEventService.super$2$saveEvent(GeoFenceEntryEventService.groovy)
at Skipped non-sillycat elements.(:1)

Since the bottom of grails 1.3.7 is hibernate 3.3.1.GA. I begin to create the environment of Hibernate and I try to produce and solve that problem on Hibernate first.

1. Build the Hibernate environment
Packages Here is my pom.xml to fetch all the related packages
<dependency>
               <groupId>org.hibernate</groupId>
               <artifactId>hibernate-core</artifactId>
               <version>${hibernate.version}</version>
</dependency>
<dependency>
               <groupId>org.hibernate</groupId>
               <artifactId>hibernate-entitymanager</artifactId>
               <version>${hibernate.version}</version>
</dependency>
<dependency>
               <groupId>org.hibernate</groupId>
               <artifactId>hibernate-annotations</artifactId>
               <version>${hibernate.version}</version>
</dependency>
<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.5.2</version>
            <scope>test</scope>
</dependency>
<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.2</version>
            <scope>test</scope>
</dependency>
<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <scope>test</scope>
</dependency>
<dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <version>5.1.23</version>
</dependency>

And the version of Hibernate I am using is <hibernate.version>3.3.1.GA</hibernate.version>

Hibernate Configuration - hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
     <session-factory>
          <!-- Database connection settings -->
          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost/test</property>
          <property name="connection.username">root</property>
          <property name="connection.password">kaishi</property>

          <!-- JDBC connection pool (use the built-in) -->
          <property name="connection.pool_size">1</property>

          <!-- SQL dialect -->
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

          <!-- Enable Hibernate's automatic session context management -->
          <property name="current_session_context_class">thread</property>

          <!-- Disable the second-level cache -->
          <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

          <!-- Echo all executed SQL to stdout -->
          <property name="show_sql">true</property>

          <!-- Drop and re-create the database schema on startup -->
          <property name="hbm2ddl.auto">update</property>

          <!-- Mapping files -->
          <mapping resource="hbm/student.hbm.xml" />

     </session-factory>
</hibernate-configuration>

And the Object Configuration file for Hibernate
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <!--
     <class name="com.sillycat.easyhibernate.model.Student" table="students" optimistic-lock="all" dynamic-update="true">
     <class name="com.sillycat.easyhibernate.model.Student" table="students" optimistic-lock="none">
     -->
     <class name="com.sillycat.easyhibernate.model.Student" table="students" optimistic-lock="none">
          <id name="id" type="int" column="id">
               <generator class="native" />
          </id>
          <property name="firstName">
               <column name="first_name" />
          </property>
          <property name="lastName">
               <column name="last_name" />
          </property>
     </class>
</hibernate-mapping>

2. Reproduce the Exception with Java Codes
package com.sillycat.easyhibernate.temp;

import java.util.Date;

import org.hibernate.Session;

import com.sillycat.easyhibernate.model.Student;
import com.sillycat.easyhibernate.util.HibernateUtil;

public class MyRunnable implements Runnable {
     private String id;

     @Override
     public void run() {
          Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
          Student student = (Student) session.load(Student.class, Integer.valueOf(id));
          student.setFirstName("xxx" + (new Date()).getTime());
          session.save(student);
          try {
               Thread.sleep(10000);
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
          session.getTransaction().commit();
          System.out.println("Done " + student.getFirstName());
     }

     public MyRunnable(String id) {
          this.id = id;
     }

}

package com.sillycat.easyhibernate.temp;


public class StaleObjectStateExceptionMain {


publicstaticvoid main(String[] args) {
for(int i = 0;i< 2;i++){
Thread t = new Thread(new MyRunnable("1"));
    t.start();
}
}


}

If I am using the lock on hibernate configuration, the Exception will reproduce.
If I change it to none, it will work fine. So the most import part is as follow:
<class name="com.sillycat.easyhibernate.model.Student" table="students" optimistic-lock="all" dynamic-update="true">
<class name="com.sillycat.easyhibernate.model.Student" table="students" optimistic-lock="none">

References:
http://stackoverflow.com/questions/7664768/optimistic-locking-and-org-hibernate-staleobjectstateexception
http://www.grails.org/doc/1.3.7/guide/15.%20Grails%20and%20Hibernate.html
http://stackoverflow.com/questions/8081413/hibernate-program-errors


http://grails.org/doc/1.3.7/guide/single.html#5.5.2.7%20Optimistic%20Locking%20and%20Versioning
分享到:
评论

相关推荐

    HP-UX Trouble shooting ppt

    HP-UX Trouble shooting 课程的PPT.各版本通用

    7702-010 trouble shooting guide

    "7702-010 trouble shooting guide" 指南旨在为技术人员提供一个详尽的框架,帮助他们有效地识别和解决与7702-010相关的各种问题。这个指南可能是针对某个特定的硬件组件、软件系统、网络设备或者是一个特定的技术...

    DB Issue Trouble Shooting Guideline 很经典的数据结构资料

    DB Issue Trouble Shooting Guideline 很经典的数据结构资料

    AD 活动目录trouble shooting,英文版。MS出品

    通过对微软内部文档《AD 活动目录trouble shooting》的学习与分析,我们了解到了关于AD活动目录故障排查的多种方法和技术。这不仅有助于提高IT人员的工作效率,也能帮助企业更好地管理和维护复杂的网络环境。希望...

    Mailrouting Trouble shooting lotus domino 8

    Mailrouting Trouble shooting lotus domino 8

    ITM trouble shooting

    ### ITM Trouble Shooting #### 简介与问题确定 在IT管理领域,尤其是对于复杂的监控系统如IBM Tivoli Monitoring (ITM),问题诊断是一项关键能力。本文将基于提供的文档概览,深入探讨其中涉及的关键知识点,包括...

    西门子双源CT维修手册Gantry Trouble Shooting Guide.pdf

    西门子双源CT维修手册是西门子公司为其SOMATOM Definition双源CT扫描仪所提供的官方技术文档,用于指导技术人员对CT设备的机架部分进行故障排查。这份手册是专为授权人员使用,涵盖了安全信息、操作指南、故障排除...

    mpls trouble shooting training

    try this doc,. it has great detail of MPLS trouble shotting techniques

    trouble shooting java performance

    标题“trouble shooting java performance”直接指出了本书的主题——解决Java性能问题的故障排除方法。作者Erik Ostermueller通过使用开源工具来检测Java应用程序中的反模式,提出了一种可重复的方法来处理基于Java...

    jboss jta configuration trouble shooting

    当遇到"JBoss JTA configuration trouble shooting"的问题时,开发者通常需要深入理解JTA的工作原理以及如何在JBoss应用服务器中正确配置它。以下是对这个主题的详细讲解: 首先,JTA是一个Java标准,定义了API来...

    PI trouble shooting(错误处理)

    PI问题解决文档,包括各个组件资料。文档内容详细...

    HANA trouble shooting guide

    在进行HANA问题的诊断和性能分析时,需要关注一系列潜在的性能瓶颈和故障原因。本文档将详细介绍HANA 2.0的问题排查方法,同时涉及调优建议,以帮助数据库管理员和开发人员快速定位并解决HANA系统中遇到的各种问题。...

    tas58x5 trouble shooting

    TAS5805M故障排查 TAS5805M是一款音频Codec芯片,由德州仪器(TI)公司生产。该芯片广泛应用于音频设备中,例如音箱、耳机、音频处理器等。然而,在实际应用中,TAS5805M芯片可能会出现一些故障,影响音频设备的...

    Akamai Techincal Trouble Shooting Sharing copy.pdf

    ### Akamai技术故障排查详解 #### 一、引言 Akamai Technologies是一家全球领先的云服务提供商,致力于提供高效、可靠且安全的内容分发网络(CDN)服务。Akamai的技术支持工程师们经常面临各种各样的技术问题,本...

    Trouble-Shooting_Guide

    "Trouble-Shooting_Guide"显然是一份详细记录了各种问题解决步骤的文档,旨在帮助用户诊断并修复他们可能遇到的技术问题。这份指南可能是以一种结构化的格式编写的,包括了问题识别、原因分析、解决方案以及预防措施...

    weblogic 常见问题 TROUBLE_SHOOTING

    【WebLogic Server常见问题TROUBLE_SHOOTING】 WebLogic Server是BEA Systems(现在已被Oracle公司收购)开发的一款企业级Java应用服务器,用于部署和管理Java EE应用程序。本资料主要针对WebLogic Server的常见...

    RS485_Trouble_Shooting.zip_RS485 modbus_Trouble_modbus rs485

    标题中的“RS485_Trouble_Shooting.zip”表明这是一个关于RS485通信协议故障排查的压缩文件,而“RS485 modbus_Trouble_modbus rs485”进一步指出了主题涉及RS485接口下的Modbus通信问题。描述中的“Modbus ...

    数字钼靶机MAMMOMAT Novation_Trouble_shooting.pdf

    文档标题提及的是"MAMMOMAT Novation"的故障排查指南,这是一款由Siemens AG生产的数字钼靶机,主要用于乳腺X线摄影检查。该设备在医疗领域中用于早期检测和诊断乳腺疾病,特别是乳腺癌。 维修手册标签表明这份资料...

    Brocade Partner ADX ts

    Brocade Partner ADX Training _Trouble Shooting

Global site tag (gtag.js) - Google Analytics