DB2®Command.Cancel Method
Attempts to cancel the execution of a DB2Command.
- Namespace:
IBM.Data.DB2- Assembly:
IBM.Data.DB2(inIBM.Data.DB2.dll)
Syntax
[Visual Basic]
NotOverridable Public Sub Cancel()
[C#]
public void Cancel();
[C++]
public: __sealed void Cancel();
[JScript]
public function Cancel();
Remarks
If there is nothing to cancel, nothing happens. However, if there is a command in process, and the attempt to cancel fails, no exception is generated.
Example
The following example creates two threads, one thread for running the long query and another thread for canceling the long query after waiting for 5 seconds. To accomplish this, the method is passed a connection string which is used to connect to the database.
Here is the Visual Basic code for this
task:
Public Sub CreateMyDB2Command _
(ByVal myConnectionString As String)
Dim mySelectQuery As String = "select * from emp, dept, empact"
Dim myConnection As New DB2Connection(myConnectionString)
Dim myCommand As New DB2Command(mySelectQuery, myConnection)
Dim cmdThread As New Thread(AddressOf do_Cmd)
Dim cancelThread As New Thread(AddressOf do_Cancel)
myCommand.Connection.Open()
Console.WriteLine("Starting the command thread")
cmdThread = New Thread(New ParameterizedThreadStart(AddressOf do_Cmd))
cmdThread.Start(myCommand)
Console.WriteLine("Starting the cancel thread")
cancelThread = New Thread(New ParameterizedThreadStart(AddressOf do_Cancel))
cancelThread.Start(myCommand)
cmdThread.Join()
cancelThread.Join()
End Sub
Public Sub do_Cancel(ByVal data As Object)
Dim cmd As New DB2Command()
cmd = data
'sleep for 5 seconds before starting to cancel the command
Thread.Sleep(5000)
Try
cmd.Cancel()
Console.WriteLine("SQL command cancelled")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Public Sub do_Cmd(ByVal data As Object)
Dim cmd As New DB2Command()
Dim reader As DB2DataReader
cmd = data
Console.WriteLine("Running a long query ...")
Try
reader = cmd.ExecuteReader()
While (reader.Read())
Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1))
End While
Console.WriteLine("Query finished")
reader.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Here is the C # code for this
task:
public void CreateMyDB2Command(string myConnectionString)
{
string mySelectQuery = "select * from emp, dept, empact";
DB2Connection myConnection = new DB2Connection(myConnectionString);
DB2Command myCommand = new DB2Command(mySelectQuery, myConnection);
myCommand.Connection.Open();
System.Console.WriteLine("Starting the command thread");
Thread cmdThread = new Thread(new ParameterizedThreadStart(do_Cmd));
cmdThread.Start(myCommand);
System.Console.WriteLine("Starting the cancel thread");
Thread cancelThread = new Thread(new ParameterizedThreadStart(do_Cancel));
cancelThread.Start(myCommand);
cmdThread.Join();
cancelThread.Join();
}
public static void do_Cancel(object data)
{
DB2Command cmd = (DB2Command)data;
// sleep for 5 seconds before starting to cancel the command
Thread.Sleep(5000);
try
{
cmd.Cancel();
System.Console.WriteLine("SQL command cancelled");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void do_Cmd(object data)
{
DB2Command cmd = (DB2Command)data;
System.Console.WriteLine("Running a long query ...");
try
{
DB2DataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
System.Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1));
}
System.Console.WriteLine("Query finished");
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Note: DB2Command.Cancel will not close the cursor
associated with the reader object (such as DB2DataReader or DB2ResultSet)
created by using the DB2Command object. The application need to call
the Close method on the reader object in order to release database
resources associated with the reader object.