前几天调程序时,程序出错.手工停止后,表被锁了. 我写了一个脚本, 查看数据库多久之后将挂掉的 session 收集掉
#!/usr/bin/env python
#coding=GBK
#!/usr/bin/env python
#11:22
import time
import cx_Oracle
class BindVarTypeError(Exception):pass
db_tns = 'username/password@asdf'
def get_sysdate():
return time.strftime("%Y%m%d%H%M%S", time.localtime())
def DB_Query(func):
def result(*args, **dic):
orcl = cx_Oracle.connect(db_tns)
curs = orcl.cursor()
try:
sql_str = func(curs, **dic)
tuple_ans = curs.fetchall()
finally:
curs.close()
orcl.close()
return tuple_ans
return result
@DB_Query
def _qry_sql_(curs=None, sqlstr ="", bindVars=None):
if isinstance(bindVars, dict):
curs.execute(sqlstr, bindVars)
elif isinstance(bindVars, list):
curs.executemany(sqlstr, bindVars)
elif bindVars is None:
curs.execute(sqlstr)
else:
raise BindVarTypeError
def qry_sql(sqlstr, bindVars=None):
return _qry_sql_(sqlstr=sqlstr,bindVars=bindVars)
def cost_time(func):
from time import time
def result(*args,**dic):
beign=time()
ans = func(*args,**dic)
print "cost time : ",time()-beign
return ans
return result
@cost_time
def test():
sql="select count(*) from v$session a where a.sid = '100'"
while True:
iCount =qry_sql(sql)[0][0]
if iCount <=0 :
break
else:
print get_sysdate(), "还在"
time.sleep(5)
else:
print get_sysdate(),"结束了."
if __name__ == "__main__":
test()
|
阅读(1680) | 评论(2) | 转发(0) |