Solved

Interrupting the Query?

Posted on 2000-04-26
14
349 Views
Last Modified: 2010-04-04
Is it possible to interrupt the Query?
[while the query is running, I have to stop it.sample code desirable]Experts please...
0
Comment
Question by:veeramani
  • 5
  • 3
  • 3
  • +3
14 Comments
 
LVL 3

Expert Comment

by:Stefaan
ID: 2754299
Hi,

Could you provide us with some more information as to what DB you are working on ?

Best regards,

Stefaan

0
 
LVL 3

Expert Comment

by:shenqw
ID: 2754340
If you use Oralce:

select sid,serial#,status from v$session;
alter system kill session 'sid,serial';

you use first sql,you can get the session's sid ,serial,status. The second sql,you can kill the session .release all resources that the session uses.the 'sid,serial' are replaced by you get from the first sql result.

Good Luck. shenqw.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2754367
in v$session,the fields:USERNAME,SCHEMANAME,OSUSER,MACHINE,TERMINAL,PROGRAM can help you decide which session you want you kill.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2754583
If I'm not totally wrong then veeramani wants to stop an already (and long) running query using code not to kill sessions and such...

If I'm right then I have bad news. No, it is usually not possible a running query to stop. On some RDBMS like Interbase you can set a query timeout value to cause the server to abort the query if it runs longer than the timeout value but this is not supported on all DBMS. You can kill the thread which started the query but this is probably only to be used as last ressort...

Ciao, Mike
0
 
LVL 2

Expert Comment

by:florisb
ID: 2754696
Isn't possible to give the database some kind of interface to call a function that updates some kind of process-indicator (0..100%)?

F.
0
 
LVL 1

Author Comment

by:veeramani
ID: 2754758
I appreciate Lischke comment and that's what i am expecting for ,but how to kill the thread which started the query? If i kill the thread, does it disconnect the database?. I am using AS/400 as my backend, IDCO400 as my driver.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2754784
I said killing the thread is only as last ressort because it will only allow your application to continue. The query (which runs on the server) is still executed until the server realizes that it lost the conncetion to the client.

The perfect solution would be to notify the server to abort the query but to my knowledge this is not supported (although it wouldn't be that hard because all DBMSs are multi threaded).

On the client side you can use TerminateThread to kill a blocking thread regardless of its state. This requires of course that you have executed your query in a separate thread (see TThread, use TThread.Handle as handle for TerminateThread).

Ciao, Mike
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:shenqw
ID: 2754785
I think ,If you kill the session ,all resource of the session also should be released,include the running query.
0
 

Expert Comment

by:Volk
ID: 2754817
Isn't it possible to communicate with a running query (depending on the DBMS) through a kind of global variable? Just let the query/procedure check for some kind of stop condition at regular intervals.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2754837
This is on the server side, how do you want to check variables within a query?

Ciao, Mike
0
 
LVL 1

Author Comment

by:veeramani
ID: 2754874

How to create a thread and how to assign the particular query to that thread and how to terminate the particular thread?
0
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 2754926
That's quite easy. Say you have two buttons. One creates the query and the other one kills it:

type
  TQueryThread = class(TThread)
  private
    FQueryString: String;
  public
    procedure Execute;
  end;

  TMainForm = class(TForm)
    procedure OnQueryTermination(Sender: TObject);
  private
    :
    :
 
  end;

var
  QueryThread: TQueryThread;

procedure TMainForm.Button1Click(Sender: TObject);

begin
  QueryThread := TQueryThread.Create(True);
  with QueryThread do
  begin
    FreeOnTerminate := True;
    OnTerminate := OnQueryTermination;
    FQueryString := MyQueryString;
    Resume;
  end;
end;

procedure TMainForm.Button1Click(Sender: TObject);

var
  ExitCode: Cardinal;

begin
  if Assigned(QueryThread) then TerminateThread(QueryThread.Handle, ExitCode);
end;

procedure OnQueryTermination(Sender: TObject);

begin
  QueryThread := nil; // thread memory will be freed automatically (FreeOnTerminat = True)
end;

procedure TQueryThread.Execute;

begin
  // pseudo code
  CreateQueryFromString;
  Query.ExecSQL; or Query.Open;
  // nothing more to consider here
end;

Ciao, Mike
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2754951
There are some typos in the text, sorry, but I think you will easily correct them :-)=

For better management you can do a limited WaitForSingleObject before killing the thread:

procedure TMainForm.Button2Click(Sender: TObject);

var
  ExitCode: Cardinal;

begin
  if Assigned(QueryThread) then
  begin
    QueryThread.Terminate;
    // don't call WaitFor here as this waits unlimited time
    // wait 500ms...
    if WaitForSingleObject(QueryThread.Handle, 500) = WAIT_TIMEOUT then
      TerminateThread(QueryThread.Handle, ExitCode);
  end;
end;

Ciao, Mike
0
 
LVL 1

Author Comment

by:veeramani
ID: 2754997

Very good, thank you very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now