diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 43a755a780..54e3ad796a 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -86,18 +86,6 @@ namespace Umbraco.Core _configured = true; } -#if DEBUG_DATABASES - public List Databases - { - get - { - var factory = _factory as DefaultDatabaseFactory; - if (factory == null) throw new NotSupportedException(); - return factory.Databases; - } - } -#endif - public ISqlSyntaxProvider SqlSyntax { get; private set; } /// diff --git a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs index 87a6263351..891c4dd295 100644 --- a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs +++ b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs @@ -64,32 +64,6 @@ namespace Umbraco.Core.Persistence return ScopeProvider.AmbientOrNoScope.Database; } -#if DEBUG_DATABASES - // helps identifying when non-httpContext databases are created by logging the stack trace - private void LogCallContextStack() - { - var trace = Environment.StackTrace; - if (trace.IndexOf("ScheduledPublishing") > 0) - LogHelper.Debug("CallContext: Scheduled Publishing"); - else if (trace.IndexOf("TouchServerTask") > 0) - LogHelper.Debug("CallContext: Server Registration"); - else if (trace.IndexOf("LogScrubber") > 0) - LogHelper.Debug("CallContext: Log Scrubber"); - else - LogHelper.Debug("CallContext: " + Environment.StackTrace); - } - - private readonly List _databases = new List(); - - // helps identifying database leaks by keeping track of all instances - public List Databases { get { return _databases; } } - - private static void Log(string message, UmbracoDatabase database) - { - LogHelper.Debug(message + " (" + (database == null ? "" : database.InstanceSid) + ")."); - } -#endif - public UmbracoDatabase CreateNewDatabase() { return CreateDatabaseInstance(); diff --git a/src/Umbraco.Core/Scoping/Scope.cs b/src/Umbraco.Core/Scoping/Scope.cs index cdfa24310a..36df7e546b 100644 --- a/src/Umbraco.Core/Scoping/Scope.cs +++ b/src/Umbraco.Core/Scoping/Scope.cs @@ -53,7 +53,7 @@ namespace Umbraco.Core.Scoping public Scope ParentScope { get; set; } // the original scope (when attaching a detachable scope) - public Scope OrigScope { get; set; } + public IScope OrigScope { get; set; } /// public UmbracoDatabase Database diff --git a/src/Umbraco.Core/Scoping/ScopeProvider.cs b/src/Umbraco.Core/Scoping/ScopeProvider.cs index facd0f746c..9a1b5d18cd 100644 --- a/src/Umbraco.Core/Scoping/ScopeProvider.cs +++ b/src/Umbraco.Core/Scoping/ScopeProvider.cs @@ -109,27 +109,12 @@ namespace Umbraco.Core.Scoping { var otherScope = other as Scope; if (otherScope == null) - throw new ArgumentException(); + throw new ArgumentException("Not a Scope instance."); if (otherScope.Detachable == false) - throw new ArgumentException(); + throw new ArgumentException("Not a detachable scope."); - var ambient = AmbientScope; - if (ambient == null) - { - AmbientScope = other; - return; - } - - var noScope = ambient as NoScope; - if (noScope != null) - throw new InvalidOperationException(); - - var scope = ambient as Scope; - if (ambient != null && scope == null) - throw new Exception(); - - otherScope.OrigScope = scope; + otherScope.OrigScope = AmbientScope; AmbientScope = otherScope; } @@ -138,18 +123,18 @@ namespace Umbraco.Core.Scoping { var ambient = AmbientScope; if (ambient == null) - throw new InvalidOperationException(); + throw new InvalidOperationException("There is no ambient scope."); var noScope = ambient as NoScope; if (noScope != null) - throw new InvalidOperationException(); + throw new InvalidOperationException("Cannot detach NoScope."); var scope = ambient as Scope; if (scope == null) - throw new Exception(); + throw new Exception("Ambient scope is not a Scope instance."); if (scope.Detachable == false) - throw new InvalidOperationException(); + throw new InvalidOperationException("Ambient scope is not detachable."); AmbientScope = scope.OrigScope; scope.OrigScope = null; @@ -170,12 +155,12 @@ namespace Umbraco.Core.Scoping // peta poco nulls the shared connection after each command unless there's a trx var database = noScope.DatabaseOrNull; if (database != null && database.InTransaction) - throw new Exception(); + throw new Exception("NoScope is in a transaction."); return AmbientScope = new Scope(this, noScope); } var scope = ambient as Scope; - if (scope == null) throw new Exception(); + if (scope == null) throw new Exception("Ambient scope is not a Scope instance."); return AmbientScope = new Scope(this, scope); } @@ -189,5 +174,37 @@ namespace Umbraco.Core.Scoping StaticScopeReference.Dispose(); } + +#if DEBUG_SCOPES + // this code needs TLC + // the idea is to keep in a list all the scopes that have been created + // and remove them when they are disposed + // so we can track leaks + + // helps identifying when non-httpContext scopes are created by logging the stack trace + private void LogCallContextStack() + { + var trace = Environment.StackTrace; + if (trace.IndexOf("ScheduledPublishing") > 0) + LogHelper.Debug("CallContext: Scheduled Publishing"); + else if (trace.IndexOf("TouchServerTask") > 0) + LogHelper.Debug("CallContext: Server Registration"); + else if (trace.IndexOf("LogScrubber") > 0) + LogHelper.Debug("CallContext: Log Scrubber"); + else + LogHelper.Debug("CallContext: " + Environment.StackTrace); + } + + private readonly List _scopes = new List(); + + // helps identifying scope leaks by keeping track of all instances + public List Scopes { get { return _scopes; } } + + private static void Log(string message, UmbracoDatabase database) + { + LogHelper.Debug(message + " (" + (database == null ? "" : database.InstanceSid) + ")."); + } +#endif + } }