Commit 6ff0f9bb authored by Vincent Pelletier's avatar Vincent Pelletier

Attemp to reconnect automaticaly once using a method similar to our custom ZMySQLDA connectors.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15137 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 55396821
...@@ -42,6 +42,12 @@ except ImportError: ...@@ -42,6 +42,12 @@ except ImportError:
try: try:
import MySQLdb import MySQLdb
from MySQLdb.constants import CR
from _mysql_exceptions import OperationalError
hosed_connection = (
CR.SERVER_GONE_ERROR,
CR.SERVER_LOST
)
except ImportError: except ImportError:
LOG('SQLCache', 0, 'unable to import MySQLdb') LOG('SQLCache', 0, 'unable to import MySQLdb')
...@@ -132,7 +138,7 @@ class SQLCache(BaseCache): ...@@ -132,7 +138,7 @@ class SQLCache(BaseCache):
## no such table create it ## no such table create it
self.execSQLQuery(self.create_table_sql %self._db_cache_table_name) self.execSQLQuery(self.create_table_sql %self._db_cache_table_name)
def getCacheStorage(self): def getCacheStorage(self, force_reconnect=False):
""" """
Return current DB connection or create a new one for this thread. Return current DB connection or create a new one for this thread.
See http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307 See http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307
...@@ -142,7 +148,7 @@ class SQLCache(BaseCache): ...@@ -142,7 +148,7 @@ class SQLCache(BaseCache):
thread_id = get_ident() thread_id = get_ident()
dbConn = connection_pool.get(thread_id, None) dbConn = connection_pool.get(thread_id, None)
if dbConn is None: if force_reconnect or dbConn is None:
## we don't have dbConn for this thread ## we don't have dbConn for this thread
dbConn = MySQLdb.connect(host=self._db_server, \ dbConn = MySQLdb.connect(host=self._db_server, \
user=self._db_user,\ user=self._db_user,\
...@@ -250,12 +256,25 @@ class SQLCache(BaseCache): ...@@ -250,12 +256,25 @@ class SQLCache(BaseCache):
my_query = self.delete_all_keys_for_scope_sql %(self._db_cache_table_name, scope) my_query = self.delete_all_keys_for_scope_sql %(self._db_cache_table_name, scope)
self.execSQLQuery(my_query) self.execSQLQuery(my_query)
def _execSQLQuery(self, sql_query, connection):
"""
Execute sql query using given connection.
"""
cursor = connection.cursor()
cursor.execute(sql_query)
return cursor
def execSQLQuery(self, sql_query): def execSQLQuery(self, sql_query):
""" """
Try to execute sql query. Try to execute sql query.
Return cursor object because some queris can return result Return cursor object because some queris can return result
""" """
dbConn = self.getCacheStorage() dbConn = self.getCacheStorage()
cursor = dbConn.cursor() try:
cursor.execute(sql_query) cursor = self._execSQLQuery(sql_query, dbConn)
except OperationalError, m:
if m[0] not in hosed_connection:
raise
dbConn = self.getCacheStorage(force_reconnect=True)
cursor = self._execSQLQuery(sql_query, dbConn)
return cursor return cursor
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