-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: onFlushDirty method of Interceptor is called too many times
PostPosted: Wed Feb 08, 2006 10:11 pm 
Newbie

Joined: Sat Nov 19, 2005 10:03 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

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.HibernateTransactionManager. 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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 11:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
onFlushDirty is called once per interceptor, every time a dirty object is flushed. If there are hundreds of dirty objects in your session, you'll get hundreds of onFlushDirty calls.


Top
 Profile  
 
 Post subject: sorry,but i don't think I made myself clear
PostPosted: Wed Feb 08, 2006 11:56 pm 
Newbie

Joined: Sat Nov 19, 2005 10:03 pm
Posts: 3
tenwit wrote:
onFlushDirty is called once per interceptor, every time a dirty object is flushed. If there are hundreds of dirty objects in your session, you'll get hundreds of onFlushDirty calls.


IMHO,the onFlushDirty method is called when a method-spanned transaction is over and hibernate is going to flush all the changed data to database.if onflushDirty returns true,the dirty properties will be recalcaulated. but I neend the more details to know what caused onFlushDrity called many times on the same database result,and how can I avoid it from being called too many times.
e.g.

an OrderVO has many-to-many relation to OrderItemVO.
public class BO{

public Session openSession() {
return SessionFactoryUtils.getSession(getSessionFactory(), false);
}
public void bizProc(Long id){
a(id);
b(id);
}
public void a(Long id){
OrderVO obj=(OrderVO)openSession().load(OrderVO.class,id);
obj.setXXX();
Iterator it=obj.getOrderItems().iterator();
while (it.hasNext()) {
OrderItemVO item=(OrderItemVO )it.next();
item.setYYY();
}
}
public void b(Long id){
OrderVO obj=(OrderVO)openSession().load(OrderVO.class,id);
obj.setXXX();
Iterator it=obj.getOrderItems().iterator();
while (it.hasNext()) {
OrderItemVO item=(OrderItemVO )it.next();
item.setYYY();
}
}
}

regard to the sample code above, when the bizProc(id) is executed and transaction is commited, a lot of calls is made to onFlushDirty method which the first parameter of it is an OrderVO entity,and so I got a lot of history objects inserted into the history table.
btw,I use open session in view pattern.

what's wrong? and can I do something to avoid?
Thanks again.

Best Regards,

Steven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 12:32 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If it's the same OrderVO entity every time (the second parameter to onFlushDirty is the same each time), then there's something wrong. All I can suggest is that you put a breakpoint in your onFlushDirty method and have a look at the stack trace. If you can't find anything, have a look in JIRA: perhaps the development team are already aware of this.


Top
 Profile  
 
 Post subject: hi,I have found some clue
PostPosted: Sun Feb 12, 2006 5:54 am 
Newbie

Joined: Sat Nov 19, 2005 10:03 pm
Posts: 3
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.