web.py釋放數據庫鏈接

參考:http://blog.sina.com.cn/s/blog_73951d180101eck0.htmlhtml

webpy簡單易用,並且很是輕量。尤爲其操做數據庫很是方便。python

import web

db = web.database(dbn=DBN,host=HOST,db=DATABASE,user=USER,pw=PASSWORD, charset=CHARSET)
data = db.select(['i_project'],
                what=' p_class ',
                where='p_id = $PlatformBatchID',
                limit='1',
                vars=locals()
                )


但卻不會主動釋放鏈接,隨着時間鏈接數會愈來愈多。mysql

監控mysql鏈接數web

 show full processlist;
 show status like '%conn%';


解決方法sql

1)安裝DBUtilsshell

1.0)安裝數據庫

pip install DBUtils

1.2)使用code

db = web.database(maxshared=5,mincached=1,maxcached=5,maxconnections=10,dbn=DBN,host=HOST,db=DATABASE,user=USER,pw=PASSWORD, charset=CHARSET)

 * mincached : 啓動時開啓的空鏈接數量(缺省值 0 意味着開始時不建立鏈接)orm

 * maxcached: 鏈接池使用的最多鏈接數量(缺省值 0 表明不限制鏈接池大小)htm

 * maxshared: 最大容許的共享鏈接數量(缺省值 0 表明全部鏈接都是專用的)若是達到了最大數量,被請求爲共享的鏈接將會被共享使用。

 * maxconnections: 最大容許鏈接數量(缺省值 0 表明不限制)


2)主動釋放鏈接數及cursor

finally:
        #close cursor
        self.db._db_cursor().close()
        #close connection
        self.db._db_cursor().connection.close()
相關文章
相關標籤/搜索