13

I'm creating a RESTful API which needs to access the database. I'm using Restish, Oracle, and SQLAlchemy. However, I'll try to frame my question as generically as possible, without taking Restish or other web APIs into account.

I would like to be able to set a timeout for a connection executing a query. This is to ensure that long running queries are abandoned, and the connection discarded (or recycled). This query timeout can be a global value, meaning, I don't need to change it per query or connection creation.

Given the following code:

import cx_Oracle
import sqlalchemy.pool as pool

conn_pool = pool.manage(cx_Oracle)
conn = conn_pool.connect("username/p4ss@dbname")
conn.ping()

try:
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM really_slow_query")
    print cursor.fetchone()
finally:
    cursor.close()

How can I modify the above code to set a query timeout on it? Will this timeout also apply to connection creation?

This is similar to what java.sql.Statement's setQueryTimeout(int seconds) method does in Java.

Thanks

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
oneself
  • 38,641
  • 34
  • 96
  • 120

5 Answers5

16

for the query, you can look on timer and conn.cancel() call.

something in those lines:

t = threading.Timer(timeout,conn.cancel)
t.start()
cursor = conn.cursor()
cursor.execute(query)
res =  cursor.fetchall()
t.cancel()
  • This works, but not if there is a network problem. In that case the cancel returns after 10 minutes with ORA-12152: TNS:unable to send break message. –  Mar 12 '19 at 08:36
5

In linux see /etc/oracle/sqlnet.ora,

sqlnet.outbound_connect_timeout= value

also have options:

tcp.connect_timeout and sqlnet.expire_time, good luck!

athspk
  • 6,722
  • 7
  • 37
  • 51
BetarU
  • 53
  • 1
  • 3
0

You could look at setting up PROFILEs in Oracle to terminate the queries after a certain number of logical_reads_per_call and/or cpu_per_call

Gary Myers
  • 34,963
  • 3
  • 49
  • 74
0

For Windows 11; The below line in sqlnet.ora gave me the said timeout while connecting.

tcp.connect_timeout=3

as indicated by betaru

moken
  • 3,227
  • 8
  • 13
  • 23
Rafeek
  • 41
  • 8
-3

Timing Out with the System Alarm

Here's how to use the operating system timout to do this. It's generic, and works for things other than Oracle.

import signal
class TimeoutExc(Exception):
    """this exception is raised when there's a timeout"""
    def __init__(self): Exception.__init__(self)
def alarmhandler(signame,frame):
    "sigalarm handler.  raises a Timeout exception"""
    raise TimeoutExc()

nsecs=5
signal.signal(signal.SIGALRM, alarmhandler)  # set the signal handler function
signal.alarm(nsecs)                          # in 5s, the process receives a SIGALRM
try:
    cx_Oracle.connect(blah blah)             # do your thing, connect, query, etc
    signal.alarm(0)                          # if successful, turn of alarm
except TimeoutExc:
    print "timed out!"                       # timed out!!
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • Of course Oracle (on the server) doesn't get interrupted -- you said you wanted to "abandon the long-running query", which means that you simply give up on waiting for the response. If you want to actually make Oracle stop running the query -- that's a whole other thing, and not so easy -- you would have to "kill the session" on the server... – Nick Perkins Dec 01 '11 at 15:21
  • 1
    What I meant to say is that the query continues to run uninterrupted on the client. – oneself Dec 11 '11 at 18:07
  • FWIW, I tried to use signal in the context of a Django app attempting to connect() to a database. signal throws an exception, complaining that it can only be used on the main thread & Django multithreads the request handling. – JL Peyret Apr 02 '15 at 04:44