Friday, March 18, 2011

Hibernate Cascading on flushing




this snapshot (taken with YourKit profiler) shows the tremendous performance impact of cascading the flush to dependent objects - even if nothing has to be written to the DB.

Basically if you have
Employee {
String name;
Company company;
}

and you update/flush Employee, the blessed hibernate will try to flush also Company; And if Company contains other dependent objects, the process will go on forever and eat all your CPU.

CascadeType should be NONE by default.

As you can see here
http://www.docjar.com/docs/api/org/hibernate/event/def/AbstractFlushingEventListener.html

there are quite a lot of operations involved when persisting stuff.


So the message is: avoid as much as you can doing "find" operation in a transaction where you do updates (this will autoflush at every find). Also avoid cascading operations on dependent objects.

No comments: