Announcement Announcement Module
Collapse
No announcement yet.
spring + hibernate + AuditInterceptor ,onFlushDirty is called too many times! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring + hibernate + AuditInterceptor ,onFlushDirty is called too many times!

    http://forum.hibernate.org/viewtopic.php?t=955313

    Hibernate version:
    3.1. (in the manifest.mf)
    Spring-Version: 1.2.6
    Mapping documents:
    not needed
    Code between sessionFactory.openSession() and session.close():

    Full stack trace of any exception that occurs:

    Name and version of the database you are using:
    oracle 9.2.0.1
    The generated SQL (show_sql=true):

    Debug level Hibernate log excerpt:

    Here is my problem.

    I have a sessionfactory scope AuditInterceptor that is supposed to log the modification information like last modifier,last modify time,which business property is changed,and if any business property is changed,the previous status(i.e.the old object in the session or databasesnapshot) is inserted into a history table. All this is done in onFlushDirty method.

    to make hibernate recalculating the dirty properties,onFlushDirty returns true.But what surprising me is that the onFlushDirty method is called too many times in a transaction which is managed by org.springframework.orm.hibernate3.HibernateTransa ctionManager. Since the transaction spanned several method and the update to the object is also scattered,the onFlushDrity method may be called once when the method is end(but the transaction is not!),but I found hundreds of them!
    btw,I have another sessionfactory to manage the objects that should log history versions of every change. e.g. the OrderVO have 2 mappings ,one for business use and the other for log history versions,configured in another sessionfacotry.
    In a word,when the onFlushDirty is called in spring+hibernate and what it can do and can not do?
    any comments is welcome,thanks!
    and if any other infomation is needed ,I will reply ASAP.

    Best Regards,

    Steven Lee

  • #2
    onFlushDirty can be called more then one time inside a transaction - the syncrhonization with the database depends on the FlushMode set. Here is a snippet from the javadocs:

    Called when an object is detected to be dirty, during a flush. The interceptor may modify the detected currentState, which will be propagated to both the database and the persistent object. Note that not all flushes end in actual synchronization with the database, in which case the new currentState will be propagated to the object, but not necessarily (immediately) to the database. It is strongly recommended that the interceptor not modify the previousState.
    You are better off by using the preFlush method or the listeners from the org.hibernate.event package.

    Comment


    • #3
      I am going to solve the problem,and need your help,thanks!

      hi,all,

      the problem relies on the call of

      SessionFactoryUtils.getSession(getSessionFactory() ,false);

      after setting the flushmode of the returned session to COMMIT(OR NEVER).It seems that it solved the problem.But I am not sure it is the right approach.

      I mean,in the open session in view pattern, I used hibernatetemplate and
      session in the single request,what flushmod should I set when I get sesssion by SessionFactoryUtils.getSession(getSessionFactory() ,false)?
      and I have a Interceptor,where to set it?

      Thanks a lot!

      Comment


      • #4
        mean,in the open session in view pattern, I used hibernatetemplate and
        session in the single request,what flushmod should I set when I get sesssion by SessionFactoryUtils.getSession(getSessionFactory() ,false)?
        and I have a Interceptor,where to set it?
        Search the forum - there have been several posts on this topic including configuration with OpenSessionInViewFilter or OpenSessionInViewInterceptor.

        Comment

        Working...
        X