Sunday, December 9, 2012

Don't flush a session after an exception occurs

Few days ago, the team and I was running a performance test using JMeter, and found this error happen during the test. I was seeing this error from the log:

ERROR 2012-12-06 16:00:43,721 [ModuleABoImpl:2626]- [Unable to Finish Calculation for record ID 11223344]
    org.hibernate.AssertionFailure: null id in org.huahsin.moduleA.model.PojoA entry (don't flush the Session after an exception occurs)

...
...
[ERROR   ] Non-atomic batch failure.  The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.
[ERROR   ] Error for batch element #0: One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "TableSpaceA.ModuleATable" from having duplicate values for the index key
...
...
[ERROR   ] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)


It seems that the web app not able to handle concurrent access on the data, and took us very long hours effort to figure out the root cause. We revert the code, do a few round testing, and we found that the particular table, ModuleATable must have a primary key column define in the table. This column doesn't serve any purpose but just to resolve this problem.

Initially I was though that Hibernate have cause the problem, but eventually it is not. Experience learned is that I need to think out from a box every time looking at a problem.

No comments: