KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > core > PoolSqlMonitor


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22 package uk.org.primrose.pool.core;
23 import java.sql.*;
24
25 /**
26 * This class keeps track of whether SQL objects such as Statement,
27 * CallableStatement, PreparedStatement and ResultSet have been closed
28 * by the client program using the pool. It means that the PoolConnection
29 * can access this class and if the objects have not been closed, then
30 * close them appropriately.<br>
31 * It also means that sql statements can be logged correctly, so that during
32 * any potential connection leaks, the exact SQL that the connection is
33 * executing can be seen. With CallablePreparedStatement and PreparedStatement
34 * not only the initial SQL statement is noted, but also the bind variable
35 * values.<br>
36 * This class should not be accessed directly by normal programs.
37 */

38 class PoolSqlMonitor {
39     private Statement statement;
40     private boolean statementClosed = true;
41     private ResultSet resultSet;
42     private boolean resultSetClosed = true;
43     private String JavaDoc sql = "";
44     private String JavaDoc caller = "";
45     private long idleTime = 0L;
46     private long timeInUse = 0L;
47     private int numberOfCalls = 0;
48
49
50     protected void reset() {
51         this.sql = "TRANSITIONAL_CONNECTION_CLOSING";
52         this.caller = "RESET";
53         this.resultSet = null;
54         this.statement = null;
55         this.resultSetClosed = true;
56         this.statementClosed = true;
57         this.idleTime = System.currentTimeMillis();
58         //this.setTimeInUse(0L);
59
this.setTimeInUse(System.currentTimeMillis());
60         this.numberOfCalls++;
61     }
62
63     // Statement accessors/mutators
64

65     protected Statement getStatement() {
66         return statement;
67     }
68
69     protected void setStatement(Statement statement) {
70         this.statement = statement;
71     }
72
73     protected boolean isStatementClosed() {
74         return statementClosed;
75     }
76
77     protected void setStatementClosed(boolean statementClosed) {
78         this.statementClosed = statementClosed;
79     }
80
81     // ResultSet accessors/mutators
82

83     protected ResultSet getResultSet() {
84         return resultSet;
85     }
86
87     protected void setResultSet(ResultSet resultSet) {
88         this.resultSet = resultSet;
89     }
90
91     protected boolean isResultSetClosed() {
92         return resultSetClosed;
93     }
94
95     protected void setResultSetClosed(boolean resultSetClosed) {
96         this.resultSetClosed = resultSetClosed;
97     }
98
99
100     /**
101     * Set SQL associated with this object
102     */

103     protected void setSql(String JavaDoc sql) {
104         //this.setCaller();
105
this.setTimeInUse(System.currentTimeMillis());
106         this.sql = sql;
107     }
108
109     /**
110     * Get SQL associated with this object
111     */

112     protected String JavaDoc getSql() {
113         return this.sql;
114     }
115
116
117
118     protected int getNumberOfCalls() {
119         return numberOfCalls;
120     }
121
122     protected long getTimeInUse() {
123         if (timeInUse == 0L) {
124             //System.err.println("WOULD HAVE RETURNED : " +(System.currentTimeMillis() -timeInUse) +" " +timeInUse);
125
return 0L;
126         } else {
127             return (System.currentTimeMillis() -timeInUse);
128         }
129     }
130
131     protected void setTimeInUse(long timeInUse) {
132         this.timeInUse = timeInUse;
133     }
134
135     protected long getIdleTime() {
136         return (System.currentTimeMillis() -idleTime);
137     }
138
139     protected void setIdleTime(long idleTime) {
140         this.idleTime = idleTime;
141     }
142
143     // Work out who called the Statement - Allows us to debug code down to the point
144
// where SQL can be associated with classes, thus allowing instant debug !
145
// The stack trace will be like this:
146
// pool.core.PoolSqlMonitor.setCaller(PoolSqlMonitor.java:68)
147
// pool.core.PoolSqlMonitor.setSql(PoolSqlMonitor.java:68)
148
// pool.core.PoolStatement.executeQuery(PoolStatement.java:
149
// org.apache.jsp.test_jsp._jspService(test_jsp.java:61)
150
// So we always know that the class calling the Statement will be two elements
151
// above this.
152
private void setCaller() {
153         setCaller(4);
154     }
155
156     public void setCaller(int num) {
157             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158
159             try {
160                 throw new Exception JavaDoc();
161             } catch (Exception JavaDoc e) {
162                 StackTraceElement JavaDoc[] ste = e.getStackTrace();
163         // e.printStackTrace(System.err);
164
for (int i = 0; i < ste.length; i++) {
165                     if (this.getClass().getName().equals(ste[i].getClassName())) {
166                         //System.out.println(i +" " +num +" " +(i +num) +" " +ste.length);
167
//this.caller = (ste[i+num].getClassName() +"." +ste[i+num].getMethodName() +", line : " +ste[i+num].getLineNumber());
168
if (i+num >= (ste.length )) {
169                             break;
170                         }
171                         sb.append(ste[i+num].getFileName() +"[method:" +ste[i+num].getMethodName() +",line:" +ste[i+num].getLineNumber() +"], ");
172                         num++;
173                         if (i+num >= (ste.length )) {
174                             break;
175                         }
176                         sb.append(ste[i+num].getFileName() +"[method:" +ste[i+num].getMethodName() +",line:" +ste[i+num].getLineNumber() +"], ");
177                         num++;
178                         if (i+num >= (ste.length )) {
179                             break;
180                         }
181                         sb.append(ste[i+num].getFileName() +"[method:" +ste[i+num].getMethodName() +",line:" +ste[i+num].getLineNumber() +"], ");
182                         num++;
183                         if (i+num >= (ste.length )) {
184                             break;
185                         }
186                         sb.append(ste[i+num].getFileName() +"[method:" +ste[i+num].getMethodName() +",line:" +ste[i+num].getLineNumber() +"], ");
187
188                         break;
189
190                     }
191                 }
192             }
193
194             this.caller = sb.toString();
195     }
196
197     protected String JavaDoc getCaller() {
198         return this.caller;
199     }
200
201 }
202
Popular Tags