【发布时间】:2020-02-25 09:07:20
【问题描述】:
我正在使用 cx_oracle 和 python 3.7 连接到 oracle 数据库并执行存储在 oracle 数据库中的存储过程。
现在我连接到数据库如下
dbconstr = "username/password@databaseip/sid"
db_connection = cx_Oracle.connect(dbconstr)
cursor = db_connection.cursor()
#calling sp here
cursor.close()
db_connection.close()
但在此代码中,cx_Oracle.connect(dbconstr) 的连接时间约为 250ms,整个代码将在 500ms 左右运行想要减少 250 毫秒的连接时间。
我在python中使用flask rest-api,此代码用于此,当整个响应时间为500ms时,250ms的连接太长了。
我还尝试通过为连接对象声明全局变量并仅创建和关闭游标来在应用程序的整个生命周期内维护连接,如下所示将在 250ms
dbconstr = "username/password@databaseip/sid"
db_connection = cx_Oracle.connect(dbconstr)
def api_response():
cursor = db_connection.cursor()
#calling sp here
cursor.close()
return result
通过这种方法可以减少响应时间,但即使没有人使用该应用程序,连接也会得到维持。闲置一段时间后,第一个请求的执行速度会在闲置一段时间后降低,以秒为单位,非常糟糕。
所以,我需要帮助创建具有良好响应时间的稳定代码。
【问题讨论】:
-
如果你ping你的服务器,需要多少钱?而且您最好确保连接始终处于正常状态,尤其是对于您的第二个版本。
-
conection.ping() 需要 20 毫秒的响应时间
标签: python python-3.x database oracle cx-oracle