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.data.DataObject; 29 import org.opensubsystems.core.error.OSSDataCreateException; 30 import org.opensubsystems.core.error.OSSDataDeleteException; 31 import org.opensubsystems.core.error.OSSDataSaveException; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.util.DatabaseUtils; 34 import org.opensubsystems.core.util.GlobalConstants; 35 36 112 public abstract class DatabaseUpdateOperation extends DatabaseOperation 113 implements DatabaseOperations 114 { 115 117 120 private int m_iUpdateType; 121 122 124 131 public DatabaseUpdateOperation( 132 DatabaseFactoryImpl factory, 133 int iUpdateType 134 ) 135 { 136 this(factory, null, null, iUpdateType, null); 137 } 138 139 140 150 public DatabaseUpdateOperation( 151 DatabaseFactoryImpl factory, 152 String strQueryToPrepare, 153 ModifiableDatabaseSchema schema, 154 int iUpdateType, 155 Object data 156 ) 157 { 158 super(factory, strQueryToPrepare, schema, data, factory.getDataType()); 159 160 m_iUpdateType = iUpdateType; 161 } 162 163 165 171 public void executeUpdate( 172 ) throws OSSException 173 { 174 Connection cntConnection = null; 175 PreparedStatement pstmQuery = null; 176 177 try 178 { 179 cntConnection = m_factory.m_connectionFactory.requestConnection(false); 181 if ((m_strQuery != null) && (m_strQuery.length() > 0)) 182 { 183 if (DatabaseImpl.getInstance().isCallableStatement(m_strQuery)) 186 { 187 pstmQuery = cntConnection.prepareCall(m_strQuery); 188 } 189 else 190 { 191 pstmQuery = cntConnection.prepareStatement(m_strQuery); 192 } 193 } 194 195 performOperation(m_factory, cntConnection, pstmQuery); 197 198 m_factory.m_transactionFactory.commitTransaction(cntConnection); 203 } 204 catch (SQLException sqleExc) 207 { 208 DatabaseUtils.rollbackAndIgnoreException(cntConnection); 213 handleSQLException(sqleExc, cntConnection, m_iUpdateType, m_iDataType, m_data); 214 } 215 catch (OSSException ossExc) 219 { 220 DatabaseUtils.rollbackAndIgnoreException(cntConnection); 225 handleKnownError(ossExc, cntConnection, m_iUpdateType, m_iDataType, m_data); 226 } 227 catch (Throwable thr) 229 { 230 DatabaseUtils.rollbackAndIgnoreException(cntConnection); 235 handleUnknownError(thr, cntConnection, m_iUpdateType, m_iDataType, m_data); 236 } 237 finally 238 { 239 DatabaseUtils.closeStatement(pstmQuery); 240 m_factory.m_connectionFactory.returnConnection(cntConnection); 241 } 242 } 243 244 246 261 protected void performOperation( 262 DatabaseFactoryImpl dbfactory, 263 Connection cntConnection, 264 PreparedStatement pstmStatement 265 ) throws OSSException, 266 SQLException 267 { 268 } 270 271 276 protected void prepareData( 277 DataObject data 278 ) 279 { 280 } 282 283 295 protected int setValuesForInsert( 296 PreparedStatement insertStatement, 297 DataObject data, 298 int initialIndex 299 ) throws OSSException, 300 SQLException 301 { 302 return ((BasicDatabaseFactory)m_factory).setValuesForInsert( 304 insertStatement, data, initialIndex); 305 } 306 307 319 protected int setValuesForUpdate( 320 PreparedStatement updateStatement, 321 DataObject data, 322 int initialIndex 323 ) throws OSSException, 324 SQLException 325 { 326 return ((ModifiableDatabaseFactory)m_factory).setValuesForUpdate( 328 updateStatement, data, initialIndex); 329 } 330 331 347 protected void handleSQLException( 348 SQLException sqleExc, 349 Connection cntConnection, 350 int iOperationType, 351 int iDataType, 352 Object data 353 ) throws OSSException 354 { 355 if (m_dbschema != null) 356 { 357 m_dbschema.handleSQLException( 358 sqleExc, cntConnection, iOperationType, iDataType, data); 359 } 360 else 361 { 362 handleUnknownError(sqleExc, cntConnection, iOperationType, iDataType, data); 364 } 365 } 366 367 384 protected void handleKnownError( 385 OSSException exc, 386 Connection cntConnection, 387 int iOperationType, 388 int iDataType, 389 Object data 390 ) throws OSSException 391 { 392 throw exc; 393 } 394 395 413 protected void handleUnknownError( 414 Throwable thr, 415 Connection cntConnection, 416 int iOperationType, 417 int iDataType, 418 Object data 419 ) throws OSSException 420 { 421 switch (iOperationType) 422 { 423 case (DBOP_INSERT) : 424 { 425 throw new OSSDataCreateException( 426 "Failed to create data in the database.", thr); 427 } 428 case (DBOP_UPDATE) : 429 { 430 throw new OSSDataSaveException( 431 "Failed to update data in the database.", thr); 432 } 433 case (DBOP_DELETE) : 434 { 435 throw new OSSDataDeleteException( 436 "Failed to delete data from the database.", thr); 437 } 438 default: 439 { 440 if (GlobalConstants.ERROR_CHECKING) 441 { 442 assert false : "Unknown database update type " + iOperationType; 443 } 444 } 445 } 446 } 447 } 448 | Popular Tags |