Help us, please :)

We use SessionManager, so default isolation level, as I understand, is ReadCommitted.

We call one method asynchronously by ajax request, there are two simultaneous request.

And we always get deadlock. (code is simplified and "unrefactored")

  public PhotoDto AddPhoto(int id, HttpPostedFileBase file)
    {
        var block = Session.Demand().Query.All<PhotoGalleryBlock>().FirstOrDefault(b => b.Id == id);

        var photo = new Photo
        {
            FileName = "z"
        };

        //after uncommenting this line there is no deadlock on #1, but it happens on #2
        //###block.PhotoSet.ToList();
        block.PhotoSet.Add(photo);

        var user = block.Page.Site.User;

        var blocks = user.Site.PageSet.FirstOrDefault(p => p.IsMain).BlockSet;

        var photoGalleryBlocks = blocks.OfType<PhotoGalleryBlock>();

        foreach (var photoGalleryBlock in photoGalleryBlocks)
        {
            //#1
            foreach (var photoi in photoGalleryBlock.PhotoSet)
            {
                var x = 5;
            }

        }
        //DiskSpaces - Structure
        //#2
        user.DiskSpaces.PhotoGallery = 5;

        return new PhotoDto(photo);

    }

We get deadlock on foreach, trying to enumerate photoGalleryBlock.PhotoSet.

After uncommenting ### we get another deadlock on #2.

Actually, not on #2, but when transaction is completed, Sql Server throws an error "deadlock on update User set [DiskSpaces.PhotoGallery] = 5"

Thanks in advance.

asked Mar 21 '12 at 14:01

Ness's gravatar image

Ness
155232328


One Answer:

This information is not enough for catch this deadlock. You can use SQL Server Profiler with TSQL_Locks template or implement correct handling of this deadlock with reprocessing library

answered Mar 27 '12 at 11:23

proff's gravatar image

proff
75336

Thanks, din't know about this library.

(Mar 27 '12 at 12:04) Ness Ness's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA