如何实现python的mysql连接池并加入缓存过期
2个回答
2016-04-03
展开全部
import MySQLdb
import time
import string
import redis
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=='xxx':
pass
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
#mysql如下创建连接池:
connstring="xiaorui.cc#xiaoru.cc#xiaorui.cc#3306#dbname";
mysqlpool=PooledConnection(10,connstring,"mysql");
#获取连接:
mysqlpool.getConnection()
import time
import string
import redis
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=='xxx':
pass
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
#mysql如下创建连接池:
connstring="xiaorui.cc#xiaoru.cc#xiaorui.cc#3306#dbname";
mysqlpool=PooledConnection(10,connstring,"mysql");
#获取连接:
mysqlpool.getConnection()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询