• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Obtain Lock using Hibernate Criteria API

 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm trying to use Hibernate Criteria API to obtain row locks in the Oracle Database. When I try to use the setLockMode method of the criteria API it is giving a null pointer exception in the Hibernate framework. I'm not able to figure where I'm going wrong.

This is the code that I'm using to obtain the lock -
Criteria criteria = getSession().createCriteria(MutexForParty.class);
ProjectionList projections = Projections.projectionList();
projections.add(Projections.property("updatedCount"));
criteria.setProjection(projections);
criteria.add(org.hibernate.criterion.Expression.in("id.deltaRef", deltaNames));
criteria.setLockMode("this", LockMode.UPGRADE);
List updatedCountList = criteria.list();

If I comment out the line criteria.setLockMode("this", LockMode.UPGRADE);
everything works perfect but when I have this statement its throwing a null pointer.

I'm new to Criteria API and unable to figure out what I'm missing here. Any inputs/comments is truly appreciated.

Thanks,
Satish
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the NullPointerException stacktrace

I am suspecting it might be about "this" in your statement. But not sure, the stacktrace should tell us a lot more.

Thanks

Mark
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thank you for your reply.

I have also tried just passing an empty string (for the alias name) in the setLockMode but still having the same exception-

This is the Stack trace for the nullpointer Exception -

Caused by: java.lang.NullPointerException
at org.hibernate.loader.criteria.CriteriaLoader.applyLocks(CriteriaLoader.java:136)
at org.hibernate.loader.Loader.preprocessSQL(Loader.java:189)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1529)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.bofa.crme.gps.bridge.dao.hibernate.HibernateMutexDao.obtainMutexLock(HibernateMutexDao.java:43)
at com.bofa.crme.gps.bridge.mutex.MutexManagerImpl.obtainMutexForParty(MutexManagerImpl.java:85)
... 32 more
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a setLockMode without an alias parameter, in case you haven't defined one.
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used both the methods in the criteria API (with and without alias) and it results in the same exception.

Thanks,
Satish
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have a solution for this. I have been struggling since 3 days to find an answer for this. Any help on this is truly appreciated.

Please let me know.

Thanks,
Satish
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried other lock modes?
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edvins,

You mean like UPGRADE.NOWAIT? I actually want to do a row lock and as per the hibernate documentation I thought UPGRADE mode would be the best to do a row lock.

Please let me know what other lock modes I can use to obtain a row lock.

Thanks,
Satish
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, try all the lock modes from the list to see which one(s) cause a problem.
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried all the lock modes and the result is same (null pointer exception). I tried debugging in the framework code and found that the Alias name is coming as null and the framework is throwing an error.

In the CriteriaLoader class of the framework -
String[] entityAliases = getAliases();
if ( dialect.forUpdateOfColumns() ) {
keyColumnNames = new HashMap();
for ( int i=0; i<entityAliases.length; i++ ) {
keyColumnNames.put(
entityAliases[i],
persisters[i].getIdentifierColumnNames()
);
}
}

entityAliases is coming as null.

I'm not sure if there was a bug in this framework around this logic? I'm using spring-hibernate3-2.0.1 jar currently in my application.

Thanks,
Satish
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding, from the documentation, is that if you haven't assigned an alias (which I don't see in the code), it is null. In this case, the method without the alias is used, and it sets the lock mode for the current entity.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic