public async Task <Unit> Handle(GeoDatabaseUpdated request, CancellationToken token) { try { _eventStore.Clear(); await _geoDatabase.BeginTransaction(); if (request.UpdateMessage is RouteNodeMessage) { await HandleRouteNode((RouteNodeMessage)request.UpdateMessage); } else if (request.UpdateMessage is RouteSegmentMessage) { await HandleRouteSegment((RouteSegmentMessage)request.UpdateMessage); } else if (request.UpdateMessage is InvalidMessage) { var message = (InvalidMessage)request.UpdateMessage; if (message.Delete) { // We only do this in very special cases where we cannot rollback await MarkToBeDeleted((request.UpdateMessage as InvalidMessage).Message, "Message is invalid and we cannot rollback so we mark it to be deleted."); await _geoDatabase.Commit(); return(await Task.FromResult(new Unit())); } else { await RollbackOrDelete((request.UpdateMessage as InvalidMessage).Message, "Message is invalid so we rollback or delete"); } } var editOperationOccuredEvent = CreateEditOperationOccuredEvent(request.UpdateMessage); if (IsOperationEditEventValid(editOperationOccuredEvent)) { if (_eventStore.Get().Count() > 0) { await _producer.Produce(_kafkaSettings.EventRouteNetworkTopicName, editOperationOccuredEvent); } await _geoDatabase.Commit(); if (_eventStore.Get().Count() > 0 && _applicationSettings.SendGeographicalAreaUpdatedNotification) { await _mediator.Publish(new GeographicalAreaUpdated { RouteNodes = _modifiedGeometriesStore.GetRouteNodes(), RouteSegment = _modifiedGeometriesStore.GetRouteSegments() }); } } else { _logger.LogError($"{nameof(RouteNetworkEditOperationOccuredEvent)} is not valid so we rollback."); await _geoDatabase.RollbackTransaction(); await _geoDatabase.BeginTransaction(); await RollbackOrDelete(request.UpdateMessage, $"Rollback or delete because {nameof(RouteNetworkEditOperationOccuredEvent)} is not valid."); await _geoDatabase.Commit(); _logger.LogInformation($"{nameof(RouteNetworkEditOperationOccuredEvent)} is now rolled rollback."); } } catch (Exception e) { _logger.LogError($"{e.ToString()}: Rolling back geodatabase transactions"); await _geoDatabase.RollbackTransaction(); await _geoDatabase.BeginTransaction(); await RollbackOrDelete(request.UpdateMessage, $"Rollback or delete because of exception: {e.Message}"); await _geoDatabase.Commit(); } finally { _eventStore.Clear(); _modifiedGeometriesStore.Clear(); await _geoDatabase.DisposeTransaction(); await _geoDatabase.DisposeConnection(); } return(await Task.FromResult(new Unit())); }