Base solution for your next web application
Open Closed

EventBus Domain Events issue with concurency #4109


User avatar
0
soonjoo created

Hello team,

I have created a simple revision tracking for my entity. Whenever this type of entity is created, updated, or deleted, i will store a snapshot of the new values into a mirror of that entity table. When I create a new entity, the first time is successful. However, the second time I create the entity, I get an internal server error. My application did not break on the exception throw, I had to go to the logs to check the error. Basically the error says

System.InvalidOperationException: The connection does not support MultipleActiveResultSets.

Not really sure why the connection is still open..

Basically I had 2 pieces of code:

public class SampleRevisionHandlers :
        AgrinomeDomainServiceBase,
        IEventHandler<EntityCreatingEventData<Sample>>,
        IEventHandler<EntityUpdatingEventData<Sample>>,
        IEventHandler<EntityDeletingEventData<Sample>>,
        ITransientDependency
    {

        private readonly ISampleRevisionManager _sampleRevisionManager;

        public SampleRevisionHandlers(
            ISampleRevisionManager sampleRevisionManager)
        {
            _sampleRevisionManager = sampleRevisionManager;
        }

        public void HandleEvent(EntityCreatingEventData<Sample> eventData)
        {
            var sample = eventData.Entity;
            _sampleRevisionManager.CreateNewSampleRevision(sample, "CREATE");
        }

        public void HandleEvent(EntityUpdatingEventData<Sample> eventData)
        {
            var sample = eventData.Entity;
            _sampleRevisionManager.CreateNewSampleRevision(sample, "UPDATE");
        }

        public void HandleEvent(EntityDeletingEventData<Sample> eventData)
        {
            var sample = eventData.Entity;
            _sampleRevisionManager.CreateNewSampleRevision(sample, "DELETE");
        }
        
    }
public class SampleRevisionManager : DomainService, ISampleRevisionManager
    {

        private readonly IRepository<SampleRevision, long> _sampleRevisionRepository;

        public SampleRevisionManager(
            IRepository<SampleRevision, long> sampleRevisionRepository)
        {
            _sampleRevisionRepository = sampleRevisionRepository;
        }

        [UnitOfWork]
        public void CreateNewSampleRevision(Sample sample, string action)
        {
            var latestRevision = _sampleRevisionRepository.GetAll().OrderByDescending(e => e.Id).Where(e => e.SampleId == sample.Id).FirstOrDefault();
            long? latestRevisionId = null;
            if (latestRevision != null)
            {
                latestRevisionId = latestRevision.Id;
            }

            var sampleRevision = new SampleRevision(sample)
            {
                RevisionAction = action,
                PreviousRevisionId = latestRevisionId
            };

            sampleRevision = _sampleRevisionRepository.Insert(sampleRevision);
        }
    }

Any ideas why it would keep the connection open??

Thank you


3 Answer(s)
  • User Avatar
    0
    soonjoo created

    I get this error too occasionally

    Mvc.ExceptionHandling.AbpExceptionFilter - A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. System.InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

    I have a suspicion that its because my entity has a self referencing relationship... I think self referencing relationship doesn't play too well with domain events?

  • User Avatar
    0
    soonjoo created

    I have found the issue, its because I wasn't awaiting an async call.. sorry for the inconvenience.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks for the feedback @soonjoo :)