KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > event > def > DefaultAutoFlushEventListener


1 //$Id: DefaultAutoFlushEventListener.java,v 1.4 2005/05/27 03:53:58 oneovthafew Exp $
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 boolean onAutoFlush(AutoFlushEvent event) throws HibernateException {
28
29         final EventSource source = event.getSession();
30         
31         final boolean flushMightBeNeeded = !source.getFlushMode().lessThan(FlushMode.AUTO) &&
32             source.getDontFlushFromFind() == 0 &&
33             source.getPersistenceContext().hasNonReadOnlyEntities();
34         
35         if (flushMightBeNeeded) {
36
37             final int oldSize = source.getActionQueue().numberOfCollectionRemovals();
38
39             flushEverythingToExecutions(event);
40             
41             final boolean flushIsReallyNeeded = source.getActionQueue()
42                     .areTablesToBeUpdated( event.getQuerySpaces() ) ||
43                             source.getFlushMode()==FlushMode.ALWAYS;
44
45             if (flushIsReallyNeeded) {
46
47                 log.trace("Need to execute flush");
48
49                 performExecutions(source);
50                 postFlush(source);
51                 // note: performExecutions() clears all collectionXxxxtion
52
// collections (the collection actions) in the session
53

54                 if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
55                     source.getFactory().getStatisticsImplementor().flush();
56                 }
57                 
58             }
59             else {
60
61                 log.trace("Dont need to execute flush");
62                 source.getActionQueue().clearFromFlushNeededCheck( oldSize );
63             }
64             
65             return flushIsReallyNeeded;
66
67         }
68
69         return false;
70
71     }
72
73 }
74
Popular Tags