~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/event/def/DefaultAutoFlushEventListener.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: DefaultAutoFlushEventListener.java 7785 2005-08-08 23:24:44Z oneovthafew $
 
2
package org.hibernate.event.def;
 
3
 
 
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;
 
11
 
 
12
/**
 
13
 * Defines the default flush event listeners used by hibernate for 
 
14
 * flushing session state in response to generated auto-flush events.
 
15
 *
 
16
 * @author Steve Ebersole
 
17
 */
 
18
public class DefaultAutoFlushEventListener extends AbstractFlushingEventListener implements AutoFlushEventListener {
 
19
 
 
20
        private static final Log log = LogFactory.getLog(DefaultAutoFlushEventListener.class);
 
21
 
 
22
    /** Handle the given auto-flush event.
 
23
     *
 
24
     * @param event The auto-flush event to be handled.
 
25
     * @throws HibernateException
 
26
     */
 
27
        public void onAutoFlush(AutoFlushEvent event) throws HibernateException {
 
28
 
 
29
                final EventSource source = event.getSession();
 
30
                
 
31
                if ( flushMightBeNeeded(source) ) {
 
32
 
 
33
                        final int oldSize = source.getActionQueue().numberOfCollectionRemovals();
 
34
 
 
35
                        flushEverythingToExecutions(event);
 
36
                        
 
37
                        if ( flushIsReallyNeeded(event, source) ) {
 
38
 
 
39
                                log.trace("Need to execute flush");
 
40
 
 
41
                                performExecutions(source);
 
42
                                postFlush(source);
 
43
                                // note: performExecutions() clears all collectionXxxxtion 
 
44
                                // collections (the collection actions) in the session
 
45
 
 
46
                                if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
 
47
                                        source.getFactory().getStatisticsImplementor().flush();
 
48
                                }
 
49
                                
 
50
                        }
 
51
                        else {
 
52
 
 
53
                                log.trace("Dont need to execute flush");
 
54
                                source.getActionQueue().clearFromFlushNeededCheck( oldSize );
 
55
                        }
 
56
                        
 
57
                        event.setFlushRequired( flushIsReallyNeeded( event, source ) );
 
58
 
 
59
                }
 
60
 
 
61
        }
 
62
 
 
63
        private boolean flushIsReallyNeeded(AutoFlushEvent event, final EventSource source) {
 
64
                return source.getActionQueue()
 
65
                                .areTablesToBeUpdated( event.getQuerySpaces() ) || 
 
66
                                                source.getFlushMode()==FlushMode.ALWAYS;
 
67
        }
 
68
 
 
69
        private boolean flushMightBeNeeded(final EventSource source) {
 
70
                return !source.getFlushMode().lessThan(FlushMode.AUTO) && 
 
71
                                source.getDontFlushFromFind() == 0 &&
 
72
                                source.getPersistenceContext().hasNonReadOnlyEntities();
 
73
        }
 
74
 
 
75
}