oracle 在 Python 中设置数据库连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2374079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set database connection timeout in Python
提问by oneself
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.
我正在创建一个需要访问数据库的 RESTful API。我正在使用 Restish、Oracle 和 SQLAlchemy。但是,我会尽量概括我的问题,而不考虑 Restish 或其他 Web API。
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.
这类似于 java.sql.Statement 的 setQueryTimeout(int seconds) 方法在 Java 中所做的。
Thanks
谢谢
回答by Dmitry Khrisanov
for the query, you can look on timer and conn.cancel() call.
对于查询,您可以查看 timer 和 conn.cancel() 调用。
something in those lines:
在这些行中的东西:
t = threading.Timer(timeout,conn.cancel)
t.start()
cursor = conn.cursor()
cursor.execute(query)
res = cursor.fetchall()
t.cancel()
回答by BetarU
In linux see /etc/oracle/sqlnet.ora,
在linux中查看/etc/oracle/sqlnet.ora,
sqlnet.outbound_connect_timeout= value
sqlnet.outbound_connect_timeout= value
also have options:
也有选择:
tcp.connect_timeout and sqlnet.expire_time, good luck!
tcp.connect_timeout 和 sqlnet.expire_time,祝你好运!
回答by Gary Myers
回答by Mark Harrison
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.
以下是如何使用操作系统超时来执行此操作。它是通用的,适用于 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!!