Exception not being caught?!
Exception not being caught?!
- Subject: Exception not being caught?!
- From: Kieran Kelleher <email@hidden>
- Date: Wed, 19 May 2004 09:26:34 -0400
I followed the guidelines and code examples in the EO book to implement
optimistic locking exception recovery, but still get an exception
thrown on one browser when saving the same EO at the same time via two
different application instances. I can only reproduce this if I log
into different instances of the application. Logging in to two
different sessions of the same instance will not produce an exception.
My exception handling is shown below. Any advice/guidance would be
greatly appreciated.
Here is the error message:
Exception Description
------------------------------------------------------------------------
--------
Application: epworkperks
Error: com.webobjects.eoaccess.EOGeneralAdaptorException:
updateValuesInRowDescribedByQualifier --
com.webobjects.jdbcadaptor.JDBCChannel method failed to update row in
database
Reason:
Stack trace: File Line# Method Package
------------------------------------------------------------------------
--------
NA : Non applicable, JIT activated
Here is the exception handling code.
I have a save() method in my Session class which handles saving and
implements "last write wins" exception handling (well, supposed to
anyway!):
/
************************************************************************
***********************
* My generic save method for this session's editing context.
* based on code found at
http://developer.apple.com/documentation/WebObjects/Enterprise_Objects/
UpdateStrategies/chapter_9_section_8.html#//apple_ref/doc/uid/
TP30001011-DontLinkChapterID_15-BHAHFAFH
************************************************************************
***********************/
public void save() {
EOEditingContext editingContext = defaultEditingContext();
try {
editingContext.saveChanges();
//Thrown for each eo that fails to save.
} catch (EOGeneralAdaptorException saveException) {
//Determine if the exception is an optimistic locking
exception.
if (isOptimisticLockingFailure(saveException)) {
//Deal with the optimistic locking exception using
"last write wins" strategy.
handleOptimisticLockingFailureByLastWriteWins(saveException);
} else {
//Don't know what went wrong so revert editing context
to a stable state.
editingContext.revert();
}
}
}
/
************************************************************************
***********************
* Determine if the exception thrown during a save is an optimistic
locking exception.
************************************************************************
***********************/
public boolean isOptimisticLockingFailure(EOGeneralAdaptorException
exceptionWhileSaving) {
//Get the info dictionary that is created when the exception is
thrown.
NSDictionary exceptionInfo = exceptionWhileSaving.userInfo();
//Determine the type of the failure.
Object failureType = (exceptionInfo != null) ?
exceptionInfo.objectForKey(EOAdaptorChannel.AdaptorFailureKey) : null;
//Return depending on the type of failure.
if ( (failureType != null) && (
failureType.equals(EOAdaptorChannel.AdaptorOptimisticLockingFailure) )
) {
return true;
} else {
return false;
}
}
/
************************************************************************
***********************
* Deal with an optimistic locking failure by recovering and "last
write wins"
************************************************************************
***********************/
public void
handleOptimisticLockingFailureByLastWriteWins(EOGeneralAdaptorException
lockingException) {
//Get the info dictionary that is created when the exception is
thrown.
NSDictionary info = lockingException.userInfo();
//Determine the adaptor operation that triggered the optimistic
locking failure.
EOAdaptorOperation adaptorOperation =
(EOAdaptorOperation)info.objectForKey(EOAdaptorChannel.FailedAdaptorOper
ationKey);
int operationType = adaptorOperation.adaptorOperator();
//Determine the database operation that triggered the failure.
EODatabaseOperation dbOperation =
(EODatabaseOperation)info.objectForKey(EODatabaseContext.FailedDatabaseO
perationKey);
//Retrieve the enterprise object that triggered the failure.
EOEnterpriseObject failedEO =
(EOEnterpriseObject)dbOperation.object();
//Retrieve the dictionary of values involved in the failure.
NSDictionary valuesInFailedSave =
adaptorOperation.changedValues();
NSLog.out.appendln("valuesInFailedSave: " + valuesInFailedSave);
//Take action based on the type of adaptor operation that
triggered the optimistic locking failure.
if (operationType == EODatabaseOperation.AdaptorUpdateOperator)
{
//Recover by essentially ignoring the optimistic locking
failure and committing the
//changes that originally failed. This is a last write wins
policy.
//Overwrite any changes in the database with the eo's
values.
failedEO.reapplyChangesFromDictionary(valuesInFailedSave);
} else {
//The optimistic locking failure was causes by another type of
adaptor operation, not an update.
throw new NSForwardException(lockingException, "Unknown
adaptorOperator " + operationType + " in optimistic locking
exception.");
}
this.defaultEditingContext().saveChanges();
}
Any suggestions where to go from here??
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.