java - Spring boot and Hibernate Transaction not working as desired -
i have spring boot application hibernate on mysql, , need use pessimistic locking logic services.
i have scheduled job should update objects, happens following iteration doesn't take previous account, want put pessimistic logic service.
so, scheduled task
@scheduled(fixeddelay=3000, initialdelay=5000) public void checkswitchtoshutdown(){ // logger.debug("scheduled partito :"+ dateformat.format(new date())); list<interruttore> interruttori = interruttoreservice.findallswitches(); (int = 0; < interruttori.size(); i++){ interruttore interruttore = interruttori.get(i); interruttoreservice.toggleswitchnew(interruttore); } }
interruttoreservice.toggleswitchnew
on concrete implementation of service layer. implementation annotated @transactional
the service method is:
@override @transactional(isolation = isolation.serializable, readonly = false) public void toggleswitchnew(interruttore interruttore) { date nexttimeout = interruttore.gettimeoutdate(); date date = new date(); date = new timestamp(date.gettime()); if (nexttimeout == null) { nexttimeout = now; } boolean on = interruttore.isstato(); string pinname = interruttore.getpinname(); if ((now.after(nexttimeout) && on) || (!on && nexttimeout.after(now))) { try { logger.debug("status @ beginning " + on); interruttore.setdatetime(now); interruttore.setstato(!on); updateinterruttore(interruttore); logger.debug("updated status " + interruttore.isstato()); gpiopindigitaloutput relepin = interruttore.getgpio() .provisiondigitaloutputpin(raspipin.getpinbyname(pinname)); relepin.high(); try { thread.sleep(300); } catch (interruptedexception e) { logger.error("errore sleep ", e); } relepin.low(); interruttore.getgpio().unprovisionpin(relepin); } catch (gpiopinexistsexception ge) { logger.error("gpio giĆ esistente"); } catch (throwable t) { logger.error("pessimistic", t); } } }
updateinterruttore
is
@override @transactional(readonly = false, isolation = isolation.serializable) public void updateinterruttore(interruttore interruttore) { try { interruttore entity = dao.findbyid(interruttore.getidinterruttore()); if (entity != null) { entity.setstato(interruttore.isstato()); entity.setdatetime(interruttore.getdatetime()); entity.settimeoutdate(interruttore.gettimeoutdate()); } } catch (throwable t) { logger.debug("error: ",t); } }
now happens 2 consecutive steps of scheduled task go toggleswitchnew
, if first has changed status of switch true, second value false, transaction not being locked...
for instance log of happens:
2016-11-08 13:03:03 debug interruttoreserviceimpl:173 [pool-2-thread-1] - status @ beginning false 2016-11-08 13:03:03 debug interruttoreserviceimpl:177 [pool-2-thread-1] - updated status true 2016-11-08 13:03:03 debug interruttoreserviceimpl:183 [pool-2-thread-1] - l'interruttore si deve accendere: zona -4b 2016-11-08 13:03:07 debug interruttoreserviceimpl:173 [pool-2-thread-1] - status @ beginning false 2016-11-08 13:03:07 debug interruttoreserviceimpl:177 [pool-2-thread-1] - updated status true 2016-11-08 13:03:07 debug interruttoreserviceimpl:183 [pool-2-thread-1] - l'interruttore si deve accendere: zona -4b
Comments
Post a Comment