How to use cx_Oracle session pool with Flask gracefuly?

我是研究僧i 提交于 2021-01-28 11:33:09

问题


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) need I release connection to poll explicitly?

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)

回答1:


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!



来源:https://stackoverflow.com/questions/48517547/how-to-use-cx-oracle-session-pool-with-flask-gracefuly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!