一个简单的python连接池,以及DBUtil.PooledDB的使用

http://www.dbafree.net/?p=1125

 

的一个很简单的连接池,基于线程安全的queue来实现,分享下:
#!/home/oracle/dbapython/bin/python
# -*- coding: utf-8 -*-
#Author: & dbafree
import cx_Oracle
import MySQLdb
import time
import string
class PooledConnection:
def __init__(self, maxconnections, connstr,dbtype):
from Queue import Queue
self._pool = Queue(maxconnections) # create the queue
self.connstr = connstr
self.dbtype=dbtype
self.maxconnections=maxconnections
try :
for i in range(maxconnections):
self.fillConnection(self.CreateConnection(connstr,dbtype))
except Exception,e:
raise e
def fillConnection(self,conn):
try :
self._pool.put(conn)
except Exception,e:
raise "fillConnection error:" +str(e)
def returnConnection(self, conn):
try :
self._pool.put(conn)
except Exception,e:
raise "returnConnection error:" +str(e)
def getConnection(self):
try :
return self._pool.get()
except Exception,e:
raise "getConnection error:" +str(e)
def ColseConnection(self,conn):
try :
self._pool.get().close()
self.fillConnection(self.CreateConnection(connstr,dbtype))
except Exception,e:
raise "CloseConnection error:" +str(e)
def CreateConnection(self,connstr,dbtype):
if dbtype== 'oracle' :
try :
db_conn = connstr.split( "#" );
connection=cx_Oracle.connect(db_conn[0],db_conn[1],db_conn[2],threaded=True)
connection.clientinfo = 'datasync connection pool from datasync.py'
connection.ping()
return connection
except Exception,e:
raise 'conn targetdb datasource Excepts,%s!!!(%s).' %(db_conn[2],str(e))
return None
elif dbtype== 'mysql' :
try :
db_conn = connstr.split( "#" );
#conndb=MySQLdb.connect(db=conf.mydb,host=conf.dbip,user=conf.myuser,passwd=conf.mypasswd);
conndb=MySQLdb.connect(user=db_conn[0],passwd=db_conn[1],host=db_conn[2],port=string.atoi(db_conn[3]),db=db_conn[4]);
conndb.clientinfo = 'datasync connection pool from datasync.py' ;
conndb.ping();
except Exception, e:
raise 'conn targetdb datasource Excepts,%s!!!(%s).' %(db_conn[2],str(e))
return None
#oracle如下创建连接池:
connstring= "alidba#alidba#test.db.alibaba.com:1525/testdb"
oraclepool=PooledConnection(10,connstring, "oracle" );
#获取连接:
oraclepool.getConnection()
#mysql如下创建连接池:
connstring= "alibaba#alibaba#test.db.alibaba.com#3306#database" ;
mysqlpool=PooledConnection(10,connstring, "mysql" );
#获取连接:
mysqlpool.getConnection()
time.sleep(10)
 
 
import MySQLdb
import time
import string
from DBUtils.PooledDB import PooledDB
pool = PooledDB(MySQLdb, user = "root", passwd = "root", host = "x.x.x.x
",port=3306,db="cloudflash",mincached=20,maxcached=20,maxshared=20,maxconnections=20)
#for i in range(1000):
time.sleep(1)
conn = pool.connection()
cursor = conn.cursor()
result = cursor.execute("""select count(*) from metadata""")
result = cursor.fetchall();
print  "result",result#cursor.description
conn.close();
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值