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(); } }
|