1
//$Id: DefaultAutoFlushEventListener.java 7785 2005-08-08 23:24:44Z oneovthafew $
2
package org.hibernate.event.def;
4
import org.apache.commons.logging.Log;
5
import org.apache.commons.logging.LogFactory;
6
import org.hibernate.FlushMode;
7
import org.hibernate.HibernateException;
8
import org.hibernate.event.AutoFlushEvent;
9
import org.hibernate.event.AutoFlushEventListener;
10
import org.hibernate.event.EventSource;
13
* Defines the default flush event listeners used by hibernate for
14
* flushing session state in response to generated auto-flush events.
16
* @author Steve Ebersole
18
public class DefaultAutoFlushEventListener extends AbstractFlushingEventListener implements AutoFlushEventListener {
20
private static final Log log = LogFactory.getLog(DefaultAutoFlushEventListener.class);
22
/** Handle the given auto-flush event.
24
* @param event The auto-flush event to be handled.
25
* @throws HibernateException
27
public void onAutoFlush(AutoFlushEvent event) throws HibernateException {
29
final EventSource source = event.getSession();
31
if ( flushMightBeNeeded(source) ) {
33
final int oldSize = source.getActionQueue().numberOfCollectionRemovals();
35
flushEverythingToExecutions(event);
37
if ( flushIsReallyNeeded(event, source) ) {
39
log.trace("Need to execute flush");
41
performExecutions(source);
43
// note: performExecutions() clears all collectionXxxxtion
44
// collections (the collection actions) in the session
46
if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
47
source.getFactory().getStatisticsImplementor().flush();
53
log.trace("Dont need to execute flush");
54
source.getActionQueue().clearFromFlushNeededCheck( oldSize );
57
event.setFlushRequired( flushIsReallyNeeded( event, source ) );
63
private boolean flushIsReallyNeeded(AutoFlushEvent event, final EventSource source) {
64
return source.getActionQueue()
65
.areTablesToBeUpdated( event.getQuerySpaces() ) ||
66
source.getFlushMode()==FlushMode.ALWAYS;
69
private boolean flushMightBeNeeded(final EventSource source) {
70
return !source.getFlushMode().lessThan(FlushMode.AUTO) &&
71
source.getDontFlushFromFind() == 0 &&
72
source.getPersistenceContext().hasNonReadOnlyEntities();