-->
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: Help with lock
PostPosted: Sun May 17, 2015 10:57 am 
Newbie

Joined: Tue Oct 31, 2006 2:44 pm
Posts: 10
I'm trynig to do one lock with no succes,

i have one table named 'TbSelo' so i have a lot of records here, and i have one service that my user can reserve one record.
so i do one select to get one or more records unreserved an update that record with informations from this user. but when i have a few user asking for records ano or more receive the same record.
so i try to lock my register just like this:

Code:
   boolean erro = false;
      SeloReservado seloReservado = null;
      do{
         erro = false;
         try{
         session.getTransaction().begin();      
         Criteria criteria = session.createCriteria(TbSelo.class);
         criteria.add(Restrictions.isNull("dtSolicitacao"));      
         criteria.add(Restrictions.isNull("dtUtilizacao"));
         criteria.add(Restrictions.eq("icGratuito", icGratuito));
         criteria.addOrder(Order.asc("cdSelo"));
         criteria.setMaxResults(1);
         TbSelo selo = null;
         selo = (TbSelo) criteria.uniqueResult();
         selo.setNmNaturezaSolicitacao(nmNatureza);
         selo.setNmUsuarioSolicitacao(nmUsuario);
         selo.setDtSolicitacao(new Timestamp(Calendar.getInstance().getTime().getTime()));
         
         session.update(selo);
         session.getTransaction().commit();
         seloReservado = new SeloReservado(selo.getNmSerie(), String.format("%05d", selo.getNrSerie()), icGratuito, selo.getAleatorio());
         }catch (StaleObjectStateException e ){
            erro = true;
            session.getTransaction().rollback();      
         }
         
      }while(erro);



but when i test this code, some users returns correct and from some user i get a infinit loop.

i try to use @Version in my entity, and try to put criteria.setLockMode(LockMode.PESSIMISTIC_WRITE); in my criteria
when i debug the code, I noticed that when the loop back to init the criteria always return the same records regardless of it field are change( in this case that records have dtSolicitacao filled) i try to use session.evict(selo) but dont work

can someone help me? tks

I using Postgresql


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.