Commit 94c2cab6 authored by Vincent Pelletier's avatar Vincent Pelletier

Implement MySQL connection pooling.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13633 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 675bfbb0
...@@ -96,6 +96,7 @@ from Globals import HTMLFile ...@@ -96,6 +96,7 @@ from Globals import HTMLFile
from ImageFile import ImageFile from ImageFile import ImageFile
from ExtensionClass import Base from ExtensionClass import Base
from DateTime import DateTime from DateTime import DateTime
from thread import allocate_lock
manage_addZMySQLConnectionForm=HTMLFile('connectionAdd',globals()) manage_addZMySQLConnectionForm=HTMLFile('connectionAdd',globals())
...@@ -106,6 +107,10 @@ def manage_addZMySQLConnection(self, id, title, ...@@ -106,6 +107,10 @@ def manage_addZMySQLConnection(self, id, title,
self._setObject(id, Connection(id, title, connection_string, check)) self._setObject(id, Connection(id, title, connection_string, check))
if REQUEST is not None: return self.manage_main(self,REQUEST) if REQUEST is not None: return self.manage_main(self,REQUEST)
# Connection Pool for connections to MySQL.
database_connection_pool_lock = allocate_lock()
database_connection_pool = {}
class Connection(DABase.Connection): class Connection(DABase.Connection):
" " " "
database_type=database_type database_type=database_type
...@@ -117,14 +122,26 @@ class Connection(DABase.Connection): ...@@ -117,14 +122,26 @@ class Connection(DABase.Connection):
def factory(self): return DB def factory(self): return DB
def connect(self,s): def connect(self, s):
try: self._v_database_connection.close() try:
except: pass database_connection_pool_lock.acquire()
self._v_connected='' self._v_connected = ''
DB=self.factory() pool_key = self.getPhysicalPath()
## No try. DO. connection = database_connection_pool.get(pool_key)
self._v_database_connection=DB(s) if connection is not None and connection.connection == s:
self._v_connected=DateTime() self._v_database_connection = connection
else:
if connection is not None:
connection.close()
DB = self.factory()
database_connection_pool[pool_key] = DB(s, self)
self._v_database_connection = database_connection_pool[pool_key]
# XXX If date is used as such, it can be wrong because an existing
# connection may be reused. But this is suposedly only used as a
# marker to know if connection was successfull.
self._v_connected = DateTime()
finally:
database_connection_pool_lock.release()
return self return self
def sql_quote__(self, v, escapes={}): def sql_quote__(self, v, escapes={}):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment