-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Timestamp as optimistic locking column problem
PostPosted: Tue Aug 24, 2004 3:33 pm 
Newbie

Joined: Tue Aug 24, 2004 2:26 pm
Posts: 6
Hibernate version: 2.1.6

Mapping documents:
Department:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.paymentone.hibernate.prototype.Department" table="department">
        <id name="id" type="string" unsaved-value="null">
            <generator class="uuid.string">               
            </generator>
        </id>
        <timestamp
        column="timestamp"
        name="timestamp"
        unsaved-value="null"/>       
        <property name="name" type="string"/>                        
        <bag name="employees" table="employee" lazy="true" inverse="true" cascade="all">
           <key column="dept"/>
           <one-to-many class="com.paymentone.hibernate.prototype.Employee"/>
        </bag>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
createDepartment:
Code:
Session s = HibernateUtil.currentSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            s.save(dept);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }

updateDepartment:
Code:
Session s = HibernateUtil.currentSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            s.update(dept);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }

queryDepartment:
Code:
Session s = HibernateUtil.currentSession();
        try {
            List depts = s.find("from Department"); 
           
            return depts;
        } finally {
            HibernateUtil.closeSession();
        }
Full stack trace of any exception that occurs:

Name and version of the database you are using: Oracle 9.2.0.4

Debug level Hibernate log excerpt: NA

I was trying to execute the following code to see if optimistic locking is working the way I expected:

Code:
private void testOptimisticLocking() throws Exception{
        HumanResourceMgmt mgr = HumanResourceMgmt.getInstance();
       
        Department dept = new Department();
        dept.setName("Engineering");
        mgr.createDepartment(dept);
       
        List depts = mgr.queryDepartments();       
        Department queriedDept = (Department) depts.get(0);
       
        List depts2 = mgr.queryDepartments();   
        Department queriedDept2 = (Department) depts2.get(0);
       
        // queriedDept and queriedDept2 came from different sessions and are
        // thus different objects
       
        queriedDept.setName("Engineering1");
        mgr.updateDepartment(queriedDept);
        //Thread.sleep(20000);
        try {
            queriedDept2.setName("Engineering2");
            mgr.updateDepartment(queriedDept2);
            System.out.println("Exception is not thrown with timestamp as version column!");
        } catch (Exception e) {
            System.out.println("As expected, optimistic locking causes exception to be thrown");
        }
    }


But to my surprise, the StaleObjectStateException is not thrown. I was developing the test cases in Eclipse, so I changed to the debug mode to try to see if I did something wrong. Again to my surprise, the StaleObjectStateException is thrown when running the testcase in the debug mode!

Would anyone have any idea what the problem is? The jdk version I am using is: j2re1.4.2_03.

If I changed the timestamp column to the following:
<version column="version" name="version" type="integer" unsaved-value="negative"/>
,the optimistic locking works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.