KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > mordred > PoolConnEntry


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util.mordred;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.sql.*;
23 import java.util.Map JavaDoc;
24
25 /**
26  * An entry in a connection pool.
27  *
28  */

29 public class PoolConnEntry implements java.sql.Connection JavaDoc{
30     private static final boolean DEEP_DEBUG = false;
31
32     // States for connections (in use, being tested, or active)
33
public final static int AVAILABLE = 0;
34     public final static int ACTIVE = 1;
35
36     private JdbcDataSource container;
37
38     private Connection connection;
39     private int status;
40     private long lockTime;
41     private long createDate;
42     private long lastActivity;
43     private int id;
44     private java.lang.Throwable JavaDoc trace;
45
46     /**
47      * Insert the method's description here.
48      * Creation date: (8/24/99 11:43:45 AM)
49      * @param conn java.sql.Connection
50      */

51     public PoolConnEntry(JdbcDataSource container, Connection conn, int id) {
52         this.container = container;
53         this.connection = conn;
54         status = AVAILABLE;
55         createDate = System.currentTimeMillis();
56         lastActivity = System.currentTimeMillis();
57         this.id = id;
58     }
59
60     /**
61      * Locks an entry for anybody else using it
62      */

63     public synchronized boolean lock() throws SQLException {
64         if (DEEP_DEBUG) {
65             System.out.println("Trying to lock");
66         }
67
68         if (status != PoolConnEntry.AVAILABLE) {
69             return false;
70         }
71
72         if (DEEP_DEBUG) {
73             System.out.println("Available");
74         }
75
76         if (false) {
77             //There really is no sense in doing this...
78
// maybe make it a conf option at some point, but really slows
79
// down the pooling.
80
if (connection.isClosed()) {
81                 throw new SQLException("Connection has been closed.");
82             }
83
84             if (DEEP_DEBUG) {
85                 System.out.println("not closed");
86             }
87         }
88
89
90         status = PoolConnEntry.ACTIVE;
91         lockTime = System.currentTimeMillis();
92         lastActivity = lockTime;
93         trace = new Throwable JavaDoc();
94         clearWarnings();
95         if (DEEP_DEBUG) {
96             System.out.println("Returning");
97         }
98         return true;
99     }
100
101     /**
102      * Resets flags on an entry for reuse in the pool
103      */

104     public synchronized void unlock() {
105         lastActivity = System.currentTimeMillis();
106         trace = null;
107         status = AVAILABLE;
108     }
109
110     /**
111      * Simple method to log any warnings on an entry (connection), and
112      * then clear them.
113      * @throws java.sql.SQLException
114      */

115     public void clearWarnings() {
116         try {
117             SQLWarning currSQLWarning = connection.getWarnings();
118             while (currSQLWarning != null) {
119                 StringBuffer JavaDoc logBuffer =
120                     new StringBuffer JavaDoc(256)
121                             .append("Warnings on connection ")
122                             .append(id)
123                             .append(currSQLWarning);
124                 container.debug(logBuffer.toString());
125                 currSQLWarning = currSQLWarning.getNextWarning();
126             }
127             connection.clearWarnings();
128         } catch (SQLException sqle) {
129             container.debug("Error while clearing exceptions on " + id);
130             // It will probably get killed by itself before too long if this failed
131
}
132     }
133
134
135     /**
136      * Insert the method's description here.
137      * Creation date: (8/24/99 11:43:19 AM)
138      * @return a long representing the time this entry was created
139      */

140     public long getCreateDate() {
141         return createDate;
142     }
143
144     /**
145      * Insert the method's description here.
146      * Creation date: (8/24/99 12:09:01 PM)
147      * @return int
148      */

149     public int getId() {
150         return id;
151     }
152
153     /**
154      * Insert the method's description here.
155      * Creation date: (8/24/99 11:43:19 AM)
156      * @return long
157      */

158     public long getLastActivity() {
159         return lastActivity;
160     }
161
162     /**
163      * Insert the method's description here.
164      * Creation date: (8/24/99 11:43:19 AM)
165      * @return long
166      */

167     public long getLockTime() {
168         return lockTime;
169     }
170
171     /**
172      * Insert the method's description here.
173      * Creation date: (8/24/99 11:43:19 AM)
174      * @return int
175      */

176     public int getStatus() {
177         return status;
178     }
179
180     /**
181      * Insert the method's description here.
182      * Creation date: (8/24/99 2:33:38 PM)
183      * @return java.lang.Throwable
184      */

185     public java.lang.Throwable JavaDoc getTrace() {
186         return trace;
187     }
188
189     /**
190      * Need to clean up the connection
191      */

192     protected void finalize() {
193         container.debug("Closing connection " + id);
194         try {
195             connection.close();
196         } catch (SQLException ex) {
197             StringBuffer JavaDoc warnBuffer =
198                 new StringBuffer JavaDoc(64)
199                     .append("Cannot close connection ")
200                     .append(id)
201                     .append(" on finalize");
202             container.warn(warnBuffer.toString());
203         }
204         // Dump the stack trace of whoever created this connection
205
if (getTrace() != null) {
206             StringWriter JavaDoc sout = new StringWriter JavaDoc();
207             trace.printStackTrace(new PrintWriter JavaDoc(sout, true));
208             container.info(sout.toString());
209         }
210     }
211
212     public String JavaDoc getString() {
213         StringBuffer JavaDoc poolConnStringBuffer =
214             new StringBuffer JavaDoc(64)
215                     .append(getId())
216                     .append(": ")
217                     .append(connection.toString());
218         return poolConnStringBuffer.toString();
219     }
220
221
222     /*
223      * New approach now actually has this implement a connection, as a wrapper.
224      * All calls will be passed to underlying connection.
225      * Except when closed is called, which will instead cause the releaseConnection on
226      * the parent to be executed.
227      *
228      * These are the methods from java.sql.Connection
229      */

230
231     public void close() throws SQLException {
232         clearWarnings();
233         container.releaseConnection(this);
234     }
235
236     /**
237      * Returns whether this entry is closed.
238      *
239      * @return whether the underlying conntection is closed
240      */

241     public boolean isClosed() throws SQLException {
242         return connection.isClosed();
243     }
244
245
246     public final Statement createStatement() throws SQLException {
247         return connection.createStatement();
248     }
249
250     public final PreparedStatement prepareStatement(final String JavaDoc sql) throws SQLException {
251         return connection.prepareStatement(sql);
252     }
253
254     public final CallableStatement prepareCall(final String JavaDoc sql) throws SQLException {
255         return connection.prepareCall(sql);
256     }
257
258     public final String JavaDoc nativeSQL(final String JavaDoc sql) throws SQLException {
259         return connection.nativeSQL( sql );
260     }
261
262     public final void setAutoCommit(final boolean autoCommit) throws SQLException {
263         connection.setAutoCommit( autoCommit );
264     }
265
266     public final boolean getAutoCommit() throws SQLException {
267         return connection.getAutoCommit();
268     }
269
270     public final void commit() throws SQLException {
271         connection.commit();
272     }
273
274     public final void rollback() throws SQLException {
275         connection.rollback();
276     }
277
278     public final DatabaseMetaData getMetaData() throws SQLException {
279         return connection.getMetaData();
280     }
281
282     public final void setReadOnly( final boolean readOnly ) throws SQLException {
283         connection.setReadOnly( readOnly );
284     }
285
286     public final boolean isReadOnly() throws SQLException {
287         return connection.isReadOnly();
288     }
289
290     public final void setCatalog( final String JavaDoc catalog ) throws SQLException {
291         connection.setCatalog( catalog );
292     }
293
294     public final String JavaDoc getCatalog() throws SQLException {
295         return connection.getCatalog();
296     }
297
298     public final void setTransactionIsolation( final int level ) throws SQLException {
299         connection.setTransactionIsolation(level);
300     }
301
302     public final int getTransactionIsolation() throws SQLException {
303         return connection.getTransactionIsolation();
304     }
305
306     public final SQLWarning getWarnings() throws SQLException {
307         return connection.getWarnings();
308     }
309
310     public final Statement createStatement( final int resultSetType,
311                                             final int resultSetConcurrency )
312             throws SQLException {
313         return connection.createStatement(resultSetType, resultSetConcurrency);
314     }
315
316     public final PreparedStatement prepareStatement( final String JavaDoc sql,
317                                                final int resultSetType,
318                                                final int resultSetConcurrency )
319             throws SQLException {
320         return connection.prepareStatement( sql, resultSetType, resultSetConcurrency);
321     }
322
323     public final CallableStatement prepareCall( final String JavaDoc sql,
324                                           final int resultSetType,
325                                           final int resultSetConcurrency )
326             throws SQLException {
327         return connection.prepareCall( sql, resultSetType, resultSetConcurrency );
328     }
329
330     public final Map JavaDoc getTypeMap() throws SQLException {
331         return connection.getTypeMap();
332     }
333
334     public final void setTypeMap( final Map JavaDoc map ) throws SQLException {
335         connection.setTypeMap( map );
336     }
337
338     /* JDBC_3_ANT_KEY
339     public final void setHoldability(int holdability)
340         throws SQLException
341     {
342         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
343     }
344
345     public final int getHoldability()
346         throws SQLException
347     {
348         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
349     }
350
351     public final java.sql.Savepoint setSavepoint()
352         throws SQLException
353     {
354         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
355     }
356
357     public final java.sql.Savepoint setSavepoint(String savepoint)
358         throws SQLException
359     {
360         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
361     }
362
363     public final void rollback(java.sql.Savepoint savepoint)
364         throws SQLException
365     {
366         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
367     }
368
369     public final void releaseSavepoint(java.sql.Savepoint savepoint)
370         throws SQLException
371     {
372         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
373     }
374
375     public final Statement createStatement(int resulSetType,
376                                            int resultSetConcurrency,
377                                            int resultSetHoldability)
378         throws SQLException
379     {
380         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
381     }
382
383     public final PreparedStatement prepareStatement(String sql,
384                                         int resulSetType,
385                                         int resultSetConcurrency,
386                                         int resultSetHoldability)
387         throws SQLException
388     {
389         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
390     }
391
392     public final CallableStatement prepareCall(String sql,
393                                         int resulSetType,
394                                         int resultSetConcurrency,
395                                         int resultSetHoldability)
396         throws SQLException
397     {
398         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
399     }
400
401     public final PreparedStatement prepareStatement(String sql,
402                                         int autoGeneratedKeys)
403         throws SQLException
404     {
405         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
406     }
407
408     public final PreparedStatement prepareStatement(String sql,
409                                         int[] columnIndexes)
410         throws SQLException
411     {
412         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
413     }
414
415     public final PreparedStatement prepareStatement(String sql,
416                                         String[] columnNames)
417         throws SQLException
418     {
419         throw new SQLException("This is not a Jdbc 3.0 Compliant Connection");
420     }
421     JDBC_3_ANT_KEY */

422
423 }
424
Popular Tags