1 21 22 package org.opensubsystems.core.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 28 import org.opensubsystems.core.error.OSSDatabaseAccessException; 29 import org.opensubsystems.core.error.OSSException; 30 import org.opensubsystems.core.util.DatabaseUtils; 31 import org.opensubsystems.core.util.GlobalConstants; 32 33 74 public abstract class DatabaseReadOperation extends DatabaseOperation 75 implements DatabaseOperations 76 { 77 79 85 public DatabaseReadOperation( 86 DatabaseFactoryImpl factory 87 ) 88 { 89 this(factory, null, null, factory.getDataType()); 90 } 91 92 102 public DatabaseReadOperation( 103 DatabaseFactoryImpl factory, 104 String strQueryToPrepare, 105 DatabaseSchema schema, 106 int dataType 107 ) 108 { 109 super(factory, strQueryToPrepare, schema, null, dataType); 110 } 111 112 113 122 public DatabaseReadOperation( 123 DatabaseFactoryImpl factory, 124 String strQueryToPrepare, 125 DatabaseSchema schema 126 ) 127 { 128 this(factory, strQueryToPrepare, schema, factory.getDataType()); 129 } 130 131 133 140 public Object executeRead( 141 ) throws OSSException 142 { 143 Connection cntConnection = null; 144 PreparedStatement pstmQuery = null; 145 146 try 147 { 148 cntConnection = m_factory.m_connectionFactory.requestConnection(true); 150 pstmQuery = prepareQuery(m_factory, cntConnection, m_strQuery); 152 m_returnData = performOperation(m_factory, cntConnection, pstmQuery); 154 } 155 catch (SQLException sqleExc) 156 { 157 handleSQLException(sqleExc, cntConnection, DatabaseOperations.DBOP_SELECT, 158 m_iDataType, m_data); 159 } 160 catch (OSSException ossExc) 164 { 165 handleKnownError(ossExc, cntConnection, DatabaseOperations.DBOP_SELECT, 166 m_iDataType, m_data); 167 } 168 catch (Throwable thr) 170 { 171 handleUnknownError(thr, cntConnection, DatabaseOperations.DBOP_SELECT, 172 m_iDataType, m_data); 173 } 174 finally 175 { 176 DatabaseUtils.closeStatement(pstmQuery); 177 m_factory.m_connectionFactory.returnConnection(cntConnection); 178 } 179 180 return m_returnData; 181 } 182 183 184 186 200 protected PreparedStatement prepareQuery( 201 DatabaseFactoryImpl dbfactory, 202 Connection cntConnection, 203 String strQuery 204 ) throws OSSException, 205 SQLException 206 { 207 PreparedStatement pstmQuery = null; 208 209 if ((strQuery != null) && (strQuery.length() > 0)) 210 { 211 pstmQuery = cntConnection.prepareStatement(strQuery); 212 } 213 214 return pstmQuery; 215 } 216 217 232 protected Object performOperation( 233 DatabaseFactoryImpl dbfactory, 234 Connection cntConnection, 235 PreparedStatement pstmStatement 236 ) throws OSSException, 237 SQLException 238 { 239 return null; 241 } 242 243 244 260 protected void handleSQLException( 261 SQLException sqleExc, 262 Connection cntConnection, 263 int iOperationType, 264 int iDataType, 265 Object data 266 ) throws OSSException 267 { 268 if (m_dbschema != null) 269 { 270 m_dbschema.handleSQLException( 271 sqleExc, cntConnection, iOperationType, iDataType, data); 272 } 273 else 274 { 275 handleUnknownError(sqleExc, cntConnection, iOperationType, iDataType, data); 277 } 278 } 279 280 297 protected void handleKnownError( 298 OSSException exc, 299 Connection cntConnection, 300 int iOperationType, 301 int iDataType, 302 Object data 303 ) throws OSSException 304 { 305 throw exc; 306 } 307 308 326 protected void handleUnknownError( 327 Throwable thr, 328 Connection cntConnection, 329 int iOperationType, 330 int iDataType, 331 Object data 332 ) throws OSSException 333 { 334 switch (iOperationType) 335 { 336 case (DBOP_SELECT) : 337 { 338 339 throw new OSSDatabaseAccessException("Failed to load data from the database.", 340 thr); 341 } 342 default: 343 { 344 if (GlobalConstants.ERROR_CHECKING) 345 { 346 assert false : "Unknown database operation type " + iOperationType; 347 } 348 } 349 } 350 } 351 } 352 | Popular Tags |