Note that there are some explanatory texts on larger screens.

plurals
  1. POsave(flush: true) is not flushing the session
    primarykey
    data
    text
    <p>I was creating a new grails app with several domain classes and controllers. All my controllers have working methods for save(), update() and list(), e.g.</p> <pre><code>def update(Long id, Long version) { def crawlerConfigInstance = CrawlerConfig.get(id) if (!crawlerConfigInstance) { flash.message = message(code: 'default.not.found.message', args: [message(code: 'crawlerConfig.label', default: 'CrawlerConfig'), id]) redirect(action: "list") return } if (version != null) { if (crawlerConfigInstance.version &gt; version) { crawlerConfigInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'crawlerConfig.label', default: 'CrawlerConfig')] as Object[], "Another user has updated this CrawlerConfig while you were editing") render(view: "edit", model: [crawlerConfigInstance: crawlerConfigInstance]) return } } crawlerConfigInstance.properties = params if (!crawlerConfigInstance.save(flush: true)) { render(view: "edit", model: [crawlerConfigInstance: crawlerConfigInstance]) return } flash.message = message(code: 'default.updated.message', args: [message(code: 'crawlerConfig.label', default: 'CrawlerConfig'), crawlerConfigInstance.id]) redirect(action: "show", id: crawlerConfigInstance.id) } </code></pre> <p>which is actually an autogenerated / scaffolded method by IDE</p> <p>However, now I was adding an additional controller method which should run through a list of domain objects and update them accordingly. Getting the list, changing the attributes and validation is successfull.</p> <p>save(flush:true) doesn't throw any errors and returns the updated domain object, as expected. However, checking the hibernate SQL statements, all I see is SELECTS, no update. In fact, if I directly call a delete() on the domain object after the save(), I get a concurrent transaction exception on the domain object.</p> <p>My controller method looks like</p> <pre><code>def doSomething() { def results = CrawlerConfig.findAll(bQ) results.each { crawlerConfigInstance -&gt; crawlerConfigInstance.needsReview = true if (!crawlerConfigInstance.save(flush: true)) { render(view: "show", model: [crawlerConfigInstance: crawlerConfigInstance]) return } } redirect(action: "list") } </code></pre> <p>Pretty straightforward. If I check for the dirty fields, needsReview is marked as dirty before the save(). save() updates the version on the object.</p> <p>I currently still use h2:mem as database.</p> <p>For me it looks like the transaction is not finished. But I don't get why or how I can get it to finish.</p> <p>Thx in advance</p> <p><strong>EDIT after POST from Ondrej</strong></p> <p>created a Service (which gets injected and called fine)</p> <pre><code>package lizard </code></pre> <p>import org.springframework.transaction.annotation.Transactional</p> <p>@Transactional class CrawlerConfigPersistenceService {</p> <pre><code>static transactional = true def checkRequirementsForCrawlerConfigList(String query) { def results = CrawlerConfig.findAll(query) results.each { crawlerConfigInstance -&gt; crawlerConfigInstance.needsReview = true if (!crawlerConfigInstance.save(flush: true)) { println crawlerConfigInstance.errors.getAllErrors() return } } } } </code></pre> <p>But this results in exactly the same behaviour, everything looks OK, except that there is no update statement raised by hibernate</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload