如何在Flask gracey中使用cx_Oracle会话池? [英] How to use cx_Oracle session pool with Flask gracefuly?
问题描述
我是Python和Flask的新手,我使用Oracle,在学习Flask教程时,我按如下编写代码,但它的气味真的很差,请帮助我解决这些问题,非常感谢!
I'm a newbie to Python and Flask, and I use Oracle, when learning Flask tutorial, I code as follow, but it smells really bad, please help me with these questions, thanks a lot!
1)我需要释放连接以进行明确轮询吗?
1) need I release connection to poll explicitly?
2)我如何才能优雅地实现民意调查的获得和释放?
2) how can I implement poll acquire and release gracefully?
def get_dbpool():
if not hasattr(g, 'db_pool'):
g.dbPool = connect_db()
return g.dbPool
@app.teardown_appcontext
def close_db(error):
if hasattr(g, 'db_pool'):
g.dbPool.close()
@app.route('/')
def hello_world():
db = get_dbpool().acquire()
cursor=db.cursor()
sql=''
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()
get_dbpool().release(db)
return json.jsonify(combines=rows)
推荐答案
除非您打算将处理时间保持一段时间并且不再需要连接,否则无需显式释放与池的连接.当连接超出范围(功能结束)时,cx_Oracle会自动将连接释放回池中,当然,前提是您尚未实现对连接的循环引用!在这种情况下,您必须等到垃圾回收执行为止.希望能回答您的问题!
There is no need to release the connection to the pool explicitly unless you intend to keep processing for some time and don't need the connection any longer. cx_Oracle automatically releases the connection back to the pool when the connection goes out of scope (function ends), provided that you haven't implemented a circular reference to the connection, of course! In that case you would have to wait until garbage collection executes. Hopefully that answers your questions!
这篇关于如何在Flask gracey中使用cx_Oracle会话池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!