Friday, July 19, 2013

[Grails] Use Domain.withNewSession instead of refresh()

Waaaaaaay faster.
For instance, if you have a service run by a scheduled recurring Quartz job, the service seem to be attached to a thread which does not see the updated fields in the database (always returns cached values). We may refresh the whole domain calling the refresh() method on the domain class instance, or just do this:

def allCampaigns
        Campaign.withNewSession{ session ->
            allCampaigns = Campaign.findAll("from Campaign where myStatus IN ('active','paused') OR myStatus = null")
        }