user5441083
user5441083

Reputation:

data not saving in after Update grails and stackoverflow error?

i m having a very weird issue, i m using grails afterUpdate in domain for saving the activity on db.

The code i m using to create a record in my collection "DatabaseEvent" is working in controller and saving the object perfectly but the same code is not saving object in afterUpdate() method ,

even the code is executing and not giving any error but still it is not saving the object

def afterUpdate () {
    println "==++++==="
    def dbEvent = new DatabaseEvent(type: "Created", entityClass : "Central Zone", objectId: this.id )
    if(!dbEvent.save()){
        println "======"
        dbEvent.errors.each{
            println it
        }
    }
    else{
        println "saved=="
    }
}

it does print out "==++++===" and goes in else and print saved== but the object is not saved in db, i m using mongodb

and sometimes it gives , the stackoverflow error and creates a lot of entries in just one call, i dont understand it, any solution for this ??

i have edited the question, as one i thing i noticed it is giving me stackoverflow when i use save(flush :true) and created a lots of records in one call and if i dont use it , it just dont create any ??

Upvotes: 0

Views: 299

Answers (1)

roanjain
roanjain

Reputation: 1252

Transaction in service layer is coupled with the datasource associated, here you need to initiate a transaction to save the data like this

def afterUpdate () {
println "==++++==="
   DatabaseEvent.withTransaction { status->
   def dbEvent = new DatabaseEvent(type: "Created", entityClass : "Central Zone", objectId: this.id )
if(!dbEvent.save()){
    println "======"
    dbEvent.errors.each{
        println it
    }
}
else{
    println "saved=="
}
}
}

Upvotes: 1

Related Questions