标签:
想要模仿zabbix的oracle插件orabix来实现对db2的监控,但是Java能力有限,就用python来实现了。但是python常用的连接池PooledDB似乎并不支持db2,一直报这样的错误:"Database module is not thread-safe."所幸我只是用来做监控的,要求并不是很高,只要实现连接池的两个基本功能即可:
1.连接复用
2.连接检查,重连
1 #!/usr/local/bin/python 2 # -*- coding: utf-8 -*- 3 4 import threading,ibm_db,time,Queue 5 6 class db2pool(object): 7 def __init__(self,host,port,user,password,db,conn_num): 8 self.host=host 9 self.port=port 10 self.user=user 11 self.password=password 12 self.db=db 13 self.conn_num=conn_num 14 15 self.conn_queue=Queue.Queue(0) 16 17 for i in range (0,self.conn_num): 18 try: 19 conn=(ibm_db.connect("DATABASE="+self.db+";HOSTNAME="+self.host+";PORT="+self.port+";PROTOCOL=TCPIP;UID="+self.user+";PWD="+self.password+";", "", "")) 20 self.conn_queue.put(conn) 21 except Exception as e: 22 print e 23 24 def getconnect(self): 25 26 for i in range (0,5): 27 28 if self.conn_queue.qsize()>0: 29 conn=self.conn_queue.get() 30 31 try: 32 stmt=ibm_db.prepare(conn,"select 1 from sysibm.sysdummy1") 33 ibm_db.execute(stmt) 34 return conn 35 except Exception as e: 36 print e 37 try: 38 conn=(ibm_db.connect("DATABASE="+self.db+";HOSTNAME="+self.host+";PORT="+self.port+";PROTOCOL=TCPIP;UID="+self.user+";PWD="+self.password+";", "", "")) 39 self.conn_queue.put(conn) 40 except Exception as e: 41 print e 42 43 time.sleep(0.5) 44 45 self.logger.echo("warning","get connection error") 46 47 def getclose(self,conn): 48 self.conn_queue.put(conn) 49 50 #example 51 def work(pool,i): 52 # while True: 53 conn=pool.getconnect() 54 if conn: 55 try: 56 pass 57 stmt=ibm_db.prepare(conn,"select "+str(i)+" from sysibm.sysdummy1") 58 ibm_db.execute(stmt) 59 result=ibm_db.fetch_both(stmt) 60 while (result): 61 print "\nresult[0]="+str(result[0])+"++++"+str(conn) 62 result=ibm_db.fetch_both(stmt) 63 except Exception as e: 64 print e 65 finally: 66 pass 67 pool.getclose(conn) 68 time.sleep(5) 69 70 if __name__=="__main__": 71 pool=db2pool("172.16.2.9","60000","db2inst1","db2inst1","secs",5) 72 for i in range (0,50): 73 t=threading.Thread(target=work,args=(pool,i,)) 74 t.start()
因为我自己对python也不熟,这里有个地方我采用了妥协的办法:
我是在调用连接的时候去检查连接是否还存活着,而一般的情况下都是用一个后台的线程每隔一段时间去检查线程的。
我也尝试着这样去做了,但发现一个问题:获取连接队列conn=self.conn_queue.get()这一过程对各方都是平等的,谁抢到就是谁的。如果要做个检查连接的方法,则我希望它获得队列的优先级要高与正常调用的的方法,这样就不会出现连接队列一直被正常调用的方法霸占着,而检查连接的方法却因为抢不到连接而不工作。看来这部分要在实现了可控优先级的队列后才能实现了。
标签:
原文地址:http://www.cnblogs.com/paololiu/p/5070749.html