KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > conn > PooledConnection


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.conn;
13
14 import com.versant.core.common.BindingSupportImpl;
15 import com.versant.core.common.Debug;
16 import com.versant.core.logging.LogEventStore;
17
18 import java.sql.*;
19
20 /**
21  * A JDBC connection belonging to a JDBCConnectionPool.
22  *
23  * @see JDBCConnectionPool
24  */

25 public final class PooledConnection extends LoggingConnection {
26
27     private final JDBCConnectionPool pool;
28     private boolean destroyed; // connection has been timedout
29
public boolean idle; // is connection idle (i.e. in pool)?
30
public PooledConnection prev, next; // linked list for JDBConnectionPool
31
public int age; // number of times con has been returned to the pool
32
private long lastActivityTime;
33     // Time when something last happened on this connection. This is used
34
// to cleanup active connections that are stuck.
35

36     private int cachedTransationIsolation = -1;
37     private boolean cachedAutoCommit;
38
39     public PooledConnection(JDBCConnectionPool pool, Connection con,
40             LogEventStore pes, boolean usePsPool, int psCacheMax) {
41         super(con, pes, usePsPool, psCacheMax, pool.isClearBatch());
42         this.pool = pool;
43     }
44
45     public JDBCConnectionPool getPool() {
46         return pool;
47     }
48
49     public synchronized long getLastActivityTime() {
50         return lastActivityTime;
51     }
52
53     public synchronized void updateLastActivityTime() {
54         lastActivityTime = System.currentTimeMillis();
55     }
56
57     /**
58      * Return ps to the pool.
59      * @see PooledPreparedStatement#close
60      */

61     public void returnPreparedStatement(PooledPreparedStatement ps)
62             throws SQLException {
63         updateLastActivityTime();
64         super.returnPreparedStatement(ps);
65     }
66
67     /**
68      * This is just going to return the connection to the pool.
69      */

70     public void close() {
71         if (Debug.DEBUG) {
72             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
73         }
74         pool.returnConnection(this);
75     }
76
77     public void setHoldability(int holdability) throws SQLException {
78         if (Debug.DEBUG) {
79             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
80         }
81         super.setHoldability(holdability);
82     }
83
84     public int getHoldability() throws SQLException {
85         if (Debug.DEBUG) {
86             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
87         }
88         return super.getHoldability();
89     }
90
91     public Savepoint setSavepoint() throws SQLException {
92         if (Debug.DEBUG) {
93             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
94         }
95         return super.setSavepoint();
96     }
97
98     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
99         if (Debug.DEBUG) {
100             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
101         }
102         return super.setSavepoint(name);
103     }
104
105     public void rollback(Savepoint savepoint) throws SQLException {
106         if (Debug.DEBUG) {
107             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
108         }
109         super.rollback(savepoint);
110     }
111
112     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
113         if (Debug.DEBUG) {
114             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
115         }
116         super.releaseSavepoint(savepoint);
117     }
118
119     public Statement createStatement(int resultSetType, int resultSetConcurrency,
120                                      int resultSetHoldability) throws SQLException {
121         if (Debug.DEBUG) {
122             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
123         }
124         return super.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
125     }
126
127     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
128                                               int resultSetConcurrency, int resultSetHoldability)
129             throws SQLException {
130         if (Debug.DEBUG) {
131             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
132         }
133         return super.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
134     }
135
136     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
137                                          int resultSetConcurrency,
138                                          int resultSetHoldability) throws SQLException {
139         if (Debug.DEBUG) {
140             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
141         }
142         return super.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
143     }
144
145     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
146             throws SQLException {
147         if (Debug.DEBUG) {
148             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
149         }
150         return super.prepareStatement(sql, autoGeneratedKeys);
151     }
152
153     public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[])
154             throws SQLException {
155         if (Debug.DEBUG) {
156             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
157         }
158         return super.prepareStatement(sql, columnIndexes);
159     }
160
161     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
162             throws SQLException {
163         if (Debug.DEBUG) {
164             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
165         }
166         return super.prepareStatement(sql, columnNames);
167     }
168
169     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
170         if (Debug.DEBUG) {
171             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
172         }
173         return super.nativeSQL(sql);
174     }
175
176     public boolean getAutoCommit() throws SQLException {
177         if (Debug.DEBUG) {
178             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
179         }
180         return cachedAutoCommit = super.getAutoCommit();
181     }
182
183     public boolean getCachedAutoCommit() {
184         return cachedAutoCommit;
185     }
186
187     public boolean isClosed() throws SQLException {
188         if (Debug.DEBUG) {
189             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
190         }
191         return super.isClosed();
192     }
193
194     public DatabaseMetaData getMetaData() throws SQLException {
195         if (Debug.DEBUG) {
196             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
197         }
198         return super.getMetaData();
199     }
200
201     public void setReadOnly(boolean readOnly) throws SQLException {
202         if (Debug.DEBUG) {
203             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
204         }
205         super.setReadOnly(readOnly);
206     }
207
208     public boolean isReadOnly() throws SQLException {
209         if (Debug.DEBUG) {
210             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
211         }
212         return super.isReadOnly();
213     }
214
215     public void setCatalog(String JavaDoc catalog) throws SQLException {
216         if (Debug.DEBUG) {
217             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
218         }
219         super.setCatalog(catalog);
220     }
221
222     public String JavaDoc getCatalog() throws SQLException {
223         if (Debug.DEBUG) {
224             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
225         }
226         return super.getCatalog();
227     }
228
229     public void setTransactionIsolation(int level) throws SQLException {
230         if (Debug.DEBUG) {
231             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
232         }
233         super.setTransactionIsolation(level);
234         cachedTransationIsolation = level;
235     }
236
237
238
239     public int getTransactionIsolation() throws SQLException {
240         if (Debug.DEBUG) {
241             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
242         }
243         return cachedTransationIsolation = super.getTransactionIsolation();
244     }
245
246     public int getCachedTransactionIsolation() throws SQLException {
247         if (Debug.DEBUG) {
248             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
249         }
250         if (cachedTransationIsolation >= 0) return cachedTransationIsolation;
251         return cachedTransationIsolation = super.getTransactionIsolation();
252     }
253
254     public SQLWarning getWarnings() throws SQLException {
255         if (Debug.DEBUG) {
256             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
257         }
258         return super.getWarnings();
259     }
260
261     public void clearWarnings() throws SQLException {
262         if (Debug.DEBUG) {
263             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
264         }
265         super.clearWarnings();
266     }
267
268     public Statement createStatement(int resultSetType, int resultSetConcurrency)
269             throws SQLException {
270         if (Debug.DEBUG) {
271             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
272         }
273         return super.createStatement(resultSetType, resultSetConcurrency);
274     }
275
276     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
277                  int resultSetConcurrency) throws SQLException {
278         if (Debug.DEBUG) {
279             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
280         }
281         return super.prepareCall(sql, resultSetType, resultSetConcurrency);
282     }
283
284     public java.util.Map JavaDoc getTypeMap() throws SQLException {
285         if (Debug.DEBUG) {
286             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
287         }
288         return super.getTypeMap();
289     }
290
291     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException {
292         if (Debug.DEBUG) {
293             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
294         }
295         super.setTypeMap(map);
296     }
297
298     public Statement createStatement() throws SQLException {
299         updateLastActivityTime();
300         if (Debug.DEBUG) {
301             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
302         }
303         return super.createStatement();
304     }
305
306     public PreparedStatement prepareStatement(String JavaDoc sql)
307             throws SQLException {
308         if (Debug.DEBUG) {
309             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
310         }
311         return super.prepareStatement(sql);
312     }
313
314
315     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
316             int resultSetConcurrency) throws SQLException {
317         if (Debug.DEBUG) {
318             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
319         }
320         return super.prepareStatement(sql,resultSetType, resultSetConcurrency);
321     }
322
323
324     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
325         if (Debug.DEBUG) {
326             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
327         }
328         return super.prepareCall(sql);
329     }
330
331     public void setAutoCommit(boolean autoCommit) throws SQLException {
332         if (Debug.DEBUG) {
333             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
334         }
335         super.setAutoCommit(autoCommit);
336         cachedAutoCommit = autoCommit;
337     }
338
339     public void commit() throws SQLException {
340         if (Debug.DEBUG) {
341             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
342         }
343         super.commit();
344     }
345
346     public void rollback() throws SQLException {
347         if (Debug.DEBUG) {
348             if (idle) throw BindingSupportImpl.getInstance().internal("con in pool");
349         }
350         super.rollback();
351     }
352
353
354     /**
355      * Has this connection been destroyed?
356      * @see #destroy()
357      */

358     public boolean isDestroyed() {
359         return destroyed;
360     }
361
362     /**
363      * Forceably close the real connection and set a flag to make sure this
364      * connection will not go back in the pool. Any exceptions on close
365      * are silently discarded. This is a NOP if the connection has already
366      * been destroyed.
367      * @see #isDestroyed()
368      */

369     public void destroy() {
370         if (destroyed) return;
371         this.destroyed = true;
372         try {
373             super.close();
374         } catch (SQLException e) {
375             // ignore
376
}
377     }
378
379 }
380
381
Popular Tags