You want to emulate Zabbix's Oracle plug-in Orabix to monitor DB2, but Java has a limited ability to implement it with Python. However, Python's common connection pool POOLEDDB does not seem to support DB2, and has consistently reported such errors: "Database module is not thread-safe." Fortunately I was only used for monitoring, the requirements are not very high, as long as the implementation of the connection pool Two basic functions can be:
1. Connection multiplexing
2. Connection check, re-connect
1 #!/usr/local/bin/python2 #-*-coding:utf-8-*-3 4 ImportThreading,ibm_db,time,queue5 6 classDb2pool (object):7 def __init__(self,host,port,user,password,db,conn_num):8self.host=Host9self.port=PortTenSelf.user=User Oneself.password=Password Aself.db=DB -self.conn_num=Conn_num - theSelf.conn_queue=queue.queue (0) - - forIinchRange (0,self.conn_num): - Try: +Conn= (Ibm_db.connect ("database="+self.db+"; Hostname="+self.host+"; Port="+self.port+"; Protocol=tcpip; Uid="+self.user+"; Pwd="+self.password+";","","")) - Self.conn_queue.put (conn) + exceptException as E: A Printe at - defGetConnect (self): - - forIinchRange (0,5): - - ifSelf.conn_queue.qsize () >0: inconn=Self.conn_queue.get () - to Try: +Stmt=ibm_db.prepare (Conn,"Select 1 from sysibm.sysdummy1") - Ibm_db.execute (stmt) the returnConn * exceptException as E: $ PrintePanax Notoginseng Try: -Conn= (Ibm_db.connect ("database="+self.db+"; Hostname="+self.host+"; Port="+self.port+"; Protocol=tcpip; Uid="+self.user+"; Pwd="+self.password+";","","")) the Self.conn_queue.put (conn) + exceptException as E: A Printe the +Time.sleep (0.5) - $Self.logger.echo ("Warning","Get connection Error") $ - defGetclose (self,conn): - Self.conn_queue.put (conn) the - #ExampleWuyi defWork (pool,i): the #While True: -conn=Pool.getconnect () Wu ifConn: - Try: About Pass $Stmt=ibm_db.prepare (Conn,"Select"+str (i) +"From sysibm.sysdummy1") - Ibm_db.execute (stmt) -result=Ibm_db.fetch_both (stmt) - while(Result): A Print "\nresult[0]="+str (Result[0]) +"++++"+STR (conn) +result=Ibm_db.fetch_both (stmt) the exceptException as E: - Printe $ finally: the Pass the Pool.getclose (conn) theTime.sleep (5) the - if __name__=="__main__": inPool=db2pool ("172.16.2.9","60000","Db2inst1","Db2inst1","secs", 5) the forIinchRange (0,50): theT=threading. Thread (target=work,args=(Pool,i,)) AboutT.start ()
Because I'm not familiar with Python myself, here's a place where I used a compromise approach:
I'm going to check to see if the connection is still alive when I call the connection, and in general it's a background thread that checks for threads every once in a while.
I also tried to do it, but found a problem: Get the connection Queue Conn=self.conn_queue.get () This process is equal to all parties, and whoever grabs it is who it is. If I want to do a check of the connection, then I want it to get the queue priority is higher and normal call method, so that the connection queue is always called by the normal method of seizing, and the method of checking the connection is not able to get the connection and not work. It seems that this part will be implemented after the control-priority queue is implemented.
Custom implementation of DB2 connection pooling with Python