-->
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: Differences between session.lock and query.setLockMode.
PostPosted: Mon Nov 27, 2006 6:22 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Hibernate version: 3.1.3
Mapping documents: not matter
Name and version of the database you are using: PostgreSQL 8.1.3

Hello, I have implemented some classes to translate hibernate exceptions and other stuff and now I have some doubt about lock methods to use pessimistic locking. So, I have done the following code:
Code:
public List findUnique(String query, Object[] params, String lockAlias) {

   Session session = (Session) transManag.getPersistenceSession();

   String queryValue = queryProperties.getProperty(query);
   Type[] types = TypeConverter.convertTypes(params);

   Query hibernateQuery = session.createQuery(queryValue);
   hibernateQuery.setParameters(params, types);
   hibernateQuery.setMaxResults(1);

   return hibernateQuery.setLockMode(lockAlias, LockMode.WRITE).list();
}

But this is a little bit intrusive because I must specify the lockAlias. Therefore, I am considering change it to the code below:
Code:
public List findUnique(String query, Object[] params) {

   Session session = (Session) transManag.getPersistenceSession();

   String queryValue = queryProperties.getProperty(query);
   Type[] types = TypeConverter.convertTypes(params);

   Query hibernateQuery = session.createQuery(queryValue);
   hibernateQuery.setParameters(params, types);
   hibernateQuery.setMaxResults(1);

   List queryResult = hibernateQuery.setLockMode(lockAlias, LockMode.WRITE).list();

   Object result = queryResult.iterator().next();

   return result;
}

In the second code snipped I don't have to use the alias parameter. But I am concerned about concurrency issues. Is the first way more secure than the second? Or are they equivalent?

kind regards

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


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.