Search code examples
pythonmysqlcursor

dynamic database and dict cursor in MySQLConnectionPool


I have been trying to use connection pool for mysql in python. While searching for options, I came across MySQLConnectionPool. It seems to work decently but I am not able to find how to achieve some of the features of MySQL.

  1. How to dynamically select database.
  2. How to use dict_cursor. For normal MySQL without pool, I use cursorclass=MySQLdb.cursors.DictCursor

If someone knows about how to achieve this, please let me know. Thanks in advance.


Solution

  • I finally am able to achieve this.

    1. For dynamic database, I have used Lock module in python. Because I am using pool for a server, I will get concurrent requests which should not alter the database for the connection pool in its entirety.
    2. For dictionary, while creating cursor I set the value of parameter dictionary to True.

      lock.acquire()
      try:
          self._pool.set_config(**conn_config)  //conn_config contains the modified database details
          conn = self._pool.get_connection()
          cursor = conn.cursor(dictionary=True)
      except PoolError:
         //do something
      lock.release()