Copied to clipboard

Flag this post as spam?

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


  • keilo 568 posts 1023 karma points
    Nov 01, 2015 @ 22:14
    keilo
    0

    MediaService Saved handler throwing SQL Deadlock

    I am experimenting with the mediaservice event handler, to get a copy of the saved (uploaded) media items to another folder (aka backup).

    The code

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MediaService.Saved += MediaServiceSaved;     
            }   
    
            void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
            {
    
                foreach (var mediaItem in e.SavedEntities)
                {
                     var umbracoFileProperty = mediaItem.Properties["umbracoFile"].Value.ToString(); 
    
     var newfile = .. get file path and server.mappath it
    var fromPath = get target copy directory mappath
    
        var fi = new System.IO.FileInfo(newfile);
    
                if (!fi.Directory.Exists) fi.Directory.Create();    
                System.IO.File.Copy(fromPath,newfile);
            }
    }
    

    When i save a single and/or multiple files from Media Section-> Upload, the file copy (backup) takes place fine but the UI throws error like

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

    Any idea why this is happening? The file copy takes place fine (all within the same host so it occurs within a second or two), do I need to call some method to prevent this?

    Would greatly appreciate if someone can shed some light on this.

Please Sign in or register to post replies

Write your reply to:

Draft