diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs index 4fb2e42bb8..f9b6072692 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs @@ -20,6 +20,14 @@ namespace Umbraco.Core.Persistence.UnitOfWork private readonly List _operations = new List(); + /// + /// Creates a new unit of work instance + /// + /// + /// + /// The Database instance used for this unit of work should not be shared with other unit's of work, other repositories, etc... + /// as it will get disposed of when this unit of work is disposed. + /// public PetaPocoUnitOfWork(UmbracoDatabase database) { Database = database; @@ -151,16 +159,15 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// /// Ensures disposable objects are disposed - /// + /// /// - /// We will not dispose the database because this will get disposed of automatically when - /// in the HttpContext by the UmbracoModule because the DatabaseFactory stores the instance in HttpContext.Items - /// when in a web context. - /// When not in a web context, we may possibly be re-using the database context. + /// Ensures that the Database instance is disposed of. As per the constructor documentation, the database instance + /// used to construct this object should not be shared as it will get disposed of when this object is disposed. /// protected override void DisposeResources() { _operations.Clear(); + Database.Dispose(); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs index 886c060123..ce635761db 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs @@ -13,9 +13,20 @@ namespace Umbraco.Core.Persistence.UnitOfWork #region Implementation of IUnitOfWorkProvider + /// + /// Creates a Unit of work with a new UmbracoDatabase instance for the work item/transaction. + /// + /// + /// + /// Each PetaPoco UOW uses it's own Database object, not the shared Database object that comes from + /// the DatabaseContext.Current.Database. This is because each transaction should use it's own Database + /// and we Dispose of this Database object when the UOW is disposed. + /// public IDatabaseUnitOfWork GetUnitOfWork() { - return new PetaPocoUnitOfWork(DatabaseContext.Current.Database); + return new PetaPocoUnitOfWork( + new UmbracoDatabase( + GlobalSettings.UmbracoConnectionName)); } #endregion