1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.event.def;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.hibernate.FlushMode;
30 import org.hibernate.HibernateException;
31 import org.hibernate.event.AutoFlushEvent;
32 import org.hibernate.event.AutoFlushEventListener;
33 import org.hibernate.event.EventSource;
34
35 /**
36 * Defines the default flush event listeners used by hibernate for
37 * flushing session state in response to generated auto-flush events.
38 *
39 * @author Steve Ebersole
40 */
41 public class DefaultAutoFlushEventListener extends AbstractFlushingEventListener implements AutoFlushEventListener {
42
43 private static final Logger log = LoggerFactory.getLogger(DefaultAutoFlushEventListener.class);
44
45 /** Handle the given auto-flush event.
46 *
47 * @param event The auto-flush event to be handled.
48 * @throws HibernateException
49 */
50 public void onAutoFlush(AutoFlushEvent event) throws HibernateException {
51
52 final EventSource source = event.getSession();
53
54 if ( flushMightBeNeeded(source) ) {
55
56 final int oldSize = source.getActionQueue().numberOfCollectionRemovals();
57
58 flushEverythingToExecutions(event);
59
60 if ( flushIsReallyNeeded(event, source) ) {
61
62 log.trace("Need to execute flush");
63
64 performExecutions(source);
65 postFlush(source);
66 // note: performExecutions() clears all collectionXxxxtion
67 // collections (the collection actions) in the session
68
69 if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
70 source.getFactory().getStatisticsImplementor().flush();
71 }
72
73 }
74 else {
75
76 log.trace("Dont need to execute flush");
77 source.getActionQueue().clearFromFlushNeededCheck( oldSize );
78 }
79
80 event.setFlushRequired( flushIsReallyNeeded( event, source ) );
81
82 }
83
84 }
85
86 private boolean flushIsReallyNeeded(AutoFlushEvent event, final EventSource source) {
87 return source.getActionQueue()
88 .areTablesToBeUpdated( event.getQuerySpaces() ) ||
89 source.getFlushMode()==FlushMode.ALWAYS;
90 }
91
92 private boolean flushMightBeNeeded(final EventSource source) {
93 return !source.getFlushMode().lessThan(FlushMode.AUTO) &&
94 source.getDontFlushFromFind() == 0 &&
95 source.getPersistenceContext().hasNonReadOnlyEntities();
96 }
97
98 }