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)