Can't stop SSIS Package at runtime (.NET C#)
I'm trying to Stop SSIS Packages during runtime with this code
RunningPackages开发者_开发知识库 pkgs = app.GetRunningPackages("myserver");
foreach (RunningPackage p in pkgs)
{
Console.WriteLine("PackageName: " + p.PackageName);
p.Stop();
}
No errors during runtime
However, when I debug to pause between stopping each packages and go to view running packages in the SQL SERVER Managment Studio, I still see all the packages running.
I even try to right-click and choose stop on the running packages and they won't stop.
If anyone has any insight into this please advise, I've combed through Google and can't find anything. Any help will be greatly appreciated.
According to MSDN,
The Stop method issues a stop request to the Integration Services service, but this does not stop packages immediately. There may be a delay between the time a stop request is issued and the time that packages actually stop.
This is the safe approach implemented (I can try and explain why/how if you want), but if you really must kill the package execution then and there, you need to kill the process that's executing it. Let me know if you need help with this as well.
However, I recommend that your control center use multiple threads with callback to achieve what you're trying to do. Your control center can issue a request to stop the running packages on a separate thread. Your separate thread can also be responsible for notifying your control center's primary thread to do whatever it is you're trying to do once the packages have stopped running. Or, if a synchronous approach works for you, then great.
EDIT: Explaining the "safe approach" and how to perform the "unsafe approach"
From what I understand, SSIS packages only check for stop requests in between tasks. That said, if the package is in the middle of executing a long Data Flow task, you must wait until the Data Flow task completes. This is to preserve data integrity and to prevent inconsistent states.
So then, what can you do to stop a package when it's in the middle of executing a long Data Flow task? As I said earlier, you'd have to kill the process running the package (with understanding that this may lead to data corruption and other negative things).
using System.Diagnostic;
...
void StopAllPackages() {
// Get all processes executing an SSIS package on the database server.
// If you run this on the database server itself, remove the second parameter
// from GetProcessesByName.
foreach(Process p in Process.GetProcessesByName("dtexec.exe","databaseServerMachine") {
try { p.Kill(); }
catch(Win32Exception w32e) {
// process was already terminating or can't be terminated
}
catch(InvalidOperationException ioe) {
// process has already exited
}
}
}
Another way to do this is to figure out the process ID on SQL Server (i.e. if you view Activity Monitor) and issue a request to kill the SQL Server process that's associated with the package execution.
精彩评论