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