示例#1
0
 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)
示例#3
0
 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
示例#4
0
 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
示例#5
0
 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
示例#6
0
 def build_cursor(self, connection: PooledMySQLConnection) -> MySQLCursor:
     return connection.cursor()
示例#7
0
 def _close_connection(self, conn: PooledMySQLConnection) -> None:
     if conn is not None:
         conn.close()
示例#8
0
 def _commit(self, conn: PooledMySQLConnection) -> None:
     conn.commit()
示例#9
0
 def _is_connected(self, conn: PooledMySQLConnection) -> bool:
     return conn.is_connected()
示例#10
0
 def get_conn(self):
     return PooledMySQLConnection(self, self._pool.get_connection())
示例#11
0
 def _commit(self,
             connection: PooledMySQLConnection) -> PooledMySQLConnection:
     connection.commit()
     return connection
示例#12
0
 def _get_cursor(self, connetion: PooledMySQLConnection) -> MySQLCursor:
     return connetion.cursor()
示例#13
0
 def _give_connection(self, connection: PooledMySQLConnection) -> None:
     connection.close()