def close_instance(self, connection: PooledMySQLConnection, cursor: MySQLCursor) -> None: if connection is not None and connection.is_connected(): if cursor is not None: cursor.close() connection.close() self._logger.info( f"MySQL connection is closed. - PID: {os.getpid()}") else: self._logger.info( "Connection has been disconnect or be killed before.")
def get_connection(self): """Get a connection from the pool This method returns an PooledMySQLConnection instance which has a reference to the pool that created it, and the next available MySQL connection. When the MySQL connection is not connect, a reconnect is attempted. Raises PoolError on errors. Returns a PooledMySQLConnection instance. """ with CONNECTION_POOL_LOCK: try: cnx = self._cnx_queue.get(block=False) except queue.Empty: raise errors.PoolError( "Failed getting connection; pool exhausted") # pylint: disable=W0201,W0212 if not (yield from cnx.is_connected()) \ or self._config_version != cnx._pool_config_version: cnx.config(**self._cnx_config) try: yield from cnx.reconnect() except errors.InterfaceError: # Failed to reconnect, give connection back to pool self._queue_connection(cnx) raise cnx._pool_config_version = self._config_version else: print("get a pooled connector. current pool size is:", self._cnx_queue.qsize() ) return PooledMySQLConnection(self, cnx)
def _get_new_connection(self): """ Creates a new pooled mysql connection and adds it to the existing connection pool. :return: a pooled mysql connection """ connection = self.create_connection() pooled_connection = PooledMySQLConnection(self.connection_pool, connection) return pooled_connection
def show_tables(self, cnx: PooledMySQLConnection = None) -> list: """ 数据库小助手-获取库中的表\n :param cnx: 数据库连接,装饰器自动赋值 :return: 将数据库中表名列表予以返回 """ cursor = cnx.cursor() cursor.execute("SHOW TABLES") tables = [row[0] for row in cursor.fetchall()] return tables
def get_table_fields(self, cnx: PooledMySQLConnection = None, table: str = None): """ 数据库小助手-获取表结构\n :param cnx: 数据库连接,装饰器自动赋值 :param table: 表名 :return: 将表结构的字典列表予以返回(key:字段名,type:字段类型,null:是否为空) """ cursor = cnx.cursor() cursor.execute(f"DESC {table}") feilds = [{ 'key': f[0], 'type': f[1], 'null': 'NULL' if f[2] == 'YES' else 'NOT NULL' } for f in cursor.fetchall()] return feilds
def build_cursor(self, connection: PooledMySQLConnection) -> MySQLCursor: return connection.cursor()
def _close_connection(self, conn: PooledMySQLConnection) -> None: if conn is not None: conn.close()
def _commit(self, conn: PooledMySQLConnection) -> None: conn.commit()
def _is_connected(self, conn: PooledMySQLConnection) -> bool: return conn.is_connected()
def get_conn(self): return PooledMySQLConnection(self, self._pool.get_connection())
def _commit(self, connection: PooledMySQLConnection) -> PooledMySQLConnection: connection.commit() return connection
def _get_cursor(self, connetion: PooledMySQLConnection) -> MySQLCursor: return connetion.cursor()
def _give_connection(self, connection: PooledMySQLConnection) -> None: connection.close()