KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sql > standard > StandardDBQuery


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: StandardDBQuery.java,v 1.3 2005/05/26 08:08:10 predrag Exp $
22  */

23 package com.lutris.appserver.server.sql.standard;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import org.enhydra.dods.DODS;
29
30 import com.lutris.appserver.server.sql.DBConnection;
31 import com.lutris.appserver.server.sql.DBQuery;
32 import com.lutris.appserver.server.sql.ExtendedQuery;
33 import com.lutris.appserver.server.sql.ObjectIdException;
34 import com.lutris.appserver.server.sql.Query;
35 import com.lutris.logging.Logger;
36 import com.lutris.util.FatalExceptionError;
37
38 /**
39  * Standard implementation of a DBQuery object.
40  *
41  * @see DBQuery
42  * @author Kyle Clark
43  * @since LBS1.7
44  * @version $Revision: 1.3 $
45  */

46 public class StandardDBQuery implements DBQuery {
47
48     /**
49      * Identifier for this query object.
50      */

51     private int id;
52
53     /**
54      * Next available identifier.
55      */

56     private static int nextId;
57
58     /**
59      * Connection used by this query object.
60      */

61     private DBConnection conn;
62
63     /**
64      * Result set after performing a query.
65      */

66     private ResultSet JavaDoc resultSet = null;
67
68     /**
69      * The interface via which the query will be
70      * executed.
71      */

72     private Query queryInterface = null;
73
74     /**
75      * Is this object still active, or has it been released.
76      */

77     private boolean released = false;
78     private boolean releaseConnection = true;
79
80     /**
81      * The log channel.
82      */

83     // private LogChannel channel;
84

85     /**
86      * Construct a query object for use on the supplied dB connection.
87      *
88      * @param dbConnection
89      * The database connection to use.
90      * @exception SQLException
91      * If a database access error occurs.
92      */

93     protected StandardDBQuery(DBConnection dbConnection)
94         throws SQLException JavaDoc {
95         id = nextId++;
96         logDebug("new instance");
97         conn = dbConnection;
98     }
99
100     /**
101      * Query the database.
102      *
103      * @param q
104      * Query interface via which the query will be executed.
105      * @exception SQLException
106      * If a database access error occurs.
107      */

108     public synchronized void query(Query q)
109         throws SQLException JavaDoc {
110         logDebug("execute query");
111         validate();
112         conn.incrRequestCount();
113         queryInterface = q;
114         try {
115             if (resultSet != null) {
116                 resultSet.close();
117             }
118             resultSet = queryInterface.executeQuery(conn);
119         } catch (SQLException JavaDoc e) {
120             handleException(e);
121             throw e;
122         }
123     }
124
125     /**
126      * Returns a new object representing the next result form
127      * the query.
128      *
129      * @return
130      * New instance of object representing query results.
131      * <I>null</I> is returned when there are no more results.
132      * @exception SQLException
133      * If a database access error occurs.
134      * @exception ObjectIdException
135      * If ObjectId was not found.
136      */

137     public Object JavaDoc next() throws SQLException JavaDoc, ObjectIdException {
138         logDebug("get next result");
139         validate();
140         try {
141             return queryInterface.next(resultSet);
142         } catch (SQLException JavaDoc e) {
143             handleException(e);
144             throw e;
145         }
146     }
147
148     /**
149      * Frees all resources consumed by this query.
150      * Connections are returned to the connection pool.
151      * Subsequent queries via this object,
152      * will throw an exception.
153      */

154     public synchronized void release() {
155         logDebug("release");
156         try {
157             validate();
158         } catch (SQLException JavaDoc except) {
159             throw new FatalExceptionError(except);
160         }
161         try {
162             //
163
// Free all resources consumed by this connection
164
// and return the connection to the connection
165
// pool.
166
//
167
// NOTE: if a connection cannot handle and exception,
168
// then it is automatically dropped from the connection
169
// pool and closed. This means that the connection
170
// should no longer be used.
171
//
172
SQLException JavaDoc sqlEx = null;
173
174             try {
175                 java.sql.Statement JavaDoc stmt=null;
176                 try {
177                     if (resultSet != null) {
178                         resultSet.close();
179                     }
180                 }catch(SQLException JavaDoc sqle) {
181                     logDebug(sqle.toString());
182                 }
183                 stmt = ((ExtendedQuery)queryInterface).getStatement();
184                 if (stmt!=null){
185                     stmt.close();
186                 }
187             } catch (SQLException JavaDoc e) {
188                 conn.handleException(e);
189             }
190             try {
191                 if (releaseConnection) {
192                     conn.reset();
193                 }
194             } catch (SQLException JavaDoc e) {
195                 conn.handleException(e);
196             }
197             //
198
// Release the connection.
199
//
200
if (releaseConnection) {
201                 conn.release();
202             }
203         }
204         finally {
205             released = true;
206             resultSet = null;
207             queryInterface = null;
208             conn = null;
209         }
210     }
211
212     /**
213      * Exception handler. This object is should not be
214      * used for subsequent queries if this method returns
215      * false.
216      *
217      * @return
218      * True if the exception can be handled and the object is
219      * still valid, false otherwise.
220      */

221     public synchronized boolean handleException(SQLException JavaDoc e) {
222         logDebug("handle exception");
223         return conn.handleException(e);
224     }
225
226     /**
227      * Method to ensure this object is still valid.
228      * Once this object has been released it cannot be
229      * used any more.
230      *
231      * @exception SQLException
232      * If a database access error occurs.
233      */

234     public void validate() throws SQLException JavaDoc {
235         if (released) {
236             throw new SQLException JavaDoc("Cannot access this object "
237                                        + "once it has been released.");
238         }
239     }
240
241     /**
242      * Finalizer.
243      * If this object has not been <A HREF=#release>released</A>,
244      * this method ensures that garbage collection does so.
245      */

246     protected void finalize() {
247         if (!released) {
248             release();
249         }
250     }
251
252     /**
253      * Logging. For debuging only, since it effects all Query objects.
254      *
255      * @param str
256      * The data to log.
257      */

258     protected void logDebug(String JavaDoc str) {
259         if (DODS.getDatabaseManager().debug) {
260             DODS.getLogChannel().write(Logger.DEBUG, "DBTransaction[" + id + "]: " + str);
261         }
262     }
263
264     /**
265      *
266      */

267     protected void setReleaseConnection(boolean rc) {
268         releaseConnection = rc;
269     }
270 }
271
Popular Tags