KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > smartlib > pool > core > SmartConnection


1 /*
2  * @(#) SmartConnection 1.0 02/08/01
3  */

4
5 package org.smartlib.pool.core;
6
7 import java.sql.*;
8 import java.util.*;
9
10 /**
11  * This class encapsulates a Connection.
12  * Dont expect me to document this class, if you want refer Sun's Documentation.
13  *
14  * @author Sachin Shekar Shetty
15  * @version 1.0, 02/08/01
16  */

17
18 public class SmartConnection implements Connection ,
19         Close , ConnectionMonitor {
20
21     private Connection conn = null;
22     private long lastAccessedTime = 0;
23     private long connectionObtainedTime = 0;
24
25     private ArrayList references;
26     private boolean autoClose = false;
27     private Debugger debug;
28
29     private boolean isCurrentlyInTransaction;
30
31     //Pool Ref to which this connection belongs
32
private Pool poolReference;
33
34     private boolean isClosed = false;
35
36     private boolean isForcedClosed = false;
37     private long forcedCloseTime = 0;
38
39     private String JavaDoc owner ;
40
41     // default contructor , can only be used within the package
42
SmartConnection(Connection conn , Pool poolReference , String JavaDoc owner
43                 , boolean autoClose ) {
44
45         if (conn == null)
46             throw new IllegalArgumentException JavaDoc(
47                     "Connection Object is null/closed");
48         this.conn = conn;
49         this.poolReference = poolReference;
50         this.owner = owner;
51         lastAccessedTime = System.currentTimeMillis();
52         connectionObtainedTime = lastAccessedTime;
53         this.autoClose = autoClose;
54         if (autoClose)
55             references = new ArrayList();
56         debug = new Debugger("SmartConnection" , true);
57
58     }
59
60     SmartConnection(Connection conn , Pool poolReference) {
61
62         this(conn , poolReference , "N/A" , false);
63
64     }
65
66
67
68     private void preProcess() throws SQLException {
69
70         if (isForcedClosed)
71             throw new StaleConnectionException("SmartPool had withdrawn the connection since it was idle for more than the specified time. Connection withdrawn at " + new java.util.Date JavaDoc(forcedCloseTime));
72         if (isClosed)
73             throw new SQLException("Connection already closed");
74         lastAccessedTime = System.currentTimeMillis();
75
76     }
77
78     public long getLastAccessedTime() {
79
80         return lastAccessedTime;
81
82     }
83
84     void setLastAccessedTime() {
85         
86         lastAccessedTime = System.currentTimeMillis();
87
88     }
89     
90
91     public boolean isCurrentlyInTransaction() {
92
93         return isCurrentlyInTransaction;
94
95     }
96
97     public String JavaDoc getPoolName() {
98         return ((PoolMonitor)poolReference).getName();
99     }
100
101     // default access , only accessible within the package
102
public long getConnectionObtainedTime() {
103
104         return connectionObtainedTime;
105
106     }
107
108     public String JavaDoc getOwner() {
109
110         return owner;
111
112     }
113
114     public Statement createStatement() throws SQLException {
115
116         preProcess();
117         SmartStatement stmt = new SmartStatement(conn.createStatement() , this);
118         if (autoClose)
119             references.add(stmt);
120         return stmt;
121
122     }
123
124     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
125
126         preProcess();
127         SmartPreparedStatement pstmt = new SmartPreparedStatement(
128             conn.prepareStatement(sql) , this);
129         if (autoClose)
130             references.add(pstmt);
131         return pstmt;
132
133     }
134
135     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
136
137         preProcess();
138         SmartCallableStatement cstmt = new SmartCallableStatement(
139                         conn.prepareCall(sql) , this );
140         if (autoClose)
141             references.add(cstmt);
142         return cstmt;
143
144     }
145
146     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
147
148         preProcess();
149         return conn.nativeSQL(sql);
150
151     }
152
153     public void setAutoCommit(boolean autoCommit) throws SQLException {
154
155         preProcess();
156         isCurrentlyInTransaction = autoCommit;
157         conn.setAutoCommit(autoCommit);
158
159     }
160
161     public boolean getAutoCommit() throws SQLException {
162
163         preProcess();
164         return conn.getAutoCommit();
165
166     }
167
168     public void commit() throws SQLException {
169
170         preProcess();
171         conn.commit();
172
173     }
174
175     public void rollback() throws SQLException {
176
177         preProcess();
178         conn.rollback();
179
180     }
181
182     /**
183      * This method is called when the connection is idle for more than
184      * the specified time in max-conection-idle-time in configuration.
185      * The PollThread calls this method to forcefully take the
186      * connection back
187      */

188     void forcedClose() {
189        
190         try {
191             //if (isForcedClosed)
192
//return;
193
synchronized (this) {
194                 forcedCloseTime = System.currentTimeMillis();
195                 debug.print("Forcefully closing connection for owner" + owner);
196                 close();
197                 isForcedClosed = true;
198             }
199         }
200         catch (SQLException sqe) {
201             debug.writeException(sqe);
202         }
203
204     }
205
206     public void close() throws SQLException {
207
208         preProcess();
209         // returning connection to the pool
210
if (autoClose)
211             closeAll();
212         // roll back and set autoCommit to true if in a transaction
213
if (!conn.getAutoCommit() && ((ConnectionPool)poolReference).
214                 getConfigMonitor().getConnectionLoaderClass() != null ) {
215             conn.rollback();
216             conn.setAutoCommit(true);
217         }
218         debug.print("Connection Released for:" + getOwner());
219         poolReference.returnConnection(this);
220         isClosed = true;
221
222     }
223
224     
225
226     
227     private void closeAll() throws SQLException {
228
229         for (int i=0 ; i<references.size() ; i++ ) {
230             Close close = (Close)references.get(i);
231             debug.print("Closing " + close);
232             if (!close.isClosed())
233                 close.close();
234         }
235
236     }
237
238     public boolean isClosed() throws SQLException {
239
240         return isClosed;
241
242     }
243
244     public DatabaseMetaData getMetaData() throws SQLException {
245
246         preProcess();
247         return conn.getMetaData();
248
249     }
250
251     public void setReadOnly(boolean readOnly) throws SQLException {
252
253         preProcess();
254         conn.setReadOnly(readOnly);
255
256     }
257
258     public boolean isReadOnly() throws SQLException {
259
260         preProcess();
261         return conn.isReadOnly();
262
263     }
264
265     public void setCatalog(String JavaDoc catalog) throws SQLException {
266
267         preProcess();
268         conn.setCatalog(catalog);
269
270     }
271
272     public String JavaDoc getCatalog() throws SQLException {
273
274         preProcess();
275         return conn.getCatalog();
276
277     }
278
279
280     public void setTransactionIsolation(int level) throws SQLException {
281
282         preProcess();
283         conn.setTransactionIsolation(level);
284
285     }
286
287     public int getTransactionIsolation() throws SQLException {
288
289         preProcess();
290         return conn.getTransactionIsolation();
291
292     }
293
294     public SQLWarning getWarnings() throws SQLException {
295
296         preProcess();
297         return conn.getWarnings();
298
299     }
300
301     public void clearWarnings() throws SQLException {
302
303         preProcess();
304         conn.clearWarnings();
305
306     }
307
308
309     public Statement createStatement(int resultSetType,
310             int resultSetConcurrency) throws SQLException {
311
312         preProcess();
313         SmartStatement stmt = new SmartStatement(
314                 conn.createStatement(resultSetType , resultSetConcurrency)
315                     ,this);
316         if (autoClose)
317             references.add(stmt);
318         return stmt;
319
320     }
321
322     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
323             int resultSetConcurrency) throws SQLException {
324
325         preProcess();
326         SmartPreparedStatement pstmt = new SmartPreparedStatement(
327             conn.prepareStatement(sql,resultSetType,resultSetConcurrency) , this);
328         if (autoClose)
329             references.add(pstmt);
330         return pstmt;
331
332     }
333
334     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
335             int resultSetConcurrency) throws SQLException {
336
337         preProcess();
338         SmartCallableStatement cstmt = new SmartCallableStatement(
339                 conn.prepareCall(sql , resultSetType,
340                     resultSetConcurrency) , this );
341         if (autoClose)
342             references.add(cstmt);
343         return cstmt;
344
345     }
346
347     public java.util.Map JavaDoc getTypeMap() throws SQLException {
348
349         preProcess();
350         return conn.getTypeMap();
351
352     }
353
354     public void setTypeMap(Map<String JavaDoc, Class JavaDoc<?>> map) throws SQLException {
355         //To change body of implemented methods use File | Settings | File Templates.
356
}
357
358     public void setHoldability(int holdability) throws SQLException {
359         //To change body of implemented methods use File | Settings | File Templates.
360
}
361
362     public int getHoldability() throws SQLException {
363         return 0; //To change body of implemented methods use File | Settings | File Templates.
364
}
365
366     public Savepoint setSavepoint() throws SQLException {
367         return null; //To change body of implemented methods use File | Settings | File Templates.
368
}
369
370     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
371         return null; //To change body of implemented methods use File | Settings | File Templates.
372
}
373
374     public void rollback(Savepoint savepoint) throws SQLException {
375         //To change body of implemented methods use File | Settings | File Templates.
376
}
377
378     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
379         //To change body of implemented methods use File | Settings | File Templates.
380
}
381
382     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
383         return null; //To change body of implemented methods use File | Settings | File Templates.
384
}
385
386     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
387         return null; //To change body of implemented methods use File | Settings | File Templates.
388
}
389
390     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
391         return null; //To change body of implemented methods use File | Settings | File Templates.
392
}
393
394     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
395         return null; //To change body of implemented methods use File | Settings | File Templates.
396
}
397
398     public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[]) throws SQLException {
399         return null; //To change body of implemented methods use File | Settings | File Templates.
400
}
401
402     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException {
403         return null; //To change body of implemented methods use File | Settings | File Templates.
404
}
405
406
407     // default access
408
Connection returnConnection() {
409
410             return conn;
411
412     }
413
414     public String JavaDoc toString() {
415
416         return conn.toString();
417
418     }
419
420 }
421
Popular Tags