-->
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: Criteria with setLockMode in SQL Server
PostPosted: Tue Sep 27, 2005 4:58 pm 
Newbie

Joined: Tue Sep 27, 2005 12:04 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Name and version of the database you are using: SQL Server 2000

I have a Criteria query like this one:


Code:
      Criteria criteria = dbSession.createCriteria(Numerator.class);
      criteria.add(Expression.eq("name", numeratorNameKey));
      criteria.setLockMode(LockMode.UPGRADE);
      Numerator numerator = (Numerator)criteria.uniqueResult();


Since SQL server hasn't "for Update" syntax this criteria must generate a query with a "with (updlock)" hint after the from table. But the generated query is:
Code:
select this_.OID as OID0_, this_.MODIFICATION_TIMESTAMP as MODIFICA2_23_0_, this_.CREATION_TIMESTAMP as CREATION3_23_0_, this_.CREATION_USER_ID as CREATION4_23_0_, this_.MODIFICATION_USER_ID as MODIFICA5_23_0_, this_.MODIFICATION_IP_ADDRESS as MODIFICA6_23_0_, this_.CREATION_IP_ADDRESS as CREATION7_23_0_, this_.NAME as NAME23_0_, this_.LAST_NUMBER as LAST9_23_0_, this_.DAILY_OPEN_NUMBER as DAILY10_23_0_ from NUMERATOR this_ where this_.NAME=?


However, if I do the following I get the lock but one unnecessary query:
Code:
      Criteria criteria = dbSession.createCriteria(Numerator.class);
      criteria.add(Expression.eq("name", numeratorNameKey));
      Numerator numerator = (Numerator)criteria.uniqueResult();
      dbSession.lock(numerator, LockMode.UPGRADE);


In the last case I get:
Code:
select this_.OID as OID0_, this_.MODIFICATION_TIMESTAMP as MODIFICA2_23_0_, this_.CREATION_TIMESTAMP as CREATION3_23_0_, this_.CREATION_USER_ID as CREATION4_23_0_, this_.MODIFICATION_USER_ID as MODIFICA5_23_0_, this_.MODIFICATION_IP_ADDRESS as MODIFICA6_23_0_, this_.CREATION_IP_ADDRESS as CREATION7_23_0_, this_.NAME as NAME23_0_, this_.LAST_NUMBER as LAST9_23_0_, this_.DAILY_OPEN_NUMBER as DAILY10_23_0_ from NUMERATOR this_ where this_.NAME=?

select OID from NUMERATOR with (updlock, rowlock) where OID =? and MODIFICATION_TIMESTAMP =?


The problem is that with the last option the application get into deadlocks, because the lock is obtained (probably) over an old object if between the first and the second query another user does the same operation. In fact I simulate this situation and I get the deadlock.

Code:
2005-09-27 17:09:44,774 [Servlet.Engine.Transports : 2] ERROR org.hibernate.util.JDBCExceptionReporter - [ML]Transaction (Process ID 101) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
2005-09-27 17:09:44,837 [Servlet.Engine.Transports : 2] ERROR com.yax.rd.core -
org.hibernate.exception.GenericJDBCException: could not lock: [com.yax.rd.core.businessentities.Numerator#16]
   at java.lang.Throwable.<init>(Throwable.java)


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.