KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > profile > SqlUtilProfiler


1 package jodd.db.profile;
2
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5
6 import jodd.db.SqlUtil;
7 import jodd.db.pool.ConnectionPool;
8
9 /**
10  * Profiles database access by measuring <code>executeQuery</code> and
11  * <code>executeUpdate</code> execution times. After measuring, a callback
12  * method in <code>SqlProfilerHandler</code> is called with profile data.
13  * <p>
14  *
15  * Usage is quite simple: instead of <code>SqlUtil</code> use
16  * <code>SqlUtilProfiler</code>. Before first usage, usually during
17  * initialization, assign handler field to an instance of the
18  * <code>SqlProfilerHandler</code>.
19  *
20  * <p>Note: java System.currentTimeMillis() is not very precise, so
21  * as query execution is longer, the duration is more precise, and the
22  * error is lower.
23  *
24  * @see jodd.db.SqlUtil
25  */

26 public class SqlUtilProfiler extends SqlUtil {
27
28     /**
29      * Holds current profile handler. Must be set before first usage.
30      */

31     public static SqlProfilerHandler handler = null;
32
33     /**
34      * Overloaded constructor.
35      *
36      * @param cp
37      */

38     public SqlUtilProfiler(ConnectionPool cp) {
39         super(cp);
40     }
41
42     /**
43      * Executes query and measures time.
44      *
45      * @return sql query result
46      * @exception SQLException
47      */

48     public ResultSet executeQuery() throws SQLException {
49         if (handler == null) {
50             return super.executeQuery();
51         }
52         SqlProfileData spdata = new SqlProfileData();
53         spdata.setSqlQuery(this.toString());
54         long start = System.currentTimeMillis();
55         ResultSet rs = null;
56         try {
57             spdata.setQueryTime(System.currentTimeMillis());
58             rs = super.executeQuery();
59             spdata.setDuration(System.currentTimeMillis() - start);
60         } catch (SQLException sex) {
61             spdata.setDuration(System.currentTimeMillis() - start);
62             throw sex;
63         } finally {
64             handler.onExecuteQuery(spdata);
65         }
66         return rs;
67     }
68
69     /**
70      * Executes update and measures time.
71      *
72      * @return sql update result
73      * @exception SQLException
74      */

75     public int executeUpdate() throws SQLException {
76         if (handler == null) {
77             return super.executeUpdate();
78         }
79         SqlProfileData spdata = new SqlProfileData();
80         spdata.setSqlQuery(this.toString());
81         long start = System.currentTimeMillis();
82         int r = 0;
83         try {
84             spdata.setQueryTime(System.currentTimeMillis());
85             r = super.executeUpdate();
86             spdata.setDuration(System.currentTimeMillis() - start);
87         } catch (SQLException sex) {
88             spdata.setDuration(System.currentTimeMillis() - start);
89             throw sex;
90         } finally {
91             handler.onExecuteUpdate(spdata);
92         }
93         return r;
94     }
95
96
97 }
98
Popular Tags