Exception handling Concurrency issue

Feb 14, 2008 at 1:46 PM
When there is concurrency error raised the following code handle the exception in the entity Repository class (e.g CustomerRepository.
The error is always stopped at the first case ErrorCodes.ValidationError, it can never make it to the second Case for ConcurrencyViolationError
Why is the switch on the ex.state should it not be on the ErrorCodes The state is alwyas = 1 hence the second case is never hit.


private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
{
if (ex.Number == ErrorCodes.SqlUserRaisedError)
{
switch (ex.State)
{
case ErrorCodes.ValidationError:
string[] messageParts = ex.Errors0.Message.Split(':');
throw new RepositoryValidationException(
mapper.MapDbParameterToBusinessEntityProperty(messageParts0),
messageParts1, ex);

case ErrorCodes.ConcurrencyViolationError:
throw new ConcurrencyViolationException(ex.Message, ex);

}
}


Jawahar
Feb 14, 2008 at 3:26 PM
I fixed this issue replacing the third parameter ( '1' ) on the RAISERROR call on the RethrowError stored procedure with the @ErrorState parameter:

RAISERROR(@ErrorMessage, @ErrorSeverity, 1,
@ErrorNumber, /* parameter: original error number. */
@ErrorSeverity, /* parameter: original error severity. */
@ErrorState, /* parameter: original error state. */
@ErrorProcedure, /* parameter: original error procedure name. */
@ErrorLine /* parameter: original error line number. */
);

to:

RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState,
@ErrorNumber, /* parameter: original error number. */
@ErrorSeverity, /* parameter: original error severity. */
@ErrorState, /* parameter: original error state. */
@ErrorProcedure, /* parameter: original error procedure name. */
@ErrorLine /* parameter: original error line number. */
);

Now I am getting the correct value on the ex.State property.

--Santiago