-->
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.  [ 6 posts ] 
Author Message
 Post subject: Controlling query order during Flush in Hibernate
PostPosted: Mon May 21, 2007 8:45 am 
Newbie

Joined: Mon May 21, 2007 8:39 am
Posts: 3
I have a transaction that has more than one query stored internally by hibernate. It contains both updates and inserts. When flush() is called at the end of the transaction I want the queries to be executed in the order in which the session.insert() and session.update() were called. Right now Hibernate is collating all inserts and then all updates and flushing it in insert/update order.

Is there any way we could control Hibernate and specify the order in which the queries are executed? Any other way we could handle this scenario?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 8:51 am 
Newbie

Joined: Mon May 21, 2007 3:36 am
Posts: 16
You could manually flush the session from time to time by calling
Code:
session.flush()


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 9:12 am 
Newbie

Joined: Mon May 21, 2007 8:39 am
Posts: 3
Thanks for the reply. This is one option we are currently looking at but this would severely affect the performance, as there would be multiple database calls. We want to avoid this scenario and do the flush only once.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 3:44 pm 
Newbie

Joined: Mon May 21, 2007 3:36 am
Posts: 16
I'm not sure if this really affects performance, because the number of sql statements sent to the database is the same in both cases, as long as you do not change the same objects over and over again.

(Note that hibernate itself by default calls flush every time before executing a query)

Hower, flushing very early may result in early database locks - so the probability of deadlocks may be increased.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 12:06 am 
Newbie

Joined: Mon May 21, 2007 8:39 am
Posts: 3
We would like to do it at transaction level as otherwise Rollbacks would be difficult to keep track off. We read that the default order at flush time is Insert, Updates followed by Deletes for entity level processing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 4:12 pm 
Newbie

Joined: Mon May 21, 2007 3:36 am
Posts: 16
What about using a StatelessSession? Stateless sessions do not cache anything and any insert/update/delete operation is immediately executed as SQL statement.

You can retrieve a stateless session from your SessionFactory:

Code:
StatelessSession session = sessionFactory.openStatelessSession();


But be cautious! Stateless sessions do not provider dirty checking, cascading of entity modifications and they bypass hibernate's event model and interceptors.

See chapter 13.3 of the hibernate documentation:
http://www.hibernate.org/hib_docs/v3/re ... esssession


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.