001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.jdbc.listeners;
017
018 import org.kuali.common.jdbc.model.event.SqlEvent;
019 import org.kuali.common.jdbc.model.event.SqlExecutionEvent;
020 import org.kuali.common.util.Assert;
021 import org.kuali.common.util.inform.PercentCompleteInformer;
022
023 /**
024 * Thread safe tracking of SQL execution related statistics
025 */
026 public final class ThreadSafeListener extends NoOpSqlListener {
027
028 public ThreadSafeListener(PercentCompleteInformer informer, boolean trackProgressByUpdateCount) {
029 Assert.noNulls(informer);
030 this.informer = informer;
031 this.trackProgressByUpdateCount = trackProgressByUpdateCount;
032 }
033
034 private final PercentCompleteInformer informer;
035 private final boolean trackProgressByUpdateCount;
036
037 private volatile long aggregateTime;
038 private volatile long aggregateUpdateCount;
039 private volatile long aggregateSqlCount;
040 private volatile long aggregateSqlSize;
041
042 @Override
043 public synchronized void afterExecution(SqlExecutionEvent event) {
044 this.aggregateTime += event.getStopTimeMillis() - event.getStartTimeMillis();
045 }
046
047 @Override
048 public synchronized void afterExecuteSql(SqlEvent event) {
049 this.aggregateUpdateCount += event.getUpdateCount();
050 this.aggregateSqlCount++;
051 this.aggregateSqlSize += event.getSql().length();
052 if (trackProgressByUpdateCount) {
053 informer.incrementProgress(event.getUpdateCount());
054 } else {
055 informer.incrementProgress();
056 }
057 }
058
059 public long getAggregateTime() {
060 return aggregateTime;
061 }
062
063 public long getAggregateUpdateCount() {
064 return aggregateUpdateCount;
065 }
066
067 public PercentCompleteInformer getInformer() {
068 return informer;
069 }
070
071 public boolean isTrackProgressByUpdateCount() {
072 return trackProgressByUpdateCount;
073 }
074
075 public long getAggregateSqlCount() {
076 return aggregateSqlCount;
077 }
078
079 public long getAggregateSqlSize() {
080 return aggregateSqlSize;
081 }
082 }