def _do_exec(self, conn, sql): for retry in range(2): cursor = conn.cursor() try: cursor.execute(sql) conn.commit() return True except pymysql.err.OperationalError as e: errno, _ = e.args if retry == 1 or errno not in [2006, 2013]: log.v('[mysql] SQL={}\n'.format(sql)) log.e('[mysql] _do_exec() failed: {}\n'.format( e.__str__())) return None log.w('[mysql] lost connection, reconnect.\n') with self._locker: thread_id = threading.get_ident() if thread_id not in self._connections: log.e('[mysql] database pool internal error.\n') return None _conn = self._do_connect() if _conn is not None: self._connections[thread_id] = _conn conn = _conn else: return None except Exception as e: log.e('[mysql] _do_exec() failed: {}\n'.format(e.__str__())) log.e('[mysql] SQL={}'.format(sql)) return None finally: cursor.close()
def create_and_init(self, step_begin, step_end): log.v('start database create and initialization process.\n') if self.db_type == self.DB_TYPE_SQLITE: db_path = os.path.dirname(self.sqlite_file) if not os.path.exists(db_path): utils.make_dir(db_path) if not os.path.exists(db_path): log.e( 'can not create folder `{}` to store database file.\n'. format(db_path)) return False # 创建一个空数据文件,这样才能进行connect。 if not os.path.exists(self.sqlite_file): try: with open(self.sqlite_file, 'w') as f: pass except: log.e('can not create db file `{}`.\n'.format( self.sqlite_file)) return False if create_and_init(self, step_begin, step_end): log.v('database created.\n') self.need_create = False return True else: log.e('database create and initialize failed.\n') return False
def stop(self): self._stop_flag = True self._timer_cond.acquire() self._timer_cond.notify() self._timer_cond.release() self.join() log.v('{} stopped.\n'.format(self.name))
def stop_keep_alive(self): self._stop_flag = True self._thread_keep_alive_cond.acquire() self._thread_keep_alive_cond.notify() self._thread_keep_alive_cond.release() if self._thread_keep_alive_handle is not None: self._thread_keep_alive_handle.join() log.v('database-keep-alive-thread stopped.\n')
def upgrade_database(self, step_begin, step_end): log.v('start database upgrade process.\n') if DatabaseUpgrade(self, step_begin, step_end).do_upgrade(): log.v('database upgraded.\n') self.need_upgrade = False return True else: log.e('database upgrade failed.\n') return False
def _do_query(self, conn, sql): cursor = conn.cursor() try: cursor.execute(sql) db_ret = cursor.fetchall() conn.commit() return db_ret except Exception as e: log.v('[mysql] SQL={}\n'.format(sql)) log.e('[mysql] _do_query() failed: {}\n'.format(e.__str__())) return None finally: cursor.close()
def stop(self): self._stop_flag = True self.join() log.v('{} stopped.'.format(self.name))