Browse Source

fixed db disposals

Luke Pulverenti 12 years ago
parent
commit
eb612bd303

+ 0 - 4
MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs

@@ -1,12 +1,8 @@
 using MediaBrowser.Controller.Dto;
 using MediaBrowser.Controller.Dto;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Entities.Audio;
-using MediaBrowser.Controller.Entities.Movies;
-using MediaBrowser.Controller.Entities.TV;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Persistence;
 using MediaBrowser.Controller.Persistence;
 using MediaBrowser.Model.Dto;
 using MediaBrowser.Model.Dto;
-using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.Querying;
 using MediaBrowser.Model.Querying;
 using ServiceStack.ServiceHost;
 using ServiceStack.ServiceHost;
 using System;
 using System;

+ 0 - 1
MediaBrowser.Api/UserLibrary/YearsService.cs

@@ -2,7 +2,6 @@
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Persistence;
 using MediaBrowser.Controller.Persistence;
 using ServiceStack.ServiceHost;
 using ServiceStack.ServiceHost;
-using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Globalization;
 using System.Globalization;
 using System.Linq;
 using System.Linq;

+ 7 - 0
MediaBrowser.Controller/IServerApplicationHost.cs

@@ -3,8 +3,15 @@ using MediaBrowser.Model.System;
 
 
 namespace MediaBrowser.Controller
 namespace MediaBrowser.Controller
 {
 {
+    /// <summary>
+    /// Interface IServerApplicationHost
+    /// </summary>
     public interface IServerApplicationHost : IApplicationHost
     public interface IServerApplicationHost : IApplicationHost
     {
     {
+        /// <summary>
+        /// Gets the system info.
+        /// </summary>
+        /// <returns>SystemInfo.</returns>
         SystemInfo GetSystemInfo();
         SystemInfo GetSystemInfo();
     }
     }
 }
 }

+ 10 - 1
MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs

@@ -1,4 +1,3 @@
-using System.IO;
 using Funq;
 using Funq;
 using MediaBrowser.Common;
 using MediaBrowser.Common;
 using MediaBrowser.Common.Extensions;
 using MediaBrowser.Common.Extensions;
@@ -17,6 +16,7 @@ using ServiceStack.WebHost.Endpoints.Support;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Globalization;
 using System.Globalization;
+using System.IO;
 using System.Linq;
 using System.Linq;
 using System.Net;
 using System.Net;
 using System.Net.WebSockets;
 using System.Net.WebSockets;
@@ -498,6 +498,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer
             _logger.Info("Calling ServiceStack AppHost.Init");
             _logger.Info("Calling ServiceStack AppHost.Init");
             Init();
             Init();
         }
         }
+
+        /// <summary>
+        /// Releases the specified instance.
+        /// </summary>
+        /// <param name="instance">The instance.</param>
+        public override void Release(object instance)
+        {
+            // Leave this empty so SS doesn't try to dispose our objects
+        }
     }
     }
 
 
     /// <summary>
     /// <summary>

+ 24 - 21
MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs

@@ -160,27 +160,30 @@ namespace MediaBrowser.Server.Implementations.Sqlite
             {
             {
                 try
                 try
                 {
                 {
-                    if (connection != null)
+                    lock (this)
                     {
                     {
-                        if (EnableDelayedCommands)
+                        if (connection != null)
                         {
                         {
-                            FlushOnDispose();
+                            if (EnableDelayedCommands)
+                            {
+                                FlushOnDispose();
+                            }
+
+                            if (connection.IsOpen())
+                            {
+                                connection.Close();
+                            }
+
+                            connection.Dispose();
+                            connection = null;
                         }
                         }
-                        
-                        if (connection.IsOpen())
+
+                        if (FlushTimer != null)
                         {
                         {
-                            connection.Close();
+                            FlushTimer.Dispose();
+                            FlushTimer = null;
                         }
                         }
-
-                        connection.Dispose();
-                    }
-
-                    if (FlushTimer != null)
-                    {
-                        FlushTimer.Dispose();
-                        FlushTimer = null;
                     }
                     }
-
                 }
                 }
                 catch (Exception ex)
                 catch (Exception ex)
                 {
                 {
@@ -195,13 +198,13 @@ namespace MediaBrowser.Server.Implementations.Sqlite
         private void FlushOnDispose()
         private void FlushOnDispose()
         {
         {
             // If we're not already flushing, do it now
             // If we're not already flushing, do it now
-            if (!IsFlushing)
+            if (!_isFlushing)
             {
             {
                 Flush(null);
                 Flush(null);
             }
             }
 
 
             // Don't dispose in the middle of a flush
             // Don't dispose in the middle of a flush
-            while (IsFlushing)
+            while (_isFlushing)
             {
             {
                 Thread.Sleep(25);
                 Thread.Sleep(25);
             }
             }
@@ -225,7 +228,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite
         /// <summary>
         /// <summary>
         /// The is flushing
         /// The is flushing
         /// </summary>
         /// </summary>
-        private bool IsFlushing;
+        private bool _isFlushing;
 
 
         /// <summary>
         /// <summary>
         /// Flushes the specified sender.
         /// Flushes the specified sender.
@@ -241,12 +244,12 @@ namespace MediaBrowser.Server.Implementations.Sqlite
                 return;
                 return;
             }
             }
 
 
-            if (IsFlushing)
+            if (_isFlushing)
             {
             {
                 return;
                 return;
             }
             }
 
 
-            IsFlushing = true;
+            _isFlushing = true;
             var numCommands = 0;
             var numCommands = 0;
 
 
             using (var tran = connection.BeginTransaction())
             using (var tran = connection.BeginTransaction())
@@ -278,7 +281,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite
             Logger.Debug("SQL Delayed writer executed " + numCommands + " commands");
             Logger.Debug("SQL Delayed writer executed " + numCommands + " commands");
 
 
             FlushTimer.Change(TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1));
             FlushTimer.Change(TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1));
-            IsFlushing = false;
+            _isFlushing = false;
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 22 - 28
MediaBrowser.ServerApplication/ApplicationHost.cs

@@ -159,6 +159,9 @@ namespace MediaBrowser.ServerApplication
         /// </summary>
         /// </summary>
         /// <value>The user data repository.</value>
         /// <value>The user data repository.</value>
         private IUserDataRepository UserDataRepository { get; set; }
         private IUserDataRepository UserDataRepository { get; set; }
+        private IUserRepository UserRepository { get; set; }
+        private IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
+        private IItemRepository ItemRepository { get; set; }
 
 
         /// <summary>
         /// <summary>
         /// The full path to our startmenu shortcut
         /// The full path to our startmenu shortcut
@@ -239,6 +242,15 @@ namespace MediaBrowser.ServerApplication
             UserDataRepository = new SQLiteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
             UserDataRepository = new SQLiteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
             RegisterSingleInstance(UserDataRepository);
             RegisterSingleInstance(UserDataRepository);
 
 
+            UserRepository = new SQLiteUserRepository(ApplicationPaths, JsonSerializer, LogManager);
+            RegisterSingleInstance(UserRepository);
+
+            DisplayPreferencesRepository = new SQLiteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
+            RegisterSingleInstance(DisplayPreferencesRepository);
+
+            ItemRepository = new SQLiteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
+            RegisterSingleInstance(ItemRepository);
+
             UserManager = new UserManager(Logger, ServerConfigurationManager, UserDataRepository);
             UserManager = new UserManager(Logger, ServerConfigurationManager, UserDataRepository);
             RegisterSingleInstance(UserManager);
             RegisterSingleInstance(UserManager);
 
 
@@ -299,11 +311,9 @@ namespace MediaBrowser.ServerApplication
         /// <returns>Task.</returns>
         /// <returns>Task.</returns>
         private async Task ConfigureDisplayPreferencesRepositories()
         private async Task ConfigureDisplayPreferencesRepositories()
         {
         {
-            var repository = new SQLiteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
-
-            await repository.Initialize().ConfigureAwait(false);
+            await DisplayPreferencesRepository.Initialize().ConfigureAwait(false);
 
 
-            ((DisplayPreferencesManager)DisplayPreferencesManager).Repository = repository;
+            ((DisplayPreferencesManager)DisplayPreferencesManager).Repository = DisplayPreferencesRepository;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -312,11 +322,9 @@ namespace MediaBrowser.ServerApplication
         /// <returns>Task.</returns>
         /// <returns>Task.</returns>
         private async Task ConfigureItemRepositories()
         private async Task ConfigureItemRepositories()
         {
         {
-            var repository = new SQLiteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
+            await ItemRepository.Initialize().ConfigureAwait(false);
 
 
-            await repository.Initialize().ConfigureAwait(false);
-
-            ((LibraryManager)LibraryManager).ItemRepository = repository;
+            ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -328,13 +336,15 @@ namespace MediaBrowser.ServerApplication
             return UserDataRepository.Initialize();
             return UserDataRepository.Initialize();
         }
         }
 
 
+        /// <summary>
+        /// Configures the user repositories.
+        /// </summary>
+        /// <returns>Task.</returns>
         private async Task ConfigureUserRepositories()
         private async Task ConfigureUserRepositories()
         {
         {
-            var repository = new SQLiteUserRepository(ApplicationPaths, JsonSerializer, LogManager);
+            await UserRepository.Initialize().ConfigureAwait(false);
 
 
-            await repository.Initialize().ConfigureAwait(false);
-
-            ((UserManager)UserManager).UserRepository = repository;
+            ((UserManager)UserManager).UserRepository = UserRepository;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -553,21 +563,5 @@ namespace MediaBrowser.ServerApplication
                 process.WaitForExit();
                 process.WaitForExit();
             }
             }
         }
         }
-
-        /// <summary>
-        /// Gets the repository.
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="repositories">The repositories.</param>
-        /// <param name="name">The name.</param>
-        /// <returns>``0.</returns>
-        private T GetRepository<T>(IEnumerable<T> repositories, string name)
-            where T : class, IRepository
-        {
-            var enumerable = repositories as T[] ?? repositories.ToArray();
-
-            return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
-                   enumerable.FirstOrDefault();
-        }
     }
     }
 }
 }

+ 2 - 2
MediaBrowser.ServerApplication/EntryPoints/NewItemNotifier.cs

@@ -1,5 +1,4 @@
-using System.Windows;
-using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Model.Logging;
 using MediaBrowser.Model.Logging;
@@ -7,6 +6,7 @@ using MediaBrowser.ServerApplication.Controls;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Threading;
 using System.Threading;
+using System.Windows;
 using System.Windows.Controls.Primitives;
 using System.Windows.Controls.Primitives;
 
 
 namespace MediaBrowser.ServerApplication.EntryPoints
 namespace MediaBrowser.ServerApplication.EntryPoints

+ 3 - 3
MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs

@@ -1,6 +1,4 @@
-using System.Linq;
-using System.Threading;
-using MediaBrowser.Common.Events;
+using MediaBrowser.Common.Events;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Common.Plugins;
 using MediaBrowser.Common.Plugins;
 using MediaBrowser.Common.ScheduledTasks;
 using MediaBrowser.Common.ScheduledTasks;
@@ -15,6 +13,8 @@ using MediaBrowser.Model.Logging;
 using MediaBrowser.Model.Tasks;
 using MediaBrowser.Model.Tasks;
 using MediaBrowser.Model.Updates;
 using MediaBrowser.Model.Updates;
 using System;
 using System;
+using System.Linq;
+using System.Threading;
 
 
 namespace MediaBrowser.ServerApplication.EntryPoints
 namespace MediaBrowser.ServerApplication.EntryPoints
 {
 {