Commit 93aef5ed authored by Julien Muchembled's avatar Julien Muchembled

mysql: log failed query in case of database failure

parent f8ce322b
...@@ -33,6 +33,11 @@ from neo.lib.exception import DatabaseFailure ...@@ -33,6 +33,11 @@ from neo.lib.exception import DatabaseFailure
from neo.lib.protocol import CellStates, ZERO_OID, ZERO_TID, ZERO_HASH from neo.lib.protocol import CellStates, ZERO_OID, ZERO_TID, ZERO_HASH
def getPrintableQuery(query, max=70):
return ''.join(c if c in string.printable and c not in '\t\x0b\x0c\r'
else '\\x%02x' % ord(c) for c in query)
class MySQLDatabaseManager(DatabaseManager): class MySQLDatabaseManager(DatabaseManager):
"""This class manages a database on MySQL.""" """This class manages a database on MySQL."""
...@@ -92,17 +97,12 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -92,17 +97,12 @@ class MySQLDatabaseManager(DatabaseManager):
def query(self, query): def query(self, query):
"""Query data from a database.""" """Query data from a database."""
if LOG_QUERIES:
logging.debug('querying %s...',
getPrintableQuery(query.split('\n', 1)[0][:70]))
while 1:
conn = self.conn conn = self.conn
try: try:
if LOG_QUERIES:
printable_char_list = []
for c in query.split('\n', 1)[0][:70]:
if c not in string.printable or c in '\t\x0b\x0c\r':
c = '\\x%02x' % ord(c)
printable_char_list.append(c)
query_part = ''.join(printable_char_list)
logging.debug('querying %s...', query_part)
conn.query(query) conn.query(query)
if query.startswith("SELECT "): if query.startswith("SELECT "):
r = conn.store_result() r = conn.store_result()
...@@ -110,17 +110,18 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -110,17 +110,18 @@ class MySQLDatabaseManager(DatabaseManager):
tuple([d.tostring() if isinstance(d, array) else d tuple([d.tostring() if isinstance(d, array) else d
for d in row]) for d in row])
for row in r.fetch_row(r.num_rows())]) for row in r.fetch_row(r.num_rows())])
break
except OperationalError, m:
if self._active or m[0] not in (SERVER_GONE_ERROR, SERVER_LOST):
raise DatabaseFailure('MySQL error %d: %s\nQuery: %s'
% (m[0], m[1], getPrintableQuery(query[:1000])))
logging.info('the MySQL server is gone; reconnecting')
self._connect()
r = query.split(None, 1)[0] r = query.split(None, 1)[0]
if r in ("INSERT", "REPLACE", "DELETE", "UPDATE"): if r in ("INSERT", "REPLACE", "DELETE", "UPDATE"):
self._active = 1 self._active = 1
else: else:
assert r in ("ALTER", "CREATE", "DROP", "TRUNCATE"), query assert r in ("ALTER", "CREATE", "DROP", "TRUNCATE"), query
except OperationalError, m:
if m[0] in (SERVER_GONE_ERROR, SERVER_LOST) and not self._active:
logging.info('the MySQL server is gone; reconnecting')
self._connect()
return self.query(query)
raise DatabaseFailure('MySQL error %d: %s' % (m[0], m[1]))
@property @property
def escape(self): def escape(self):
......
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