Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Locking not working
PostPosted: Wed Mar 21, 2012 3:06 pm 
Newbie

Joined: Mon Mar 12, 2012 1:38 pm
Posts: 4
hi Team,
I need to lock a row of table and others shouldnt update on the row.until the lock is released
I have used the following query for locking ,but its not working(others able to update even when locked")pl help me how to solve this?? and any changes to be added in hibernate configuration files
Also tell me how to unlock after the operation is compleete?
how to release all the locks on startup ???
Thanks in adv folks

/**
* Locking the Job row for jobAlias
*
* @author Sravanthi
* @param jobAlias
*
* @return
*/

public boolean lockJobRow(String jobalias) {
String jobAlias = jobalias;
JobLock jobLock=null;Job job=null;
boolean rowLocked=true;
Date date= new Date();
//check for existing status in joblock row
try{
Session session = PersistenceSessionFactory.getCurrentSession();
String query = "from JobLock jl where jl.pk.jobAlias2=:jobalias";
Query queryResult = session.createQuery(query).setParameter("jobalias",
jobAlias);

queryResult.list();
System.out.println("size of querylist is"+queryResult.list().size());
if (queryResult.list().size() != 0){
jobLock = (JobLock) queryResult.list().get(0);
if(jobLock.getJobLockStatus().equalsIgnoreCase("LOCKED")){
rowLocked=false;
}

else{
Criteria criteria = session.createCriteria(Job.class);
criteria.add(Restrictions.eq("jobAlias", jobAlias));
criteria.setLockMode("this", LockMode.UPGRADE);
List list = criteria.list();
System.out.println("size of JobLockRow--->" + list.size());
//setting the status in jobLock row to LOCKED
job=(Job)findById(Job.class,jobAlias);
jobLock.setJobLockStatus("LOCKED");
jobLock.setLastLockedTs((new Timestamp(date.getTime())));
jobLock.setLastLockedUserid(job.getUserid());
jobLock.setDtupdate((new Timestamp(date.getTime())));
update(jobLock);
}

}
}catch(Exception e){
e.printStackTrace();
}
return rowLocked;
}

//unlocking row
* UN Locking the Job row for jobAlias
*
* @author Sravanthi
* @param jobAlias
*
* @return
*/

public void unLockJobRow(String jobalias) {
String jobAlias = jobalias;
JobLock jobLock=null;
Job job=null;
Date date = new Date();
try{
Session session = PersistenceSessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Job.class);
criteria.add(Restrictions.eq("jobAlias", jobAlias));
criteria.setLockMode("this", LockMode.NONE);
List list = criteria.list();
System.out.println("size of JobLockRow--->" + list.size());
//setting the status in JobLock table to UNLOCKED
String query = "from JobLock jl where jl.pk.jobAlias2=:jobalias";
Query queryResult = session.createQuery(query).setParameter("jobalias",
jobAlias);

queryResult.list();
System.out.println("size of querylist is"+queryResult.list().size());
if (queryResult.list().size() != 0){
jobLock = (JobLock) queryResult.list().get(0);
String joblst=jobLock.getJobLockStatus().toString();
System.out.println("joblock status is"+joblst);
if(joblst.equalsIgnoreCase("LOCKED")){
jobLock.setJobLockStatus("UNLOCKED");
jobLock.setLastUnlockedTs((new Timestamp(date.getTime())));
job=(Job)findById(Job.class,jobAlias);
jobLock.setLastUnlockedUserid(job.getUserid());
jobLock.setDtupdate((new Timestamp(date.getTime())));
update(jobLock);

}


}
}catch(Exception e){
e.printStackTrace();
}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.