01: //$Id: DefaultAutoFlushEventListener.java 7785 2005-08-08 23:24:44Z oneovthafew $
02: package org.hibernate.event.def;
03:
04: import org.apache.commons.logging.Log;
05: import org.apache.commons.logging.LogFactory;
06: import org.hibernate.FlushMode;
07: import org.hibernate.HibernateException;
08: import org.hibernate.event.AutoFlushEvent;
09: 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
19: AbstractFlushingEventListener implements AutoFlushEventListener {
20:
21: private static final Log log = LogFactory
22: .getLog(DefaultAutoFlushEventListener.class);
23:
24: /** Handle the given auto-flush event.
25: *
26: * @param event The auto-flush event to be handled.
27: * @throws HibernateException
28: */
29: public void onAutoFlush(AutoFlushEvent event)
30: throws HibernateException {
31:
32: final EventSource source = event.getSession();
33:
34: if (flushMightBeNeeded(source)) {
35:
36: final int oldSize = source.getActionQueue()
37: .numberOfCollectionRemovals();
38:
39: flushEverythingToExecutions(event);
40:
41: if (flushIsReallyNeeded(event, source)) {
42:
43: log.trace("Need to execute flush");
44:
45: performExecutions(source);
46: postFlush(source);
47: // note: performExecutions() clears all collectionXxxxtion
48: // collections (the collection actions) in the session
49:
50: if (source.getFactory().getStatistics()
51: .isStatisticsEnabled()) {
52: source.getFactory().getStatisticsImplementor()
53: .flush();
54: }
55:
56: } else {
57:
58: log.trace("Dont need to execute flush");
59: source.getActionQueue().clearFromFlushNeededCheck(
60: oldSize);
61: }
62:
63: event.setFlushRequired(flushIsReallyNeeded(event, source));
64:
65: }
66:
67: }
68:
69: private boolean flushIsReallyNeeded(AutoFlushEvent event,
70: final EventSource source) {
71: return source.getActionQueue().areTablesToBeUpdated(
72: event.getQuerySpaces())
73: || source.getFlushMode() == FlushMode.ALWAYS;
74: }
75:
76: private boolean flushMightBeNeeded(final EventSource source) {
77: return !source.getFlushMode().lessThan(FlushMode.AUTO)
78: && source.getDontFlushFromFind() == 0
79: && source.getPersistenceContext()
80: .hasNonReadOnlyEntities();
81: }
82:
83: }
|