Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tonny Schou 11 posts 111 karma points
    Sep 01, 2021 @ 13:01
    Tonny Schou
    0

    MediaService.Save SQL Deadlock

    Hi!

    I'm building a website that's supposed to allow registered members to upload images to media folders, and have run into a problem trying to save these images to Media. I've distilled the problem down to this code:

    public List<ImageVM> SaveImage(IEnumerable<HttpPostedFileBase> files, int memberId)
    {
        var folder = GetMemberImageFolder(memberId);
    
        foreach(var file in files)
        {
            var new_image = _mediaService.CreateMedia(file.FileName, folder.Id, Constants.Conventions.MediaTypes.Image, memberId);
            new_image.SetValue(_contentTypeBaseService, "umbracoFile", file.FileName, file);
            _mediaService.Save(new_image);
        }
    }
    

    This function works when saving a single image, but if the files collection contain multiple images, it fails with an SQL exception.

    Transaction (Process ID 51) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
    

    The Save function looks and somewhat behaves as an async function, but isn't, so how do I work around this issue?

Please Sign in or register to post replies

Write your reply to:

Draft