本文整理汇总了Python中DBUtils.PooledDB.PooledDB.connection方法的典型用法代码示例。如果您正苦于以下问题:Python PooledDB.connection方法的具体用法?Python PooledDB.connection怎么用?Python PooledDB.connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBUtils.PooledDB.PooledDB
的用法示例。
在下文中一共展示了PooledDB.connection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test09_EquallyShared
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test09_EquallyShared(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
shareable = threadsafety > 1
pool = PooledDB(dbapi, 5, 5, 5)
self.assertEqual(len(pool._idle_cache), 5)
for i in range(15):
db = pool.connection(False)
db.cursor().execute('select test')
db.close()
self.assertEqual(len(pool._idle_cache), 5)
for i in range(5):
con = pool._idle_cache[i]
self.assertEqual(con._usage, 3)
self.assertEqual(con._con.num_queries, 3)
cache = []
for i in range(35):
db = pool.connection()
db.cursor().execute('select test')
cache.append(db)
del db
self.assertEqual(len(pool._idle_cache), 0)
if shareable:
self.assertEqual(len(pool._shared_cache), 5)
for i in range(5):
con = pool._shared_cache[i]
self.assertEqual(con.shared, 7)
con = con.con
self.assertEqual(con._usage, 10)
self.assertEqual(con._con.num_queries, 10)
del cache
self.assertEqual(len(pool._idle_cache), 5)
if shareable:
self.assertEqual(len(pool._shared_cache), 0)
示例2: test11_Rollback
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test11_Rollback(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
pool = PooledDB(dbapi, 0, 1)
self.assertEqual(len(pool._idle_cache), 0)
db = pool.connection(False)
self.assertEqual(len(pool._idle_cache), 0)
self.assertEqual(db._con._con.open_cursors, 0)
cursor = db.cursor()
self.assertEqual(db._con._con.open_cursors, 1)
cursor.execute('set doit1')
db.commit()
cursor.execute('set dont1')
cursor.close()
self.assertEqual(db._con._con.open_cursors, 0)
del db
self.assertEqual(len(pool._idle_cache), 1)
db = pool.connection(False)
self.assertEqual(len(pool._idle_cache), 0)
self.assertEqual(db._con._con.open_cursors, 0)
cursor = db.cursor()
self.assertEqual(db._con._con.open_cursors, 1)
cursor.execute('set doit2')
cursor.close()
self.assertEqual(db._con._con.open_cursors, 0)
db.commit()
session = db._con._con.session
db.close()
self.assertEqual(session, [
'doit1', 'commit', 'dont1', 'rollback',
'doit2', 'commit', 'rollback'])
示例3: test14_SetSession
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test14_SetSession(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
setsession = ('set time zone', 'set datestyle')
pool = PooledDB(dbapi, 0, 0, 0, 1, False, None, setsession)
self.assertEqual(pool._setsession, setsession)
db = pool.connection(False)
self.assertEqual(db._setsession_sql, setsession)
self.assertEqual(db._con._con.session,
['time zone', 'datestyle'])
db.cursor().execute('select test')
db.cursor().execute('set test1')
self.assertEqual(db._usage, 2)
self.assertEqual(db._con._con.num_uses, 4)
self.assertEqual(db._con._con.num_queries, 1)
self.assertEqual(db._con._con.session,
['time zone', 'datestyle', 'test1'])
db.close()
db = pool.connection(False)
self.assertEqual(db._setsession_sql, setsession)
self.assertEqual(db._con._con.session,
['time zone', 'datestyle', 'test1', 'rollback'])
db._con._con.close()
db.cursor().execute('select test')
db.cursor().execute('set test2')
self.assertEqual(db._con._con.session,
['time zone', 'datestyle', 'test2'])
示例4: RDataBase
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class RDataBase(Singleton):
def __init__(self):
if hasattr(self, '_init'):
return
self._init = True
config = RConfig()
self._db_pool = PooledDB(MySQLdb,
user=config.db_user,
passwd=config.db_passwd,
host=config.db_host,
port=config.db_port,
db=config.db_db,
mincached=config.db_mincached,
maxcached=config.db_maxcached,
maxshared=config.db_maxshared,
maxconnections=config.db_maxconnections,
cursorclass=cursors.DictCursor
)
def execute(self, sql, param):
try:
db = self._db_pool.connection()
cursor = db.cursor()
result = cursor.execute(sql, param)
except Exception as e:
print "MySQL Error Execute [%s] %r" % (sql, param)
db.close()
raise RError(1)
db.commit()
db.close()
return result
def query(self, sql, param):
try:
db = self._db_pool.connection()
cursor = db.cursor()
result = cursor.execute(sql, param)
except Exception as e:
print "MySQL Error [%s] %r" % (sql, param)
db.close()
raise RError(1)
result = cursor.fetchall()
db.close()
return result
def begin(self):
try:
db = self._db_pool.connection()
except Exception as e:
print "MySQL Error when begin an execute."
db.close()
raise RError(1)
return RDataBaseConnection(db)
def commit(self, con):
return con.commit()
示例5: __init__
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class TRtgHandler:
def __init__(self,config,queue):
self.pool = PooledDB(creator=MySQLdb,mincached=10,host=config.MYSQL_SERVER,user=config.MYSQL_USER,passwd=config.MYSQL_PASSWORD,db=config.MYSQL_DATABASE)
self.queue = queue
def response(self,r_id):
conn = self.pool.connection()
cur = conn.cursor()
cur.execute('select d_id,response.user_id,replyDate,response.content,cat_id,category.thumb,conversation.title from response inner join (conversation inner join category using (cat_id)) using(d_id) \
where r_id=%s',(r_id))
res = cur.fetchone()
user = database.fetchUserNoCache(cur,res[1])
escaped = util.escape(res[3])
newContent = util.replaceMentions(cur,escaped)
shortContent = util.replaceMentions(cur,escaped,True)
cur.close()
conn.close()
payload = {'date':res[2].isoformat(),'content':newContent,'short':shortContent,'user':user,'r_id':r_id,'d_id':res[0]}
self.queue.put(event.Message('/conversation/%d' % (res[0]), 'response',payload))
happening_data = {'user':user,'date':res[2].isoformat(),'category_image':res[5],'category_id':res[4],'d_id':res[0],'title': res[6],'r_id':r_id,'content':newContent}
self.queue.put(event.Message('/happening','happening',{'type':'response','data':happening_data}))
def conversation(self,d_id):
conn = self.pool.connection()
cur = conn.cursor()
cur.execute('select user_id,postDate,content,category.thumb,cat_id,title from conversation inner join category using (cat_id) \
where d_id=%s',(d_id,))
convo = cur.fetchone()
user = database.fetchUserNoCache(cur,convo[0])
newContent = util.escape(convo[2])
payload = {'id':d_id,'date':convo[1].isoformat(),'title':convo[5],'user':user,'content':newContent,'short':util.replaceMentions(cur,newContent,True)}
cur.close()
conn.close()
self.queue.put(event.Message('/category/%d' % (convo[4]),'conversation',payload))
happening_data = {'user':user,'date':convo[1].isoformat(),'category_image':convo[3],'d_id':d_id,'title':convo[5],'content':newContent}
self.queue.put(event.Message('/happening','happening',{'type':'post','data':happening_data}))
def auth(self,auth):
self.queue.put(event.NewAuthKey(auth.user_id,auth.key))
def userModified(self,user_id):
conn = self.pool.connection()
cur = conn.cursor()
user = database.fetchUserNoCache(cur,user_id)
cur.close()
conn.close()
self.queue.put(event.Message('/user/%d' % user_id,'user',user))
示例6: RDataBase
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class RDataBase(Singleton):
def __init__(self, config):
self._db_pool = PooledDB(MySQLdb,
user=config.db_user,
passwd=config.db_passwd,
host=config.db_host,
port=config.db_port,
db=config.db_db,
mincached=config.db_mincached,
maxcached=config.db_maxcached,
maxshared=config.db_maxshared,
maxconnections=config.db_maxconnections,
cursorclass=cursors.DictCursor
)
def execute(self, sql, param):
try:
db = self._db_pool.connection()
cursor = db.cursor()
cursor.execute("INSERT into input_params VALUE (now(), %s, %s)", (str(sql), str(param)))
result = cursor.execute(sql, param)
except Exception as e:
print "MySQL Error Execute [%s] %r" % (sql, param)
db.close()
raise RError(1)
db.commit()
db.close()
return result
def query(self, sql, param):
try:
db = self._db_pool.connection()
cursor = db.cursor()
cursor.execute("INSERT into input_params VALUE (now(), %s, %s)", (str(sql), str(param)))
result = cursor.execute(sql, param)
except Exception as e:
print "MySQL Error [%s] %r" % (sql, param)
db.close()
raise RError(1)
result = cursor.fetchall()
db.close()
return result
def begin(self):
try:
db = self._db_pool.connection()
except Exception as e:
print "MySQL Error when begin an execute."
db.close()
raise RError(1)
return RDataBaseConnection(id)
def commit(self, con):
return con.commit()
示例7: __new__
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class DataAccess:
__singleInstance = None
pool = None
pool_size = 10
debug_level = 0
def __new__(clz):
if not DataAccess.__singleInstance:
DataAccess.__singleInstance = object.__new__(clz)
return DataAccess.__singleInstance
def __init__(self):
# mysql
self.pool = PooledDB(MySQLdb, self.pool_size, db=db_set.db_dbname
, user=db_set.db_user, passwd=db_set.db_pwd, host=db_set.db_ip, charset="utf8")
def InsertRow(self, insertStr):
conn = self.pool.connection()
try:
cursor = conn.cursor()
cursor.execute('SET NAMES utf8')
cursor.execute(insertStr)
conn.commit()
return cursor.lastrowid
except:
print("InsertRow: Unexpected error:"
, sys.exc_info(), sys.exc_traceback.tb_lineno)
return 0
finally:
if conn:
conn.close()
def SelectRow(self, selectStr, where = None):
conn = self.pool.connection()
try:
cursor = conn.cursor()
cursor.execute('SET NAMES utf8')
if where is not None:
cursor.execute(selectStr, where)
else:
cursor.execute(selectStr)
res = cursor.fetchall()
return res
except:
print("SelectRow: Unexpected error:"
, sys.exc_info(), sys.exc_traceback.tb_lineno)
finally:
if conn:
conn.close()
def debug(self, *print_me):
if self.debug_level > 0:
print print_me
示例8: test05_CloseAll
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test05_CloseAll(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
shareable = threadsafety > 1
pool = PooledDB(dbapi, 10)
self.assertEqual(len(pool._idle_cache), 10)
pool.close()
self.assertEqual(len(pool._idle_cache), 0)
pool = PooledDB(dbapi, 10)
closed = ['no']
def close(what=closed):
what[0] = 'yes'
pool._idle_cache[7]._con.close = close
self.assertEqual(closed, ['no'])
del pool
self.assertEqual(closed, ['yes'])
pool = PooledDB(dbapi, 10, 10, 5)
self.assertEqual(len(pool._idle_cache), 10)
if shareable:
self.assertEqual(len(pool._shared_cache), 0)
cache = []
for i in range(5):
cache.append(pool.connection())
self.assertEqual(len(pool._idle_cache), 5)
if shareable:
self.assertEqual(len(pool._shared_cache), 5)
else:
self.assertEqual(len(pool._idle_cache), 5)
pool.close()
self.assertEqual(len(pool._idle_cache), 0)
if shareable:
self.assertEqual(len(pool._shared_cache), 0)
pool = PooledDB(dbapi, 10, 10, 5)
closed = []
def close_idle(what=closed):
what.append('idle')
def close_shared(what=closed):
what.append('shared')
if shareable:
cache = []
for i in range(5):
cache.append(pool.connection())
pool._shared_cache[3].con.close = close_shared
else:
pool._idle_cache[7]._con.close = close_shared
pool._idle_cache[3]._con.close = close_idle
self.assertEqual(closed, [])
del pool
if shareable:
del cache
self.assertEqual(closed, ['idle', 'shared'])
示例9: test10_SortShared
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test10_SortShared(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
shareable = threadsafety > 1
pool = PooledDB(dbapi, 0, 0, 5)
cache = []
for i in range(35):
db = pool.connection()
db.cursor().execute('select test1')
db.cursor().execute('select test2')
db.cursor().callproc('test3')
cache.append(db)
del db
self.assertEqual(len(pool._idle_cache), 0)
if shareable:
self.assertEqual(len(pool._shared_cache), 5)
for i in range(5):
con = pool._shared_cache[i]
self.assertEqual(con.shared, 7)
con = con.con
self.assertEqual(con._usage, 21)
self.assertEqual(con._con.num_queries, 14)
cache[3] = cache[8] = cache[33] = None
cache[12] = cache[17] = cache[34] = None
self.assertEqual(len(pool._shared_cache), 5)
self.assertEqual(pool._shared_cache[0].shared, 7)
self.assertEqual(pool._shared_cache[1].shared, 7)
self.assertEqual(pool._shared_cache[2].shared, 5)
self.assertEqual(pool._shared_cache[3].shared, 4)
self.assertEqual(pool._shared_cache[4].shared, 6)
for db in cache:
if db:
db.cursor().callproc('test4')
for i in range(6):
db = pool.connection()
db.cursor().callproc('test4')
cache.append(db)
del db
for i in range(5):
con = pool._shared_cache[i]
self.assertEqual(con.shared, 7)
con = con.con
self.assertEqual(con._usage, 28)
self.assertEqual(con._con.num_queries, 14)
del cache
if shareable:
self.assertEqual(len(pool._idle_cache), 5)
self.assertEqual(len(pool._shared_cache), 0)
else:
self.assertEqual(len(pool._idle_cache), 35)
示例10: __init__
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class MysqlPool:
'''A class of connect pool to Mysql Database'''
def __init__(self, args):
# mysql connect params
args["charset"] = "utf8"
args["setsession"] =['SET AUTOCOMMIT = 1']
# mysql connect pool
self.pooldb = PooledDB(MySQLdb, **args)
def __del__(self):
if self.pooldb: self.pooldb.close()
def select(self, sql, args=None):
try:
conn= self.pooldb.connection()
cur = conn.cursor()
cur.execute(sql, args)
results = cur.fetchall()
cur.close()
conn.close()
return results
except Exception as e:
Common.log('# MyDatabase select exception: %s %s' % (e, sql))
Common.log(args)
return None
def execute(self, sql, args=None):
try:
conn= self.pooldb.connection()
cur = conn.cursor()
cur.execute(sql, args)
cur.close()
conn.close()
except Exception as e:
Common.log('# MyDatabase execute exception: %s %s' % (e, sql))
Common.log(args)
def executemany(self, sql, args_list=[]):
try:
conn= self.pooldb.connection()
cur = conn.cursor()
cur.executemany(sql, args_list)
cur.close()
conn.close()
except Exception as e:
Common.log('# MyDatabase executemany exception: %s %s' % (e, sql))
Common.log(args_list)
示例11: PooledConnectionPolicy
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class PooledConnectionPolicy(DatabaseConnectionPolicyIface):
"""This connection policy maintains a pool of connections that are doled out
as needed for each transaction. NOTE: Appropriate for multi-threaded
applications. NOTE: The connections are NOT shared concurrently between
threads.
"""
def __init__(self):
""" Consruct an instance. The instance's open() method must be
called to make it ready for acquireConnection() calls.
"""
self._logger = _getLogger(self.__class__)
self._logger.debug("Opening")
self._pool = PooledDB(**_getCommonSteadyDBArgsDict())
self._logger.info("Created %s", self.__class__.__name__)
return
def close(self):
""" Close the policy instance and its database connection pool. """
self._logger.info("Closing")
if self._pool is not None:
self._pool.close()
self._pool = None
else:
self._logger.warning("close() called, but connection policy was alredy closed")
return
def acquireConnection(self):
""" Get a connection from the pool.
Parameters:
----------------------------------------------------------------
retval: A ConnectionWrapper instance. NOTE: Caller
is responsible for calling the ConnectionWrapper
instance's release() method or use it in a context manager
expression (with ... as:) to release resources.
"""
self._logger.debug("Acquiring connection")
dbConn = self._pool.connection(shareable=False)
connWrap = ConnectionWrapper(
dbConn=dbConn, cursor=dbConn.cursor(), releaser=self._releaseConnection, logger=self._logger
)
return connWrap
def _releaseConnection(self, dbConn, cursor):
""" Release database connection and cursor; passed as a callback to
ConnectionWrapper
"""
self._logger.debug("Releasing connection")
# Close the cursor
cursor.close()
# ... then return db connection back to the pool
dbConn.close()
return
示例12: DbManager
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class DbManager(object):
def __init__(self):
connKwargs = {'host':'127.0.0.1', 'user':'root', 'port':3306, 'passwd':'root', 'db':'yii', 'charset':"utf8"}
args = (10, 10, 30, 100, True, 0, None)
self._pool = PooledDB(MySQLdb, *args, **connKwargs)
def getConn(self):
return self._pool.connection()
def executeAndGetId(self, sql, param=None):
""" 执行插入语句并获取自增id """
conn = self.getConn()
cursor = conn.cursor()
if param == None:
cursor.execute(sql)
else:
cursor.execute(sql, param)
id = cursor.lastrowid
cursor.close()
conn.close()
return id
def queryAll(self, sql):
""" 获取所有信息 """
conn = self.getConn()
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
rowcount = cursor.execute(sql)
if rowcount > 0:
res = cursor.fetchall()
else:
res = None
cursor.close()
conn.close()
return res
def execute(self,sql, param=None):
""" 执行sql语句 """
conn = self.getConn()
cursor = conn.cursor()
if param == None:
rowcount = cursor.execute(sql)
else:
rowcount = cursor.execute(sql, param)
cursor.close()
conn.close()
return rowcount
def queryOne(self,sql):
""" 获取一条信息 """
conn = self.getConn()
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
rowcount = cursor.execute(sql)
if rowcount > 0:
res = cursor.fetchone()
else:
res = None
cursor.close()
conn.close()
return res
示例13: generate_accuser_url
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def generate_accuser_url():
pool = PooledDB(MySQLdb, 1, host="localhost", user="root", passwd="123456", db="abnormal")
conn = pool.connection()
cur = conn.cursor()
# objFoleder = 'Report'
objFile = open("ParseResult")
index = 1
# for objFile in os.listdir(objFoleder):
count = 0
while True:
line = objFile.readline()
if not line:
break
# print replace_struct_time(line)
parDict = eval(replace_struct_time(line))
reportId = parDict["reportId"]
# soup = BeautifulSoup(os.path.join(objFoleder, objFile))
# countText = soup.find(class_='W_f12 W_textb').text
# accuserCount = int(patternNumber.search(countText).group())
accuserCount = min(parDict["accuserCount"], 20)
if accuserCount > 1:
for i in xrange(accuserCount - 1):
url = "http://service.account.weibo.com/aj/reportuser?rid=%s&page=%d&_t=0" % (reportId, i)
sql = 'insert into userlinks values (%d, "%s", 0)' % (index, url)
cur.execute(sql)
index += 1
print count
count += 1
conn.commit()
cur.close()
conn.close()
示例14: test13_MaxUsage
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
def test13_MaxUsage(self):
for threadsafety in (1, 2):
dbapi.threadsafety = threadsafety
for maxusage in (0, 3, 7):
pool = PooledDB(dbapi, 0, 0, 0, 1, False, maxusage)
self.assertEqual(pool._maxusage, maxusage)
self.assertEqual(len(pool._idle_cache), 0)
db = pool.connection(False)
self.assertEqual(db._con._maxusage, maxusage)
self.assertEqual(len(pool._idle_cache), 0)
self.assertEqual(db._con._con.open_cursors, 0)
self.assertEqual(db._usage, 0)
self.assertEqual(db._con._con.num_uses, 0)
self.assertEqual(db._con._con.num_queries, 0)
for i in range(20):
cursor=db.cursor()
self.assertEqual(db._con._con.open_cursors, 1)
cursor.execute('select test%i' % i)
r = cursor.fetchone()
self.assertEqual(r, 'test%i' % i)
cursor.close()
self.assertEqual(db._con._con.open_cursors, 0)
if maxusage:
j = i % maxusage + 1
else:
j = i + 1
self.assertEqual(db._usage, j)
self.assertEqual(db._con._con.num_uses, j)
self.assertEqual(db._con._con.num_queries, j)
db.cursor().callproc('test')
self.assertEqual(db._con._con.open_cursors, 0)
self.assertEqual(db._usage, j + 1)
self.assertEqual(db._con._con.num_uses, j + 1)
self.assertEqual(db._con._con.num_queries, j)
示例15: DBConnectionPool
# 需要导入模块: from DBUtils.PooledDB import PooledDB [as 别名]
# 或者: from DBUtils.PooledDB.PooledDB import connection [as 别名]
class DBConnectionPool(object):
__pool = None
def __enter__(self):
self.conn = self.getConn()
self.cursor = self.conn.cursor()
return self
def getConn(self):
if self.__pool is None:
self.__pool = PooledDB(creator=pymysql, mincached=Config.DB_MIN_CACHED, maxcached=Config.DB_MAX_CACHED,
maxshared=Config.DB_MAX_SHARED, maxconnections=Config.DB_MAX_CONNECYIONS,
blocking=Config.DB_BLOCKING, maxusage=Config.DB_MAX_USAGE,
setsession=Config.DB_SET_SESSION,
host=Config.DB_TEST_HOST, port=Config.DB_TEST_PORT,
user=Config.DB_TEST_USER, passwd=Config.DB_TEST_PASSWORD,
db=Config.DB_TEST_DBNAME, use_unicode=False, charset=Config.DB_CHARSET)
return self.__pool.connection()
"""
@summary: 释放连接池资源
"""
def __exit__(self, type, value, trace):
self.cursor.close()
self.conn.close()