0

I have already seen several threads for this issue and none could rescue I have the following in my DomainClass

def afterInsert() {     
        elasticSearchService.index(this)
}

Where elasticsaerch is a service and I have added it to the static transient list. It seems that after calling the index method successfully it throws this exception

Message: null id in com.easytha.Student entry (don't flush the Session after an exception occurs)

This is the code of index method

def  index(object) {
    try {
        if(object==null) {
            throw new NullPointerException()
        }
        if(!(object instanceof Collection || object instanceof Object[])) {
            IndexResponse response = client.prepareIndex(grailsApplication.config.esIndexName, object.getClass().getName(),object.id.toString())
                    .setSource((object as JSON).toString() )
                    .execute().actionGet()
            println "object indexed successfully" 
        }else if(object instanceof Collection || object instanceof Object[]) {
            for (var in object) {
                index(var)
            }
        }
    }catch(e) {
        e.printStackTrace();
    }
}

"object indexed successfully" is printed in the console.

The bootstrap.groovy has the following

Student student4 = new Student(firstName:'Sapan',lastName:'Parikh',email:'sapan.parikh@eclinicalworks.com',password:'test123')
    student4.save(failOnError : true)

UPDATE

I tried Student.withNewSession { elasticSearchService.index(this) } which worked.

Sap
  • 5,197
  • 8
  • 59
  • 101
  • what are you doing in your Bootstrap.groovy? you dint give that code here – Motilal Jan 08 '14 at 10:40
  • @Motilal Added the bootstrap code, it's a simple save operation. Also there are no validation errors as well, I checked them by printing this.errors – Sap Jan 08 '14 at 10:42
  • add flush to your save () and try re-boot your app student4.save(flush:true, failOnError : true) – Motilal Jan 08 '14 at 10:46
  • @Motilal nope still the same error. – Sap Jan 08 '14 at 10:49
  • [This question](http://stackoverflow.com/questions/5298825/save-method-doesnt-flush-the-session-after-an-exception-occurs) might give you some ideas. – Hernán Erasmo Jan 08 '14 at 11:20

1 Answers1

2

It's stabbing at things but maybe shift the save to happening within a transaction:

Student.withTransaction {
  student4.save()
}

I've seen this pop up unexpectedly when doing things outside of services (that should, imo, be in services).

Summing up some subsequent discussion:

The student model was saved throughout the application, so it wasn't suitable to shift all the saves to services or wrap in transaction blocks. The OP notes that moving the original reindexing code into a new session fixed it everywhere.

def afterInsert() {     
    elasticSearchService.index(this)

}

Joe Rinehart
  • 773
  • 5
  • 8
  • Yes you are right, this solved the bootstrap issue but student.save is sprinkled all over the code base. Will I have to change it everywhere? – Sap Jan 08 '14 at 11:45
  • 1
    It worked with Student.withNewSession { elasticSearchService.index(this) } – Sap Jan 08 '14 at 12:26
  • That's probably going to solve an issue I have in some GPars code, thanks! – Joe Rinehart Jan 08 '14 at 12:59
  • Do you want to change your answer and I can accept it. It will be helpful for others? – Sap Jan 08 '14 at 13:00