-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_pool.py
More file actions
executable file
·74 lines (64 loc) · 2.11 KB
/
Copy pathdb_pool.py
File metadata and controls
executable file
·74 lines (64 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from DBUtils.PooledDB import PooledDB
import MySQLdb
from CustomSetting import CustomSetting
class db(object):
pool = None
conn = None
def __new__(cls, *args, **kwargs):
return super(db, cls).__new__(cls, *args, **kwargs)
def __init__(self):
if db.pool is None:
db.pool = PooledDB(MySQLdb, 10,host = CustomSetting.HOST, user = CustomSetting.USR, passwd = CustomSetting.PWD, db = CustomSetting.DB,charset='utf8')
self.connect()
def connect(self):
self.conn = db.pool.connection()
def get_cursor(self, how=0):
try:
self.conn.ping()
except:
self.connect()
return self.conn.cursor()
def get_row(self, *args, **kwargs):
how = 1 if ('how' in kwargs and kwargs['how'] == 1) else 0
cursor = self.get_cursor(how)
try:
cursor.execute(*args)
except Exception,e:
if e[0] == 2013 or e[0] == 2006:
return self.get_row(*args,**kwargs)
else:
raise e
row = cursor.fetchone()
cursor.close()
return row
def get_rows(self, *args, **kwargs):
how = 1 if ('how' in kwargs and kwargs['how'] == 1) else 0
cursor = self.get_cursor(how)
try:
cursor.execute(*args)
except Exception,e:
if e[0] == 2013 or e[0] == 2006:
return self.get_rows(*args,**kwargs)
else:
raise e
rows = cursor.fetchall()
cursor.close()
return rows
def execute(self, *args):
cursor = self.get_cursor()
try:
ret = cursor.execute(*args)
except Exception,e:
if e[0] == 2013 or e[0] == 2006:
return self.execute(*args)
else:
raise e
cursor.close()
return ret
def commit(self):
self.conn.commit()
if __name__ == '__main__':
d = db()
sql = "SELECT * FROM users WHERE 1 LIMIT 1"
row = d.get_row(sql)
print repr(row)