Trebla
Trebla

Reputation: 1172

Grails: Getting/solving "Could not synchronize database state with session"?

The root of this question is, what can cause the failure to synchronize? Is this a generic message that just means "something went wrong" or is there something actually wrong with the session itself? There are a handful of threads with similar titles but none seem to actually address why hibernate is failing to synchronize the session, it's just part of the error message that's ignored by people.

My specific case/details (though it's really tangential to the above question):

During a data import (20-120 minutes) I'm doing a large amount of processing. Persistence doesn't begin until the entire set of domain objects have been built and validated for consistency. Over this time, I build somewhere around 200,000 domain objects. At the end of the process, it loops across them all saving them to the database and (for performance reasons) flushing/clearing the session after every 50 or 100 objects. Once persistence begins, the Domain objects do not get changed.

This is all happening within a single service call, a single transaction. I also cannot reproduce this on my test system, it has only happened in production.

I am using domainObj.save() rather than session.saveOrUpdate(domainObj), the only time I manually touch the session at all is flushing/cleaning it after a set of updates:

def session = sessionFactory.currentSession
session.flush()
session.clear()

This is the place where the exception gets thrown.

Immediately following the failed to sync message is (presumably the result, but maybe relevant to the cause):

Could not execute JDBC batch update; SQL [insert into domainB(field1, field2, etc) values (?, ?, ?)];  
nested exception is org.hibernate.exception.ConstrainViolationException: Could not execute JDBC batch update

I realize this ConstrainViolation (yes, "constrain" not "constraint") appears to be a data error, but the dataset has been working, and without change to either the import file or the code, began throwing this error. It also continues to work on other systems, so I have ruled out data error to a relatively certain degree.

I'm almost positive the same objects are getting saved multiple times due to the nature of the object relationships. That's another spot for performance improvement, but I think is unrelated since once the object gets saved, it should have its ID assigned and being re-saved shouldn't cause an error.

I've digressed into rambling and speculation at this point, I don't want someone to solve my problem for me, but hopefully someone knows something explicitly about synchronizing the session and not "have you made sure you're not inserting duplicate data?" because I am as certain as I can be (iterating over a Hashmap.keySet() using the constrained unique field as the key to lookup the domain object to save).

Upvotes: 0

Views: 3150

Answers (1)

Trebla
Trebla

Reputation: 1172

Unfortunately, I still have no clue how to actually debug "Could not synchronize database state with session" but I was able to eventually solve this problem.

Apparently the Oracle Tablespace used by the database was not set to grow, and it had filled up. Once the tablespace was set to be "extensible", this error disappeared. I very much doubt this would help anyone else that runs into the same problem since the error message doesn't apply, but, you never know.

Upvotes: 2

Related Questions