Changed the PetaPocoUnitOfWorkProvider to ensure that a new Database object is used for each new

unit of work (transaction). When the UOW is diposed, the database instance will also be disposed.
This commit is contained in:
Shannon Deminick
2012-12-12 05:57:52 +05:00
parent 1132547c24
commit a3938edde2
2 changed files with 24 additions and 6 deletions

View File

@@ -20,6 +20,14 @@ namespace Umbraco.Core.Persistence.UnitOfWork
private readonly List<Operation> _operations = new List<Operation>();
/// <summary>
/// Creates a new unit of work instance
/// </summary>
/// <param name="database"></param>
/// <remarks>
/// 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.
/// </remarks>
public PetaPocoUnitOfWork(UmbracoDatabase database)
{
Database = database;
@@ -151,16 +159,15 @@ namespace Umbraco.Core.Persistence.UnitOfWork
/// <summary>
/// Ensures disposable objects are disposed
/// </summary>
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
protected override void DisposeResources()
{
_operations.Clear();
Database.Dispose();
}
}
}

View File

@@ -13,9 +13,20 @@ namespace Umbraco.Core.Persistence.UnitOfWork
#region Implementation of IUnitOfWorkProvider
/// <summary>
/// Creates a Unit of work with a new UmbracoDatabase instance for the work item/transaction.
/// </summary>
/// <returns></returns>
/// <remarks>
/// 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.
/// </remarks>
public IDatabaseUnitOfWork GetUnitOfWork()
{
return new PetaPocoUnitOfWork(DatabaseContext.Current.Database);
return new PetaPocoUnitOfWork(
new UmbracoDatabase(
GlobalSettings.UmbracoConnectionName));
}
#endregion