Don't just give up if an SQL operation throws an error; try, try, try again... and then some

This commit is contained in:
Terence Simpson 2011-08-26 07:48:13 +01:00
parent 53451ef83f
commit bed505399f

View File

@ -416,7 +416,7 @@ class Bantracker(callbacks.Plugin):
finally: finally:
self.lastMsgs[irc] = msg self.lastMsgs[irc] = msg
def db_run(self, query, parms, expect_result = False, expect_id = False): def db_run(self, query, parms, expect_result = False, expect_id = False, retry = True):
if not self.db or self.db.closed: if not self.db or self.db.closed:
db = self.registryValue('database') db = self.registryValue('database')
if db: if db:
@ -428,12 +428,31 @@ class Bantracker(callbacks.Plugin):
else: else:
self.log.error("Bantracker: no database") self.log.error("Bantracker: no database")
return return
try:
cur = self.db.cursor() count = 0
cur.execute(query, parms) maxCount = 5 #TODO: Make this configurable?
except: err = None
self.log.error("Bantracker: Error while trying to access the Bantracker database.")
return None while count < maxCount:
try:
cur = self.db.cursor()
cur.execute(query, parms)
break
except Exception, err:
count += 1
if count == maxCount:
self.log.error("Bantracker: Error while trying to access the Bantracker database (%s(%s)).", type(err).__name__, str(err))
try:
self.db.close()
except:
pass
self.db = None # force reconnection to database
if not retry: # We probably failed twice, so bigger issues than database locking
return None
return self.db_run(query, parms, expect_result, expect_id, False) # Try again
data = None data = None
if expect_result and cur: data = cur.fetchall() if expect_result and cur: data = cur.fetchall()
if expect_id: data = self.db.insert_id() if expect_id: data = self.db.insert_id()