Commit 0c736d01 authored by Vincent Pelletier's avatar Vincent Pelletier

Add transaction flag pooling.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@13670 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent bbe294b1
...@@ -156,8 +156,6 @@ def int_or_long(s): ...@@ -156,8 +156,6 @@ def int_or_long(s):
try: return int(s) try: return int(s)
except: return long(s) except: return long(s)
FINISH_OR_ABORT_CALLED_ID = '_finish_or_abort_called'
class DB(TM): class DB(TM):
Database_Connection=_mysql.connect Database_Connection=_mysql.connect
...@@ -188,6 +186,7 @@ class DB(TM): ...@@ -188,6 +186,7 @@ class DB(TM):
self.connection=connection self.connection=connection
self.kwargs = self._parse_connection_string(connection) self.kwargs = self._parse_connection_string(connection)
self.db = {} self.db = {}
self._finished_or_aborted = {}
db = self.getConnection() db = self.getConnection()
transactional = db.server_capabilities & CLIENT.TRANSACTIONS transactional = db.server_capabilities & CLIENT.TRANSACTIONS
if self._try_transactions == '-': if self._try_transactions == '-':
...@@ -198,6 +197,19 @@ class DB(TM): ...@@ -198,6 +197,19 @@ class DB(TM):
if self._mysql_lock: if self._mysql_lock:
self._use_TM = 1 self._use_TM = 1
def __del__(self):
self.cleanupConnections()
def getFinishedOrAborted(self):
return self._finished_or_aborted[get_ident()]
def setFinishedOrAborted(self, value):
self._finished_or_aborted[get_ident()] = value
def cleanupConnections(self):
for db in self.db:
db.close()
def forceReconnection(self): def forceReconnection(self):
db = apply(self.Database_Connection, (), self.kwargs) db = apply(self.Database_Connection, (), self.kwargs)
self.db[get_ident()] = db self.db[get_ident()] = db
...@@ -207,6 +219,8 @@ class DB(TM): ...@@ -207,6 +219,8 @@ class DB(TM):
ident = get_ident() ident = get_ident()
db = self.db.get(ident) db = self.db.get(ident)
if db is None: if db is None:
if len(self.db) != 0:
self.cleanupConnections()
db = self.forceReconnection() db = self.forceReconnection()
return db return db
...@@ -372,40 +386,36 @@ class DB(TM): ...@@ -372,40 +386,36 @@ class DB(TM):
LOG('ZMySQLDA', ERROR, "exception during _begin", LOG('ZMySQLDA', ERROR, "exception during _begin",
error=sys.exc_info()) error=sys.exc_info())
raise raise
setattr(self, FINISH_OR_ABORT_CALLED_ID, False) self.setFinishedOrAborted(False)
def _finish(self, *ignored): def _finish(self, *ignored):
if getattr(self, FINISH_OR_ABORT_CALLED_ID, False): if self.getFinishedOrAborted():
return
try:
try:
db = self.getConnection()
if self._mysql_lock:
db.query("SELECT RELEASE_LOCK('%s')" % self._mysql_lock)
db.store_result()
if self._transactions:
db.query("COMMIT")
db.store_result()
except:
LOG('ZMySQLDA', ERROR, "exception during _finish",
error=sys.exc_info())
raise
finally:
setattr(self, FINISH_OR_ABORT_CALLED_ID, True)
def _abort(self, *ignored):
if getattr(self, FINISH_OR_ABORT_CALLED_ID, False):
return return
self.setFinishedOrAborted(True)
try: try:
db = self.getConnection() db = self.getConnection()
if self._mysql_lock: if self._mysql_lock:
db.query("SELECT RELEASE_LOCK('%s')" % self._mysql_lock) db.query("SELECT RELEASE_LOCK('%s')" % self._mysql_lock)
db.store_result() db.store_result()
if self._transactions: if self._transactions:
db.query("ROLLBACK") db.query("COMMIT")
db.store_result() db.store_result()
else: except:
LOG('ZMySQLDA', ERROR, "aborting when non-transactional") LOG('ZMySQLDA', ERROR, "exception during _finish",
finally: error=sys.exc_info())
setattr(self, FINISH_OR_ABORT_CALLED_ID, True) raise
def _abort(self, *ignored):
if self.getFinishedOrAborted():
return
self.setFinishedOrAborted(True)
db = self.getConnection()
if self._mysql_lock:
db.query("SELECT RELEASE_LOCK('%s')" % self._mysql_lock)
db.store_result()
if self._transactions:
db.query("ROLLBACK")
db.store_result()
else:
LOG('ZMySQLDA', ERROR, "aborting when non-transactional")
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