- 浏览: 2493525 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
Stop Update Here
2020-04-28 09:00 270I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 438NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 319Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 331Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 297Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 383Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 382Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 337Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 406VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 343Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 424NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 370Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 302Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 215GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 401GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 285GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 274Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 270Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 259Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 246Serverless with NodeJS and Tenc ...
相关推荐
7702-010 trouble shooting guide
HP-UX Trouble shooting 课程的PPT.各版本通用
ITM trouble shooting
DB Issue Trouble Shooting Guideline 很经典的数据结构资料
Mailrouting Trouble shooting lotus domino 8
tas58x5 trouble shooting
trouble shooting java performance 英文原版带书签。
HANA 2.0 trouble shooting guide. HANA调优可以参考此文档。
西门子双源CT维修手册
Akamai Techincal Trouble Shooting Sharing copy.pdf
这是第二部分,详细以trouble shooting为主。方便想看原版的朋友
try this doc,. it has great detail of MPLS trouble shotting techniques
PI问题解决文档,包括各个组件资料。文档内容详细...
NULL 博文链接:https://aga.iteye.com/blog/298106
weblogic 常见问题 TROUBLE_SHOOTING
数字钼靶机MAMMOMAT Novation_Trouble_shooting
Modbus expalnation pdf boook rs485 20 words a a d f
Trouble_Shooting_JavaScript
Study-Trouble-Shooting