Skip to content Skip to sidebar Skip to footer

Set Database Connection Timeout In Python

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, witho

Solution 1:

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()

Solution 2:

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

sqlnet.outbound_connect_timeout= value

also have options:

tcp.connect_timeout and sqlnet.expire_time, good luck!

Solution 3:

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

Solution 4:

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
classTimeoutExc(Exception):
    """this exception is raised when there's a timeout"""def__init__(self): Exception.__init__(self)
defalarmhandler(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 SIGALRMtry:
    cx_Oracle.connect(blah blah)             # do your thing, connect, query, etc
    signal.alarm(0)                          # if successful, turn of alarmexcept TimeoutExc:
    print"timed out!"# timed out!!

Post a Comment for "Set Database Connection Timeout In Python"