Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Auto retry concurrent commands with ncqrs

, 7 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to auto retry concurrent commands with ncqrs.

In a previous post I showed you some pseudo code Gregory Young mentioned in his DDD CQRS course I attended in Krakow, Poland. In this course Greg made clear to us locking of databases isn’t necessary. He showed us some pseudo code how to easily write a merge handler to handle all concurrency conflicts.

In my current project, based on the ncqrs-framework I implemented a simpler version of this merge handler which only retries each command if a ConcurrencyException occurs. To achieve this you can easily build a wrapper around the ncqrs CommandService

public class SafeCommandService : CommandService 
{
    public override void Execute(ICommand command)
    { 
        try
        {
            base.Execute(command);
        }
        catch(ConcurrencyException ex)
        {
            Execute(command);
            //Log retry executed
        }
    }
}

You can choose to implement this in a WCF service or whatever you want. Every command that fails will be retried until it succeeds. I have tested this with hundred async JavaScript calls which send a real simple command (PrintLabelCommand). My domain only processes a LabelPrintedEvent. When sending these calls to the server my log tells me there are only 8 retries average. This is acceptable in our situation. If you have more complex scenario’s you will probably need a more advanced setup like the pseudo code in this blog post shows you.

I’m still searching for a better name for my class, because SafeCommandExecutor isn’t the best name I think. So let me know if you have a better one. I hope this article will be useful for you guys. As always, please share…

If you improved my code let me know, so I can learn from it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

marcofranssen
Software Developer Atos
Netherlands Netherlands
I am a .NET Software Developer at Atos International. Architecture, CQRS, DDD, C#, ASP.NET, MVC3, HTML5, Jquery, WP7, WPF, ncqrs, Node.js
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 7 Mar 2012
Article Copyright 2012 by marcofranssen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid