1 23 package com.lutris.appserver.server.sql.standard; 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 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 46 public class StandardDBQuery implements DBQuery { 47 48 51 private int id; 52 53 56 private static int nextId; 57 58 61 private DBConnection conn; 62 63 66 private ResultSet resultSet = null; 67 68 72 private Query queryInterface = null; 73 74 77 private boolean released = false; 78 private boolean releaseConnection = true; 79 80 83 85 93 protected StandardDBQuery(DBConnection dbConnection) 94 throws SQLException { 95 id = nextId++; 96 logDebug("new instance"); 97 conn = dbConnection; 98 } 99 100 108 public synchronized void query(Query q) 109 throws SQLException { 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 e) { 120 handleException(e); 121 throw e; 122 } 123 } 124 125 137 public Object next() throws SQLException , ObjectIdException { 138 logDebug("get next result"); 139 validate(); 140 try { 141 return queryInterface.next(resultSet); 142 } catch (SQLException e) { 143 handleException(e); 144 throw e; 145 } 146 } 147 148 154 public synchronized void release() { 155 logDebug("release"); 156 try { 157 validate(); 158 } catch (SQLException except) { 159 throw new FatalExceptionError(except); 160 } 161 try { 162 SQLException sqlEx = null; 173 174 try { 175 java.sql.Statement stmt=null; 176 try { 177 if (resultSet != null) { 178 resultSet.close(); 179 } 180 }catch(SQLException sqle) { 181 logDebug(sqle.toString()); 182 } 183 stmt = ((ExtendedQuery)queryInterface).getStatement(); 184 if (stmt!=null){ 185 stmt.close(); 186 } 187 } catch (SQLException e) { 188 conn.handleException(e); 189 } 190 try { 191 if (releaseConnection) { 192 conn.reset(); 193 } 194 } catch (SQLException e) { 195 conn.handleException(e); 196 } 197 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 221 public synchronized boolean handleException(SQLException e) { 222 logDebug("handle exception"); 223 return conn.handleException(e); 224 } 225 226 234 public void validate() throws SQLException { 235 if (released) { 236 throw new SQLException ("Cannot access this object " 237 + "once it has been released."); 238 } 239 } 240 241 246 protected void finalize() { 247 if (!released) { 248 release(); 249 } 250 } 251 252 258 protected void logDebug(String str) { 259 if (DODS.getDatabaseManager().debug) { 260 DODS.getLogChannel().write(Logger.DEBUG, "DBTransaction[" + id + "]: " + str); 261 } 262 } 263 264 267 protected void setReleaseConnection(boolean rc) { 268 releaseConnection = rc; 269 } 270 } 271 | Popular Tags |