def run(self): connection = DbSource(self.dbConnectionInfo).getConnection() while self.doRun or len(self.toDoStatements) > 0: if len(self.toDoStatements) > 0: with self.toDoStatementsLock: try: cur = connection.cursor() while len(self.toDoStatements) > 0: sql = self.toDoStatements.pop() # print(f"[INFO] dbThread sql: {sql}") try: cur.execute(sql) except Exception as ex: sys.stderr.write("error in statement: %s\n" % sql) sys.stderr.write(str(type(ex)) + "\n") sys.stderr.write(str(ex) + "\n") connection.commit() cur.close() except Exception as e: print('[ERROR] in DbThread:', e) connection = DbSource( self.dbConnectionInfo).getConnection() else: time.sleep(1) if connection: connection.commit() connection.close() print("DbThread terminated")
def run(self): numEmptyLoops = 0 connection = DbSource(self.dbConnectionInfo).getConnection() while self.doRun or len(self.toDoStatements) > 0: if len(self.toDoStatements) > 0: numEmptyLoops = 0 with self.toDoStatementsLock: sql = None try: cur = connection.cursor() while len(self.toDoStatements) > 0: sql = self.toDoStatements.pop() # print(f"[INFO] dbThread sql: {sql}") try: cur.execute(sql) connection.commit() except (sqlite3.OperationalError, pymysql.err.OperationalError) as ex: sys.stderr.write("error in statement: %s\n" % sql) sys.stderr.write(str(type(ex)) + "\n") sys.stderr.write(str(ex) + "\n") cur.close() except Exception as ex: print('[ERROR] in DbThread (1):', ex) connection = DbSource( self.dbConnectionInfo).getConnection() self.toDoStatements.append(sql) # requeue for retry else: time.sleep(1) numEmptyLoops += 1 if numEmptyLoops > 60: numEmptyLoops = 0 try: cur = connection.cursor() cur.execute( 'SELECT 1;') # to prevent connection timeouts cur.close() except ex: print(f"[ERROR] in DbThread (2):", ex) if connection: connection.commit() connection.close() print("DbThread terminated")