简体   繁体   中英

Grails .save(flush: true) does not save data instantly

Situation :

I send a request using Grails resource and service named firstService;

First request's task is

  1. save a book instance book1 book1.save(flush: true);
  2. at the same time wait 60 sec or a property of book1 is updated by another request by second request

But when I save with book1.save(flush: true); it does not save instantly and thats why withing 60 sec the second request can not find that book1 instance;

Second request's task is only updating data of first request.

How can I solve the problem ?

book1.save() do not returns true , returns itself (book1) or returns null when errors.

There are four ways of to tell if object can be saved

  • validate method before save. It should to return true
  • hasErrors property before save. It should to return false
  • save method returns the instance if all is ok
  • save method with (failOnError: true) argument raise an error when validation fails

If you're totally certain that the object was saved then check this:

  • Are you saving the object in a test context? Tests cleans the database and you must to create the object in each or to use setup method to create it
  • Are you saving the object in a service? Services are transactional, thus if an error raise after the save then all the transaction is rollback.

Note: Every Grails developer must to read the @pledbrook articles GORM Gotchas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM