1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.jdbc.driver.sql; |
15 | |
|
16 | |
import java.sql.CallableStatement; |
17 | |
import java.sql.Connection; |
18 | |
import java.sql.DatabaseMetaData; |
19 | |
import java.sql.DriverManager; |
20 | |
import java.sql.PreparedStatement; |
21 | |
import java.sql.SQLException; |
22 | |
import java.sql.SQLWarning; |
23 | |
import java.sql.Savepoint; |
24 | |
import java.sql.Statement; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collections; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import net.admin4j.jdbc.driver.ConnectionRegistry; |
31 | |
import net.admin4j.jdbc.driver.DriverContext; |
32 | |
import net.admin4j.jdbc.driver.DriverContextImpl; |
33 | |
import net.admin4j.jdbc.driver.Admin4jJdbcJmxMBean.StatementInfo; |
34 | |
import net.admin4j.jdbc.driver.DriverContextRegistry; |
35 | |
import net.admin4j.util.annotate.PackageRestrictions; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@PackageRestrictions({"net.admin4j","java","javax"}) |
43 | |
public abstract class ConnectionWrapper30Base implements Connection, DriverContext { |
44 | |
|
45 | |
protected Connection connection; |
46 | |
private StackTraceElement[] creationStackTrace; |
47 | |
|
48 | 18 | private List<StatementInfo> sqlStatementList = new ArrayList<StatementInfo>(); |
49 | |
private static final int MAX_STATEMENT_LIST_SIZE=10; |
50 | |
|
51 | 18 | private final DriverContext driverContext = new DriverContextImpl(); |
52 | |
|
53 | 18 | public ConnectionWrapper30Base(Connection conn) { |
54 | 18 | SQLWrappingUtils.notNull(conn, "Null connection not allowed."); |
55 | 18 | this.connection = conn; |
56 | 18 | ConnectionRegistry.register(this); |
57 | 18 | this.creationStackTrace = Thread.currentThread().getStackTrace(); |
58 | 18 | } |
59 | |
|
60 | |
public StackTraceElement[] registerSqlStatement(String sqlText) { |
61 | 180132 | StackTraceElement[] stack = null; |
62 | 180132 | if (DriverContextRegistry.isTrackExecutionStacks(this.getDriverContext())) { |
63 | 90126 | stack = Thread.currentThread().getStackTrace(); |
64 | |
} |
65 | |
else { |
66 | 90006 | stack = new StackTraceElement[0]; |
67 | |
} |
68 | 180132 | StatementInfo sInfo = new StatementInfo(sqlText, stack); |
69 | 180132 | synchronized(sqlStatementList) { |
70 | 180132 | this.sqlStatementList.add(sInfo); |
71 | 360174 | while (this.sqlStatementList.size() > MAX_STATEMENT_LIST_SIZE) { |
72 | 180042 | this.sqlStatementList.remove(0); |
73 | |
} |
74 | 180132 | } |
75 | |
|
76 | 180132 | return stack; |
77 | |
} |
78 | |
|
79 | |
public List<StatementInfo> getSqlStatementHistory() { |
80 | 2 | List<StatementInfo> list = new ArrayList<StatementInfo>(); |
81 | 2 | synchronized(sqlStatementList) { |
82 | 2 | list.addAll(this.sqlStatementList); |
83 | 2 | } |
84 | |
|
85 | 2 | Collections.reverse(list); |
86 | 2 | return list; |
87 | |
} |
88 | |
|
89 | |
public void clearWarnings() throws SQLException { |
90 | 3 | connection.clearWarnings(); |
91 | |
|
92 | 3 | } |
93 | |
|
94 | |
|
95 | |
public void close() throws SQLException { |
96 | 15 | try {connection.close();} |
97 | |
finally { |
98 | 15 | ConnectionRegistry.unRegister(this); |
99 | 15 | } |
100 | 15 | } |
101 | |
|
102 | |
|
103 | |
public void commit() throws SQLException { |
104 | 60003 | connection.commit(); |
105 | |
|
106 | 60003 | } |
107 | |
|
108 | |
|
109 | |
private Statement wrapStatement(Statement stmt) { |
110 | 21 | return (Statement)SQLWrappingUtils.wrap(stmt, this); |
111 | |
} |
112 | |
|
113 | |
private PreparedStatement wrapPreparedStatement(PreparedStatement stmt, String sqlText, StackTraceElement[] stack) { |
114 | 120018 | return (PreparedStatement)SQLWrappingUtils.wrap(stmt, this, sqlText, stack); |
115 | |
} |
116 | |
|
117 | |
private CallableStatement wrapCallableStatement(CallableStatement stmt, String sqlText, StackTraceElement[] stack) { |
118 | 60009 | return (CallableStatement)SQLWrappingUtils.wrap(stmt, this, sqlText, stack); |
119 | |
} |
120 | |
|
121 | |
public Statement createStatement() throws SQLException { |
122 | 15 | return this.wrapStatement(connection.createStatement()); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
public Statement createStatement(int resultSetType, int resultSetConcurrency) |
127 | |
throws SQLException { |
128 | 3 | return this.wrapStatement(connection.createStatement(resultSetType, resultSetConcurrency)); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
public Statement createStatement(int resultSetType, |
133 | |
int resultSetConcurrency, int resultSetHoldability) |
134 | |
throws SQLException { |
135 | 3 | return this.wrapStatement(connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)); |
136 | |
} |
137 | |
|
138 | |
|
139 | |
public boolean getAutoCommit() throws SQLException { |
140 | 3 | return connection.getAutoCommit(); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
public String getCatalog() throws SQLException { |
145 | 3 | return connection.getCatalog(); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
public int getHoldability() throws SQLException { |
150 | 3 | return connection.getHoldability(); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
public DatabaseMetaData getMetaData() throws SQLException { |
155 | 3 | return connection.getMetaData(); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
public int getTransactionIsolation() throws SQLException { |
160 | 3 | return connection.getTransactionIsolation(); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
public Map<String, Class<?>> getTypeMap() throws SQLException { |
165 | 3 | return connection.getTypeMap(); |
166 | |
} |
167 | |
|
168 | |
|
169 | |
public SQLWarning getWarnings() throws SQLException { |
170 | 3 | return connection.getWarnings(); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
public boolean isClosed() throws SQLException { |
175 | 3 | return connection.isClosed(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
public boolean isReadOnly() throws SQLException { |
180 | 3 | return connection.isReadOnly(); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
public String nativeSQL(String sql) throws SQLException { |
185 | 3 | return connection.nativeSQL(sql); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
public CallableStatement prepareCall(String sql) throws SQLException { |
190 | 60003 | return this.wrapCallableStatement(connection.prepareCall(sql) |
191 | |
, sql, this.registerSqlStatement(sql)); |
192 | |
} |
193 | |
|
194 | |
|
195 | |
public CallableStatement prepareCall(String sql, int resultSetType, |
196 | |
int resultSetConcurrency) throws SQLException { |
197 | 3 | return this.wrapCallableStatement( |
198 | |
connection.prepareCall(sql, resultSetType, resultSetConcurrency) |
199 | |
, sql, this.registerSqlStatement(sql)); |
200 | |
} |
201 | |
|
202 | |
|
203 | |
public CallableStatement prepareCall(String sql, int resultSetType, |
204 | |
int resultSetConcurrency, int resultSetHoldability) |
205 | |
throws SQLException { |
206 | 3 | return this.wrapCallableStatement( |
207 | |
connection.prepareCall(sql, resultSetType |
208 | |
, resultSetConcurrency, resultSetHoldability) |
209 | |
, sql, this.registerSqlStatement(sql)); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
public PreparedStatement prepareStatement(String sql) throws SQLException { |
214 | 120003 | return this.wrapPreparedStatement(connection.prepareStatement(sql) |
215 | |
, sql, this.registerSqlStatement(sql)); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) |
220 | |
throws SQLException { |
221 | 3 | return this.wrapPreparedStatement( |
222 | |
connection.prepareStatement(sql, autoGeneratedKeys) |
223 | |
, sql, this.registerSqlStatement(sql)); |
224 | |
} |
225 | |
|
226 | |
|
227 | |
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) |
228 | |
throws SQLException { |
229 | 3 | return this.wrapPreparedStatement(connection.prepareStatement(sql, columnIndexes) |
230 | |
, sql, this.registerSqlStatement(sql)); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
public PreparedStatement prepareStatement(String sql, String[] columnNames) |
235 | |
throws SQLException { |
236 | 3 | return this.wrapPreparedStatement( |
237 | |
connection.prepareStatement(sql, columnNames) |
238 | |
, sql, this.registerSqlStatement(sql)); |
239 | |
} |
240 | |
|
241 | |
|
242 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
243 | |
int resultSetConcurrency) throws SQLException { |
244 | 3 | return this.wrapPreparedStatement( |
245 | |
connection.prepareStatement(sql, resultSetType, resultSetConcurrency) |
246 | |
, sql, this.registerSqlStatement(sql)); |
247 | |
} |
248 | |
|
249 | |
|
250 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
251 | |
int resultSetConcurrency, int resultSetHoldability) |
252 | |
throws SQLException { |
253 | 3 | return this.wrapPreparedStatement( |
254 | |
connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability) |
255 | |
, sql, this.registerSqlStatement(sql)); |
256 | |
} |
257 | |
|
258 | |
|
259 | |
public void releaseSavepoint(Savepoint savepoint) throws SQLException { |
260 | 3 | connection.releaseSavepoint(savepoint); |
261 | 3 | } |
262 | |
|
263 | |
|
264 | |
public void rollback() throws SQLException { |
265 | 3 | connection.rollback(); |
266 | 3 | } |
267 | |
|
268 | |
|
269 | |
public void rollback(Savepoint savepoint) throws SQLException { |
270 | 3 | connection.rollback(savepoint); |
271 | 3 | } |
272 | |
|
273 | |
|
274 | |
public void setAutoCommit(boolean autoCommit) throws SQLException { |
275 | 3 | connection.setAutoCommit(autoCommit); |
276 | 3 | } |
277 | |
|
278 | |
|
279 | |
public void setCatalog(String catalog) throws SQLException { |
280 | 3 | connection.setCatalog(catalog); |
281 | 3 | } |
282 | |
|
283 | |
|
284 | |
public void setHoldability(int holdability) throws SQLException { |
285 | 3 | connection.setHoldability(holdability); |
286 | 3 | } |
287 | |
|
288 | |
|
289 | |
public void setReadOnly(boolean readOnly) throws SQLException { |
290 | 3 | connection.setReadOnly(readOnly); |
291 | 3 | } |
292 | |
|
293 | |
|
294 | |
public Savepoint setSavepoint() throws SQLException { |
295 | 3 | return connection.setSavepoint(); |
296 | |
} |
297 | |
|
298 | |
|
299 | |
public Savepoint setSavepoint(String name) throws SQLException { |
300 | 3 | return connection.setSavepoint(name); |
301 | |
} |
302 | |
|
303 | |
|
304 | |
public void setTransactionIsolation(int level) throws SQLException { |
305 | 3 | connection.setTransactionIsolation(level); |
306 | |
|
307 | 3 | } |
308 | |
|
309 | |
|
310 | |
public void setTypeMap(Map<String, Class<?>> map) throws SQLException { |
311 | 3 | connection.setTypeMap(map); |
312 | |
|
313 | 3 | } |
314 | |
|
315 | |
|
316 | |
public Connection getConnection() { |
317 | 19 | return connection; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
protected void finalize() throws Throwable { |
322 | 0 | if (!this.isClosed()) { |
323 | 0 | DriverManager.getLogWriter().println("Leaked database connection closed. Connection created at: " + SQLWrappingUtils.LINE_SEPARATOR + SQLWrappingUtils.array2String(this.getCreationStackTrace())); |
324 | 0 | this.close(); |
325 | |
} |
326 | 0 | else ConnectionRegistry.unRegister(this); |
327 | 0 | super.finalize(); |
328 | 0 | } |
329 | |
|
330 | |
public StackTraceElement[] getCreationStackTrace() { |
331 | 0 | return creationStackTrace; |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public String getDriverClassname() { |
338 | 0 | return driverContext.getDriverClassname(); |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public void setDriverClassname(String driverClassname) { |
345 | 15 | this.driverContext.setDriverClassname(driverClassname); |
346 | |
|
347 | 15 | } |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public Integer getDriverMajorVersion() { |
353 | 0 | return driverContext.getDriverMajorVersion(); |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
public void setDriverMajorVersion(Integer driverMajorVersion) { |
360 | 15 | this.driverContext.setDriverMajorVersion(driverMajorVersion); |
361 | |
|
362 | 15 | } |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
public Integer getDriverMinorVersion() { |
368 | 0 | return driverContext.getDriverMinorVersion(); |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
public void setDriverMinorVersion(Integer driverMinorVersion) { |
375 | 15 | this.driverContext.setDriverMinorVersion(driverMinorVersion); |
376 | |
|
377 | 15 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
public String getPoolName() { |
383 | 0 | return driverContext.getPoolName(); |
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
public void setPoolName(String poolName) { |
390 | 15 | this.driverContext.setPoolName(poolName); |
391 | |
|
392 | 15 | } |
393 | |
|
394 | |
public DriverContext getDriverContext() { |
395 | 360261 | return driverContext; |
396 | |
} |
397 | |
|
398 | |
} |