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.  [ 4 posts ] 
Author Message
 Post subject: Pessimistic locking and MS SQLServer
PostPosted: Wed Jan 04, 2006 6:02 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
Hibernate version:H3.0.5 MSSQL 2000

NOTE: I am posting this question in this forum for the second time. I am really sorry if I am violating a rule here. Since I posted my question right before the christmas holidays, I am not sure if my post became "old" before any body had a chance to take a look at it. So here it is again:

Gavid had indicated in a different post that pessimistic locking works with MSSQL server in Hibernate 3. I tried it....

Here is simple java program that tries to update the same object at the same time in 2 different threads:


Code:
package my.sandbox;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class PessimisticSpike implements Runnable {

       
    public static void main(String[] args) throws Exception {
       
       
        ThreadGroup threadGroup = new ThreadGroup("PessimisticSpike");

        Thread thread = new Thread(threadGroup, new PessimisticSpike());
        thread.setName("Thread " + 1);
        thread.start();
        Thread.currentThread().sleep(2000);//start second thread after 2 sec
        Thread thread2 = new Thread(threadGroup, new PessimisticSpike());
        thread2.setName("Thread " + 2);
        thread2.start();
    }
   
    public void run() {
        try {
            String  name = Thread.currentThread().getName();
            System.out.println("In " + name);
            Session s = HibernateAbstractSessionUtil.getCurrentSession();
            Transaction tx = s.beginTransaction();
            System.out.println("Reading Entity in Thread:" + name);
            MyEntity e = (MyEntity ) s.load(MyEntity .class, new Long(6),LockMode.UPGRADE);
            //s.lock(e, LockMode.UPGRADE);
            e.setPrimaryOfficerName("J"+name);
            System.out.println("Sleeping " + name);
            Thread.currentThread().sleep(4000);
            System.out.println("Done Sleeping " + name);
            s.flush();
            tx.commit();
            s.close();
            System.out.println("Done" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



When i run this against oracle it works fine as expected with the following output:


In Thread 1
Reading Entity in Thread 1
Sleeping Thread 1
In Thread 2
Reading Entity in Thread 2
Done Sleeping Thread 1
DoneThread 1
Sleeping Thread 2
Done Sleeping Thread 2
DoneThread 2


Thread 2 reads only after Thread 1 id done.

But when i run the same code against MSSQL server, I got stale object exception:

In Thread 1
Reading Entity in Thread 1
Sleeping Thread 1
In Thread 2
Reading Entity in Thread 2
Sleeping Thread 2
Done Sleeping Thread 1
DoneThread 1
Done Sleeping Thread 2
org.hibernate.StaleObjectStateException:

I even tried loading the entity and then locking it using session.lock(). That didn't work either.
What am i missing?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 11:18 am 
Newbie

Joined: Tue Nov 07, 2006 11:14 am
Posts: 1
Hi!

I'm wondering whether there is a solution for this yet? Seems I'm encountering the same problem....


Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 3:13 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I have this same issue. Here is what I'm doing:

Code:
Query query = session.getNamedQuery( "SchemaNamespace.getNamespace" );

query.setLockMode( "ns", LockMode.UPGRADE );  // Does this work?

query.setString( 0, document.getNamespace().getNamespace() );
SchemaNamespace ns = (SchemaNamespace)query.uniqueResult();


I found this JIRA issue, but it doesn't seem to be in H3.x:

http://opensource.atlassian.com/projects/hibernate/browse/HB-1532

I'm testing with hibernate3 v3.2.0.cr2, and my SQL Server is SQL 2000. Any ideas?

I've read the hibernate in action book, and while they have a good discussion there's little guidance on the differences between lock modes as they relate to queries, criteria, etc. There's just not a lot of explanation on why there are different methods get(), load(), lock(), refresh(), or query.setLockMode().

Any help from the hibernate team on this?

Charlie


Top
 Profile  
 
 Post subject: Fix Provided
PostPosted: Mon Nov 20, 2006 4:57 pm 
Newbie

Joined: Thu Apr 27, 2006 10:09 pm
Posts: 4
I've attached a fix for 3.2.1 to the bug report:

http://opensource.atlassian.com/project ... e/HHH-1889

Does anyone know how to get this enough attention to warrant inclusion in the next Hibernate GA build? The company I work for uses SQL Server a lot and is currently migrating multiple applications to Hibernate.


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

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.