From 138991d951b2d53c39f899bf69cd9f8c06c9c553 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 7 Feb 2017 12:43:06 +1100 Subject: [PATCH] OMG Re-rever "DOH! Revert "Fixes database context issue during startup when there is no db configured", i reverted the wrong commit on the wrong branch --- src/Umbraco.Core/DatabaseContext.cs | 11 +++++++++-- src/Umbraco.Core/Persistence/PetaPoco.cs | 4 ++-- src/Umbraco.Core/UmbracoApplicationBase.cs | 10 ++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 5708b03c22..b48ab1bca4 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -110,7 +110,14 @@ namespace Umbraco.Core /// public virtual UmbracoDatabase Database { - get { return _factory.CreateDatabase(); } + get + { + if (IsDatabaseConfigured == false) + { + throw new InvalidOperationException("Cannot create a database instance, there is no available connection string"); + } + return _factory.CreateDatabase(); + } } /// @@ -202,7 +209,7 @@ namespace Umbraco.Core } else { - throw new InvalidOperationException("Can't find a connection string with the name '" + GlobalSettings.UmbracoConnectionName + "'"); + throw new NullReferenceException("Can't find a connection string with the name '" + Constants.System.UmbracoConnectionName + "'"); } return _providerName; } diff --git a/src/Umbraco.Core/Persistence/PetaPoco.cs b/src/Umbraco.Core/Persistence/PetaPoco.cs index b569b1c45d..ba44af2c28 100644 --- a/src/Umbraco.Core/Persistence/PetaPoco.cs +++ b/src/Umbraco.Core/Persistence/PetaPoco.cs @@ -167,12 +167,12 @@ namespace Umbraco.Core.Persistence var providerName = Constants.DatabaseProviders.SqlServer; if (ConfigurationManager.ConnectionStrings[connectionStringName] != null) { - if (!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName)) + if (string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName) == false) providerName = ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName; } else { - throw new InvalidOperationException("Can't find a connection string with the name '" + connectionStringName + "'"); + throw new NullReferenceException("Can't find a connection string with the name '" + connectionStringName + "'"); } // Store factory and connection string diff --git a/src/Umbraco.Core/UmbracoApplicationBase.cs b/src/Umbraco.Core/UmbracoApplicationBase.cs index 92f5a476f9..7f1f0803f8 100644 --- a/src/Umbraco.Core/UmbracoApplicationBase.cs +++ b/src/Umbraco.Core/UmbracoApplicationBase.cs @@ -59,6 +59,16 @@ namespace Umbraco.Core //And now we can dispose of our startup handlers - save some memory ApplicationEventsResolver.Current.Dispose(); + + // after Umbraco has started there is a database in "context" and that context is + // going to stay there and never get destroyed nor reused, so we have to ensure that + // the database is disposed (which will auto-remove it from context). + if (ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + { + var database = ApplicationContext.Current.DatabaseContext.Database; + if (database != null) // never to happen... unless in weird tests + ApplicationContext.Current.DatabaseContext.Database.Dispose(); + } } ///