Thread: Can you set a timeout on a sql


Permlink Replies: 15 - Pages: 2 [ 1 2 | Next ] - Last Post: May 8, 2007 2:09 AM Last Post By: Satyaki_De
DGJ

Posts: 64
Registered: 01/10/01
Can you set a timeout on a sql
Posted: Jun 30, 2006 6:09 PM
Click to report abuse...   Click to reply to this thread Reply
Hello,

I have a cursor in my plsql program - the sql query hangs due to a problem with the target database (this is not the error). In my program I have a sequence of procedure calls and this is one of hte procedure. Since the sql hangs - the whole program hangs.

What I would like to do is to attempt to run the sql for a certain amount of time - if it does not return, then exit out of the cursor and continue with the rest of the program. Can this be done ?

Thanks
Dipti
Kamal Kishore

Posts: 7,355
Registered: 09/19/99
Re: Can you set a timeout on a sql
Posted: Jun 30, 2006 7:38 PM   in response to: DGJ in response to: DGJ
Click to report abuse...   Click to reply to this thread Reply
you can put resource limit on the user, but then it will terminate the call:
SQL> CREATE PROFILE LIMIT_USER_QUERY_TIME LIMIT CPU_PER_CALL 1000 ;

Profile created.

SQL> alter user kkishore profile LIMIT_USER_QUERY_TIME ;

User altered.

SQL> connect kkishore
Enter password:
Connected.
SQL> exec loop null ; end loop ;
BEGIN loop null ; end loop ; END;

*
ERROR at line 1:
ORA-02393: exceeded call limit on CPU usage

SQL>

DGJ

Posts: 64
Registered: 01/10/01
Re: Can you set a timeout on a sql
Posted: Jun 30, 2006 11:21 PM   in response to: Kamal Kishore in response to: Kamal Kishore
Click to report abuse...   Click to reply to this thread Reply
But this would be for the entire db user, right ? Can this be done in some fashion for the single query ?
Jameel

Posts: 1,069
Registered: 03/03/06
Re: Can you set a timeout on a sql
Posted: Jun 30, 2006 11:41 PM   in response to: DGJ in response to: DGJ
Click to report abuse...   Click to reply to this thread Reply
I made a simple try, whether it works for you?
Based on passing number of seconds in procedure...
 
SQL> create or replace function timeout
  2  return number is
  3  begin
  4   return to_number(to_char(sysdate,'SSSSS'));
  5  end timeout;
  6  / 
 
Function created.
 
SQL> create or replace procedure tcal( t in number ) as
  2  a  number:=1;
  3  st number:=to_number(to_char(sysdate,'SSSSS'));
  4  x varchar2(12);
  5  y varchar2(12);
  6  begin
  7  while(a<99999999) loop
  8  dbms_output.enable(100000); ---> Just to make loop busy...
  9  a:=a+1;
 10  exit when (timeout - st)>=t;
 11  end loop;
 12  x:=to_char(trunc(sysdate)+st/(24*60*60),'HH:MI:SS AM');
 13  dbms_output.put_line('       Started: '||x);
 14  y:=to_char(trunc(sysdate)+timeout/(24*60*60),'HH:MI:SS AM');
 15  dbms_output.put_line('       Ended:   '||y);
 16  dbms_output.put_line(timeout-st||' seconds reached...');
 17  end ;
 18  / 
 
Procedure created.
 
SQL> execute tcal(5);
Started: 12:19:24 PM
Ended:   12:19:29 PM
5 seconds reached...
 
PL/SQL procedure successfully completed.
 
SQL> 


Message was edited by:
Jameel
N Gasparotto

Posts: 19,247
Registered: 08/22/02
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 2:36 AM   in response to: Jameel in response to: Jameel
Click to report abuse...   Click to reply to this thread Reply
Jameel,

I don't understand how your example can help to stop a hang query...
Where profil, as Kamal suggested, can stop query through profil for a given user with some criteria : http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6010.htm#SQLRF01310

Nicolas.
Billy Verreynne

Posts: 9,122
Registered: 05/27/99
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 4:54 AM   in response to: DGJ in response to: DGJ
Click to report abuse...   Click to reply to this thread Reply
Ignoring resource profiles for the moment and focusing specifically on the traditional way this will be done in a database client process.

Not in PL/SQL the way you are thinking of. Yes, it is possible from a client that can do multithreading and deals directly with the (complex) OCI.

In that case you would spawn a thread to run the SQL and from the main thread wait for it to complete. When it does not do it in time, you use an OCI call to "interrupt" the Oracle process servicing that query and tell it to abort it. SQL*Plus for example use this technique when you press CTRL-BREAK when running a query.

However, the biggest advantage of PL/SQL is not having to deal with the OCI. So this OCI call cannot be made. Nor does PL/SQL support POSIX-like threads. So there's the problem.

There are ways to work around this. You can use the Unix-like fork() model in PL/SQL, spawning "child" processes via DBMS_JOB. IPC (Inter Process Communication) between parent and child processes can be done using DBMS_LOCK, DBMS_PIPE, AQ or even a plain vanilla Oracle table to serve as the IPC "shared memory segment".

The parent process can kill a child using the 'ALTER SYSTEM' command - not a nice way to interrupt the process, but with PL/SQL there are no interrupts we can code. So The Big Hammer it is.

So what you want to do in PL/SQL is not impossible - but needs some careful thought and code to implement a sensible fashion that works effectively and produces the desired results.
Jameel

Posts: 1,069
Registered: 03/03/06
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 5:11 AM   in response to: N Gasparotto in response to: N Gasparotto
Click to report abuse...   Click to reply to this thread Reply

Nicolas,
 
I think poster wanted to break/interrupt the procedure after a given time limit
if it does not complete or hanged within certain time...
 
Not sure what poster needs...
 
Kamal Kishore

Posts: 7,355
Registered: 09/19/99
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 5:33 AM   in response to: Jameel in response to: Jameel
Click to report abuse...   Click to reply to this thread Reply
I think poster wanted to break/interrupt the procedure after a given time limit

Yes, but if your while loop was running a SQL statement that took enormous amount of time and you wanted to break ...........

How would your code accomplish that?

Jameel

Posts: 1,069
Registered: 03/03/06
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 6:15 AM   in response to: Kamal Kishore in response to: Kamal Kishore
Click to report abuse...   Click to reply to this thread Reply
Kamal,
 
Yes, but it is a try to break a procedure after certain 
time limit if procedure is not completed before it...
 
CPU_PER_CALL profile limit works for that particular user
where the profile is set.
 
What about a particular procedure to break for?
 
Faust - old acc...

Posts: 524
Registered: 01/14/01
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 6:26 AM   in response to: Jameel in response to: Jameel
Click to report abuse...   Click to reply to this thread Reply
Yes, but it is a try to break a procedure after
certain
time limit if procedure is not completed before
it...

Jameel,

your example is flawed!

Can you image one SQL-statement that stops "looping" your loop because SQL-statement took enormous amount of time?

So your counter can't count => Line 10

exit when (timeout - st)>=t;

will be never reached...

Cheers!
Faust - old acc...

Posts: 524
Registered: 01/14/01
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 6:39 AM   in response to: Billy  Verreynne in response to: Billy Verreynne
Click to report abuse...   Click to reply to this thread Reply
There are ways to work around this. You can use the
Unix-like fork() model in PL/SQL, spawning "child"
processes via DBMS_JOB. IPC (Inter Process
Communication) between parent and child processes can
be done using DBMS_LOCK, DBMS_PIPE, AQ or even a
plain vanilla Oracle table to serve as the IPC
"shared memory segment".

Hi Billy!

Can you please elaborate previous thoughts little bit more?

Some code example?

Thanks!
Billy Verreynne

Posts: 9,122
Registered: 05/27/99
Re: Can you set a timeout on a sql
Posted: Jul 1, 2006 12:53 PM   in response to: Faust - old acc... in response to: Faust - old acc...
Click to report abuse...   Click to reply to this thread Reply
Can you please elaborate previous thoughts little bit more?

Well, instead of the fork() command you would use on Unix, you use the DBMS_JOB.Submit command.

The "child" process (DBMS_JOB session running as Oracle process Jnnnn) does not inherit the data segment of the parent - but this should not be a problem. You simply directly call the client procedure you want to execute via DBMS_JOB.

If you want to control the number of client jobs that can execute, you can implement a user defined resource via DBMS_LOCK and the number of locks/latches that may exist on it. Each DBMS_JOB submission locks it - and unlocks it when terminating.

If you want some form of IPC between parent and child processes, Oracle provides a range of IPC-like features. There is DBMS_PIPE. There is advanced queues. Even a normal table can suffice for the clients to insert "messages" into for the parent to process.

The basic architecture of multithreading is thus the same. Just the implementation differs slightly.

Oracle is a lot like an operating system. It has a lot of the common features that you'll find in operating systems. Many of these features are published as APIs via Oracle supplied PL/SQL packages (like DBMS_PIPE, DBMS_SCHEDULE, UTL_TCP, etc) - in other words you have:
- a TCP client socet API
- a job queue API
- a web browser
- an e-mailer
- pipes (between database sessions)
- message queues
- a "ftp" like API (between databases)
- a file system I/O API
- an integrated HTTP server daemon
etc.

Even better, PL/SQL and Oracle itself (at SQL and db core level) not only supports object classes, but has the ability to persist these as columns in tables.

I do 99% of all my back-end code inside Oracle. The few times that I have to do stuff outside Oracle is when dealing with pure o/s stuff - which I do using shell scripts.

Oracle is a very capable application platform. As Oracle Application Express clearly shows.
DGJ

Posts: 64
Registered: 01/10/01
Re: Can you set a timeout on a sql
Posted: Jul 7, 2006 2:19 PM   in response to: Jameel in response to: Jameel
Click to report abuse...   Click to reply to this thread Reply
This my scenario:

package test_package
......<otherProceduresOrFunctions>......

procedure problemProcedure is
begin

.....<code>....

for( select <someColums>
from table1, view2, mv3
where ..<constraints>....
) loop
...<codeToProcessTheCursorData>....
end loop;

....<remainingCode>.....
end;

procedure main is
begin
.....<otherCalls>.....
problemProcedure;
nextProcedure;
......<otherCalls>.....
end main;

end test_package;


The cursor hangs:

select <someColums>
from table1, view2, mv3
where ..<constraints>....

I know it is hanging due to the refresh issues in materialized views.

Ideally what I want to do is - if the query does not return in a certain timeframe, then exit the procedure and continue with the flow of the "main" procedure.

Thanks

prashant_cd

Posts: 6
Registered: 05/03/07
Re: Can you set a timeout on a sql
Posted: May 7, 2007 10:08 PM   in response to: DGJ in response to: DGJ
Click to report abuse...   Click to reply to this thread Reply
Hi,

Did you find any feasible solution for this issue? If so, can you please share it with all. We are also looking for solution for the same issue.

thanks,
Prashant
Satyaki_De

Posts: 6,760
Registered: 12/20/06
Re: Can you set a timeout on a sql
Posted: May 7, 2007 10:17 PM   in response to: Billy  Verreynne in response to: Billy Verreynne
Click to report abuse...   Click to reply to this thread Reply
Billy, how oracle handles Zombie process that happens in UNIX? Or that won't taken place in Oracle? Or oracle solves it in it's own way?

Regards.

Satyaki De.
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums