Cancelling a connection or a query while it is being executed

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
euthymos
Posts: 5
Joined: Wed 22 Nov 2006 16:55

Cancelling a connection or a query while it is being executed

Post by euthymos » Wed 22 Nov 2006 17:01

Dear developers,

I've download MyDAC trial version because I was searching for a MySQL VCL Components suite that allowed me to cancel a connection or a query while it is being executed.

Will you please suggest me how can I allow the user to click a "cancel" button on a form so that the connection attempt is simply cancelled without falling into timeout?


Thank you :)

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 23 Nov 2006 16:17

Meanwhile MyDAC does not support such feature. It will be added to the next (fifth) version of MyDAC. The beta version of MyDAC 5 will be released soon.
Meanwhile you can try to implement such behaviour by executing KILL command for the hanging session from another session.

euthymos
Posts: 5
Joined: Wed 22 Nov 2006 16:55

Post by euthymos » Thu 23 Nov 2006 16:55

Please, explain me how.

Zagawa
Posts: 21
Joined: Fri 12 May 2006 09:56

Post by Zagawa » Fri 24 Nov 2006 08:17

Also you can try to store in a variable the current connectionid of Mysql with this query .

'select CONNECTION_ID();'

After this, send your query in a thread.

When click on "cancel" button.
Make a new connection on Mysql and send the query :

'kill '+Variable

cya

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 24 Nov 2006 10:09

You can use MyConnection.ThreadId instead of execution of 'select CONNECTION_ID();'.

euthymos
Posts: 5
Joined: Wed 22 Nov 2006 16:55

Post by euthymos » Tue 28 Nov 2006 11:30

Zagawa wrote:Also you can try to store in a variable the current connectionid of Mysql with this query .

'select CONNECTION_ID();'

After this, send your query in a thread.

When click on "cancel" button.
Make a new connection on Mysql and send the query :

'kill '+Variable

cya
Please, can you be more precise?

Make a new connection... to which server? Not the same server as the primary connnection, I suppose...
Can you make me a practical example? I really would like to buy your libraries, but my precise objective is to develop this cancel procedure, it's a critical fact.

Thank you.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Tue 28 Nov 2006 16:25

The code may look like the following:

- the code in the addition thread you are reqired to create:

Code: Select all

  MyConnection1.Connect;
  id := MyConnection1.ThreadId;
  MyQuery1.Connection := MyConnection1;
  MyQuery1.SQL.Text := ;
  try
    MyQuery1.Execute;
  except
   // some code to handle Lost Connection error
  end
- the handler of the Cancel button:

Code: Select all

  MyQuery2.Connection := MyConnection2;
  MyQuery2.SQL.Text := 'KILL ' + IntToStr(id);
  MyQuery2.Execute // this will cause the Lost Connection error for MyConnection1
MyConnection1 and MyConnection2 are connected to the same MySQL Server.

euthymos
Posts: 5
Joined: Wed 22 Nov 2006 16:55

Post by euthymos » Tue 28 Nov 2006 16:43

Antaeus wrote:The code may look like the following:

- the code in the addition thread you are reqired to create:

Code: Select all

  MyConnection1.Connect;
  id := MyConnection1.ThreadId;
  MyQuery1.Connection := MyConnection1;
  MyQuery1.SQL.Text := ;
  try
    MyQuery1.Execute;
  except
   // some code to handle Lost Connection error
  end
- the handler of the Cancel button:

Code: Select all

  MyQuery2.Connection := MyConnection2;
  MyQuery2.SQL.Text := 'KILL ' + IntToStr(id);
  MyQuery2.Execute // this will cause the Lost Connection error for MyConnection1
MyConnection1 and MyConnection2 are connected to the same MySQL Server.
OK, this is for aborting a query attempt.

Now, how can I abort a connection attempt (that's what mostly interests me)? Is it possible to use your technique to achieve that effect?

Sorry for bothering you.

Thanks.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Wed 29 Nov 2006 10:16

There is no possibility to break connecting attempt. You can try to decrease value of the ConnectionTimeout property. After the time interval (in seconds) assigned to this property expires, connection attempt is considered unsuccessful.

euthymos
Posts: 5
Joined: Wed 22 Nov 2006 16:55

Post by euthymos » Thu 11 Jan 2007 23:08

I've seen MyDAC 5.00 beta has been released. Does it support this very feature?

Thank you

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 12 Jan 2007 10:03

Yes, in MyDAC 5.0 you can invoke the BreakExec method of TMyQuery or TMyCommand from a separate thread to interrupt a long-duration query.

Volniy_Strelok
Posts: 2
Joined: Wed 14 Nov 2007 11:31

can I use BreakExec in MyScritp?

Post by Volniy_Strelok » Wed 14 Nov 2007 11:37

can I use BreakExec in MyScritp?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 16 Nov 2007 09:52

No, currently you cannot use the BreakExec method in TMyScript. This method has a serious bug. We are going to handle this problem in the nearest time. Try to break the script execution in the TMyScript.BeforeExecute event.

Volniy_Strelok
Posts: 2
Joined: Wed 14 Nov 2007 11:31

Post by Volniy_Strelok » Mon 26 Nov 2007 09:00

Give an example please

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Mon 26 Nov 2007 16:43

Here is a small example:

Code: Select all

procedure TForm1.MyScript1BeforeExecute(Sender: TObject; var SQL: String;
  var Omit: Boolean);
begin
  if  then
    Abort;          // stop execution of the script 
  Omit := ;  //False prevents execution of the current statement
end;

Post Reply