grailsgrails-services

Delete records in Grails service


In Grails Service, I have to delete records from the Db, but I get below error :

spi.SqlExceptionHelper, Connection is read-only. Queries leading to data modification are not allowed.

Although, there is @Transactional(readOnly = false) in my service, here is the delete part in my service code:

def adsDurationIs7 = null
adsDurationIs7 = Ads.findAllByDuration("7 days", [sort: "dateCreated", order: "asc"])
adsDurationIs7.each {
   Ads.get(it.id).delete(flush: true)
}

Solution

  • You are executing this service function from controller's function, which is not transactional. Add @Transactional to controller's function.

    Here is an example: https://stackoverflow.com/a/21998182/2166188