1
2
3
4
5
6
7
8
9
10
11
12
13 from Exceptions import MySQLConnectionError
14
15 import time
16 import DateTime
17
18 from Products.ZenUtils import Time as timeutils
19 from Queue import Queue, Empty, Full
20
21 import logging
22 log = logging.getLogger("zen.DbConnectionPool")
23
24 POOL_SIZE = 5
25 KEEP_ALIVE = 28800
26
27
35
37
39 if not '_the_instance' in self.__dict__:
40 self._the_instance = object.__new__(self)
41 return self._the_instance
42
43 '''
44 instance = None
45 def __new__(cls, *args, **kargs):
46 if cls.instance is None:
47 cls.instance = object.__new__(cls, *args, **kargs)
48 log.debug('Returning single instance of DbConnectionPool')
49 return cls.instance
50 '''
51
54
57
58 - def get(self, host=None, port=None, username=None,
59 password=None, database=None, block=0):
60 try:
61 putstamp,obj = self.q.get(block)
62
63 if time.time() - putstamp >= KEEP_ALIVE:
64 log.debug('Retrieved a stale connection; Pool size: %s' % self.qsize())
65 obj.close()
66 return self._createConnection(host=host, port=port,
67 username=username,
68 password=password,
69 database=database)
70 else:
71 log.debug('Retrieved a connection; Pool size: %s' % self.qsize())
72 if hasattr(obj, 'ping'):
73
74 obj.ping()
75 return obj
76
77 except Empty:
78 return self._createConnection(host=host, port=port,
79 username=username,
80 password=password,
81 database=database)
82
83 - def put(self, obj, block=0):
84 try:
85 self.q.put((time.time(),obj), block)
86 log.debug('Returned a connection; Pool size: %s' % self.qsize())
87 except Full:
88 pass
89
90 - def _createConnection(self, host=None, port=None,
91 username=None, password=None, database=None):
92
93
94
95
96 import MySQLdb
97 import MySQLdb.converters
98 from MySQLdb.constants import FIELD_TYPE
99 log.debug('Creating a new connection; Pool size: %s' % self.qsize())
100 conn = None
101 mysqlconv = MySQLdb.converters.conversions.copy()
102 mysqlconv[FIELD_TYPE.DATETIME] = DateTime.DateTime
103 mysqlconv[FIELD_TYPE.TIMESTAMP] = timestamp_conv
104
105 mysqlconv[FIELD_TYPE.LONG] = int
106 if not host:
107 host, database = database, 'events'
108 if port:
109 port = int(port)
110 try:
111 conn = MySQLdb.connect(host=host, user=username,
112 port=port, passwd=password,
113 db=database, conv=mysqlconv, reconnect=1)
114 conn.autocommit(1)
115 return conn
116 except Exception, e:
117 raise MySQLConnectionError(str(e))
118