mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Fix: Exceptions in commands do cancel transactions (same thing in background tasks)
Components that control using() scope for work context will also indicate transaction cancelled when an exception passes out of that scope See http://orchard.codeplex.com/workitem/16604 --HG-- branch : dev extra : rebase_source : 2d53cdbea1e9dd7ceb022f781621b01bb1f95a87
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Autofac;
|
||||
using Orchard.Caching;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.State;
|
||||
@@ -48,6 +49,8 @@ namespace Orchard.Commands {
|
||||
tenant = tenant ?? "Default";
|
||||
|
||||
using (var env = CreateStandaloneEnvironment(tenant)) {
|
||||
var commandManager = env.Resolve<ICommandManager>();
|
||||
var transactionManager = env.Resolve<ITransactionManager>();
|
||||
|
||||
var parameters = new CommandParameters {
|
||||
Arguments = args,
|
||||
@@ -56,7 +59,16 @@ namespace Orchard.Commands {
|
||||
Output = output
|
||||
};
|
||||
|
||||
env.Resolve<ICommandManager>().Execute(parameters);
|
||||
try {
|
||||
commandManager.Execute(parameters);
|
||||
}
|
||||
catch {
|
||||
// any database changes in this using(env) scope are invalidated
|
||||
transactionManager.Cancel();
|
||||
|
||||
// exception handling performed below
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// in effect "pump messages" see PostMessage circa 1980
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Timers;
|
||||
using Autofac;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Logging;
|
||||
|
||||
@@ -57,9 +58,20 @@ namespace Orchard.Tasks {
|
||||
public void DoWork() {
|
||||
// makes an inner container, similar to the per-request container
|
||||
using (var scope = _workContextAccessor.CreateWorkContextScope()) {
|
||||
// resolve the manager and invoke it
|
||||
var manager = scope.Resolve<IBackgroundService>();
|
||||
manager.Sweep();
|
||||
var transactionManager = scope.Resolve<ITransactionManager>();
|
||||
|
||||
try {
|
||||
// resolve the manager and invoke it
|
||||
var manager = scope.Resolve<IBackgroundService>();
|
||||
manager.Sweep();
|
||||
}
|
||||
catch {
|
||||
// any database changes in this using scope are invalidated
|
||||
transactionManager.Cancel();
|
||||
|
||||
// pass exception along to actual handler
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user