Hibernate version: 2.1.8
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.webserver.Player" table="player" dynamic-update="true" dynamic-insert="true" optimistic-lock="version">
<id name="playerID" column="playerid" type="long" length="12">
<generator class="native"/>
</id>
<version name="version" column="VERSION"/>
<property name="name" column="name" type="string" length="30" not-null="true"/>
<property name="phoneNo" column="phoneno" type="string" length="12" not-null="true"/>
<property name="type" column="type" type="string" length="2" not-null="true"/>
<property name="joinDate" column="joindate" type="timestamp" length="19" not-null="true"/>
<property name="password" column="password" type="string" length="15" not-null="true"/>
<property name="emailAddress" column="emailaddress" type="string" length="60" not-null="true"/>
<property name="startBalance" column="startbalance" type="long" length="12" not-null="true"/>
<property name="creditPoint" column="creditpoint" type="long" length="12" not-null="true"/>
<property name="loginID" column="loginid" type="string" length="10" not-null="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
List playerList = session.find("from Player");
Player player = (Player) playerList.get(0); //player1
Player targetPlayer = (Player) playerList.get(1); //player2
Transaction tx = null;
try{
tx = session.beginTransaction();
player.debit(1000);
targetPlayer.credit(1000);
session.update(player);
session.update(targetPlayer); <--- concurrectly modify player2 record on db
tx.commit();
}catch(StaleObjectStateException sose){
if (tx != null){
tx.rollback();
}
}
After execution, player2 success to rollback but player1 cannot rollback. However, if I only modify player1 record, they can all rollback? That mean the transaction has roll back problem
Exception throws after transaction commit:
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for com.webserver.Player instance with identifier: 2
at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:513)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:664)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:621)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2393)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.webserver.PlayerTest.testTransferCredit(PlayerTest.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:72)
at com.webserver.PlayerTest.main(PlayerTest.java:97)
11:36:03,829 ERROR SessionImpl:2400 - Could not synchronize database state with session
Name and version of the database you are using: mysql 4.1
The generated SQL (show_sql=true):
Hibernate: update player set VERSION=?, creditpoint=? where playerid=? and VERSION=?
Hibernate: update player set VERSION=?, creditpoint=? where playerid=? and VERSION=?
|