2

My Application uses FlushMode.AUTO . For a particular service method call I want to change Hibernate Session.FlushMode to FlushMode.COMMIT and revert back to FlushMode.AUTO when the method completes.

Question:- Are there any problem/dangers of changing FlushMode during the session?

Reason for changing FlushMode during a session:- I am using Hibernate Interceptor Approach (onFlushDirty) for auditing changes. With FlushMode.AUTO ,Multiple Session Flushes are occuring(behaviour of FlushMode.AUTO) . So consequently onFlushDirty is being invoked multiple times leading to duplicate audit. I workaround above issue by changing FlushMode to COMMIT in the method where i am expecting auditing to happen.

harrybvp
  • 2,445
  • 2
  • 22
  • 30

1 Answers1

0

The most common reason for triggering an auto flush is when use execute a query during the course of your execution. If Hibernate detects that some of your non-flushed changes in your session could affect the outcome of your query, then it automatically triggers a flush. So when your query executes it gets data that is consistent with the changes you have made in the session.

So, it depends on what your code does. If this behavior does not impact the functionality then it should be ok.

codedabbler
  • 1,231
  • 7
  • 13