def add_connection(self, cnx=None): """Add a connection to the pool This method instantiates a MySQLConnection using the configuration passed when initializing the MySQLConnectionPool instance or using the set_config() method. If cnx is a MySQLConnection instance, it will be added to the queue. Raises PoolError when no configuration is set, when no more connection can be added (maximum reached) or when the connection can not be instantiated. """ with CONNECTION_POOL_LOCK: if not self._cnx_config: raise errors.PoolError( "Connection configuration not available") if self._cnx_queue.full(): raise errors.PoolError( "Failed adding connection; queue is full") if not cnx: cnx = MySQLConnection(**self._cnx_config) # pylint: disable=W0212 cnx._pool_config_version = self._config_version # pylint: enable=W0212 else: if not isinstance(cnx, MySQLConnection): raise errors.PoolError( "Connection instance not subclass of MySQLConnection.") self._queue_connection(cnx)
def pool_connection(self, cnx): if not isinstance(cnx, MySQLConnection): raise errors.PoolError( "Connection instance not subclass of MySQLConnection.") try: self._cnx_pool.put(cnx, block=False) except queue.Full: errors.PoolError("Queue is full")
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=W0212 if (not cnx.is_connected() or self._config_version != cnx._pool_config_version): cnx.config(**self._cnx_config) try: 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 # pylint: enable=W0212 return PooledMySQLConnection(self, cnx)
def _queue_connection(self, cnx): """Put connection back in the queue This method is putting a connection back in the queue. It will not acquire a lock as the methods using _queue_connection() will have it set. Raises PoolError on errors. """ if not isinstance(cnx, MySQLConnection): raise errors.PoolError( "Connection instance not subclass of MySQLConnection.") try: self._cnx_queue.put(cnx, block=False) except queue.Full: errors.PoolError("Failed adding connection; queue is full")
def add_connection(self, cnx=None): """Add a connection to the pool This method instantiates a MySQLConnection using the configuration passed when initializing the MySQLConnectionPool instance or using the set_config() method. If cnx is a MySQLConnection instance, it will be added to the queue. Raises PoolError when no configuration is set, when no more connection can be added (maximum reached) or when the connection can not be instantiated. """ with CONNECTION_POOL_LOCK: if not self._cnx_config: raise errors.PoolError( "Connection configuration not available") if self._cnx_queue.full(): raise errors.PoolError( "Failed adding connection; queue is full") if not cnx: cnx = AioMySQLConnection(**self._cnx_config) try: if (self._reset_session and self._cnx_config['compress'] and cnx.get_server_version() < (5, 7, 3)): raise errors.NotSupportedError("Pool reset session is " "not supported with " "compression for MySQL " "server version 5.7.2 " "or earlier.") except KeyError: pass # pylint: disable=W0201,W0212 cnx._pool_config_version = self._config_version # pylint: enable=W0201,W0212 else: if not isinstance(cnx, AioMySQLConnection): raise errors.PoolError( "Connection instance not subclass of AioMySQLConnection.") self._queue_connection(cnx)
def get_connection(self): with CONNECTION_POOL_LOCK: try: cnx = self._cnx_pool.get(block=False) self._recycle(cnx) self._used_connections += 1 # If queue is empty but used_connections is lower than # pool_max_size, create a new connection except queue.Empty: if self._used_connections < self._pool_max_size and not self._cnx_pool.full( ): cnx = self._create_connection() self._used_connections += 1 pass else: raise errors.PoolError("Pool exhausted") except Error as err: raise err return cnx
def set_config(self, **kwargs): """Set the connection configuration for MySQLConnection instances This method sets the configuration used for creating MySQLConnection instances. See MySQLConnection for valid connection arguments. Raises PoolError when a connection argument is not valid, missing or not supported by MySQLConnection. """ if not kwargs: return with CONNECTION_POOL_LOCK: try: test_cnx = MySQLConnection() test_cnx.config(**kwargs) self._cnx_config = kwargs self._config_version = uuid4() except AttributeError as err: raise errors.PoolError( "Connection configuration not valid: {0}".format(err))
def generate_pool_name(**kwargs): """Generate a pool name This function takes keyword arguments, usually the connection arguments for MySQLConnection, and tries to generate a name for a pool. Raises PoolError when no name can be generated. Returns a string. """ parts = [] for key in ('host', 'port', 'user', 'database'): try: parts.append(str(kwargs[key])) except KeyError: pass if not parts: raise errors.PoolError( "Failed generating pool name; specify pool_name") return '_'.join(parts)
def config(self, **kwargs): """Configuration is done through the pool""" raise errors.PoolError("Configuration for pooled connections should " "be done through the pool itself.")