View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.monitoring;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import com.jamonapi.Monitor;
23  import com.jamonapi.MonitorFactory;
24  
25  /**
26   * A simple DB-Logger that counts the number of DB roundtrips (single-threaded, i.e. roundtrips are not associated with
27   * executing thread). It is a minimalistic wrapper for {@link MonitorFactory},
28   * so use JAMons MonitorFactory directly if you need more control.
29   *
30   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/database/jamon/src/main/java/ch/elca/el4j/services/monitoring/DbLogger.java $
31   *
32   * @author Stefan Wismer (SWI)
33   */
34  public final class DbLogger {
35  	/**
36  	 * Private logger of this class.
37  	 */
38  	private static Logger s_logger = LoggerFactory.getLogger(DbLogger.class);
39  	
40  	/**
41  	 * The hidden constructor.
42  	 */
43  	private DbLogger() { }
44  	
45  	/**
46  	 * Start monitoring.
47  	 */
48  	public static void enable() {
49  		MonitorFactory.enable();
50  	}
51  	
52  	/**
53  	 * Stop monitoring.
54  	 */
55  	public static void disable() {
56  		MonitorFactory.disable();
57  	}
58  	
59  	/**
60  	 * @return    the number of DB roundtrips detected since monitor has been activated
61  	 *            (which is always done on startup) or last reset. If monitoring is not available <code>-1</code> is
62  	 *            returned.
63  	 */
64  	public static int getRoundtripCount() {
65  		int roundtrips = 0;
66  		if (MonitorFactory.getRootMonitor().getMonitors() != null) {
67  			for (Monitor monitor : MonitorFactory.getRootMonitor().getMonitors()) {
68  				// search for execution of (Prepared)Statements
69  				if (monitor.getLabel().contains("Statement.execute")) {
70  					roundtrips += monitor.getHits();
71  				}
72  			}
73  		} else {
74  			s_logger.warn("JAMon JDBC interceptor not found.");
75  			roundtrips = -1;
76  		}
77  		return roundtrips;
78  	}
79  	
80  	/**
81  	 * Reset monitoring. All acquired values get lost.
82  	 */
83  	public static void reset() {
84  		MonitorFactory.reset();
85  	}
86  }