U e]0@sdZddlZddlmZz ddlZWnek r@ddlZYnXddlZddlm Z ddl m Z e Z dZdZed Zd d ZGd d d eZGdddeZdS)z6Implementing pooling of connections to MySQL servers. N)uuid4)errors)MySQLConnection @z[^a-zA-Z0-9._:\-*$#]c KsRg}dD]0}z|t||Wqtk r6YqXq|sHtdd|S)zGenerate 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. )hostportuserZdatabasez.Failed generating pool name; specify pool_name_)appendstrKeyErrorr PoolErrorjoin)kwargspartskeyrl/opt/hc_python/lib64/python3.8/site-packages/../../../lib/python3.8/site-packages/mysql/connector/pooling.pygenerate_pool_name/s rc@s<eZdZdZddZddZddZdd Zed d Z d S) PooledMySQLConnectionaClass holding a MySQL Connection in a pool PooledMySQLConnection is used by MySQLConnectionPool to return an instance holding a MySQL connection. It works like a MySQLConnection except for methods like close() and config(). The close()-method will add the connection back to the pool rather than disconnecting from the MySQL server. Configuring the connection have to be done through the MySQLConnectionPool method set_config(). Using config() on pooled connection will raise a PoolError. cCs4t|tstdt|ts$td||_||_dS)zInitialize The pool argument must be an instance of MySQLConnectionPoll. cnx if an instance of MySQLConnection. z$pool should be a MySQLConnectionPoolzcnx should be a MySQLConnectionN) isinstanceMySQLConnectionPoolAttributeErrorr _cnx_pool_cnx)selfpoolcnxrrr__init__Vs  zPooledMySQLConnection.__init__cCs t|j|S)z0Calls attributes of the MySQLConnection instance)getattrr)rattrrrr __getattr__esz!PooledMySQLConnection.__getattr__cCs,|j}|jjr||j|d|_dS)aYDo not close, but add connection back to pool The close() method does not close the connection with the MySQL server. The connection is added back to the pool so it can be reused. When the pool is configured to reset the session, the session state will be cleared by re-authenticating the user. N)rr reset_sessionadd_connectionrrrrrcloseis  zPooledMySQLConnection.closecKstddS)z&Configuration is done through the poolzLConfiguration for pooled connections should be done through the pool itself.N)rr)rrrrrconfigzszPooledMySQLConnection.configcCs|jjSz&Return the name of the connection pool)r pool_namerrrrr*szPooledMySQLConnection.pool_nameN) __name__ __module__ __qualname____doc__r r#r'r(propertyr*rrrrrHs rc@sxeZdZdZdddZeddZed d Zed d Zd dZ ddZ ddZ ddZ dddZ ddZddZdS)rz*Class defining a pool of MySQL connectionsNTcKsd|_d|_||_||||p,tf|i|_t|j|_ t |_ |r||j f|d}||jkr|| |d7}q`dS)zInitialize Initialize a MySQL connection pool with a maximum number of connections set to pool_size. The rest of the keywords arguments, kwargs, are configuration arguments for MySQLConnection instances. Nrr) _pool_size _pool_name_reset_session_set_pool_size_set_pool_namer _cnx_configqueueQueue _cnx_queuer_config_version set_configr%)r pool_sizer*Zpool_reset_sessionrcntrrrr s    zMySQLConnectionPool.__init__cCs|jSr))r3r+rrrr*szMySQLConnectionPool.pool_namecCs|jS)z0Return number of connections managed by the pool)r2r+rrrr=szMySQLConnectionPool.pool_sizecCs|jS)zReturn whether to reset session)r4r+rrrr$sz!MySQLConnectionPool.reset_sessionc Kst|sdSt^z$t}|jf|||_t|_Wn2tk rd}ztd |W5d}~XYnXW5QRXdS)aTSet 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. Nz'Connection configuration not valid: {0}) CONNECTION_POOL_LOCKrr(r7rr;rrrformat)rrZtest_cnxerrrrrr<s   zMySQLConnectionPool.set_configcCs(|dks|tkrtdt||_dS)aSet the size of the pool This method sets the size of the pool but it will not resize the pool. Raises an AttributeError when the pool_size is not valid. Invalid size is 0, negative or higher than pooling.CNX_POOL_MAXSIZE. rz;Pool size should be higher than 0 and lower or equal to {0}N)CNX_POOL_MAXSIZErr@r2)rr=rrrr5sz"MySQLConnectionPool._set_pool_sizecCs<t|rtd|t|tkr2td|||_dS)aSet the name of the pool This method checks the validity and sets the name of the pool. Raises an AttributeError when pool_name contains illegal characters ([^a-zA-Z0-9._\-*$#]) or is longer than pooling.CNX_POOL_MAXNAMESIZE. z+Pool name '{0}' contains illegal characterszPool name '{0}' is too longN)CNX_POOL_NAMEREGEXsearchrr@lenCNX_POOL_MAXNAMESIZEr3)rr*rrrr6s  z"MySQLConnectionPool._set_pool_namecCsNt|tstdz|jj|ddWn tjk rHtdYnXdS)zPut 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. 4Connection instance not subclass of MySQLConnection.Fblock'Failed adding connection; queue is fullN)rrrrr:putr8Fullr&rrr_queue_connections z%MySQLConnectionPool._queue_connectionc Cst|jstd|jr*td|stf|j}z*|jrb|jdrb|dkrbt dWnt k rxYnX|j |_ nt |tstd||W5QRXdS)aAdd 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. z&Connection configuration not availablerJcompress)r1z_Pool reset session is not supported with compression for MySQL server version 5.7.2 or earlier.rGN)r?r7rrr:fullrr4Zget_server_versionZNotSupportedErrorrr;_pool_config_versionrrMr&rrrr%s0      z"MySQLConnectionPool.add_connectionc Cstz|jjdd}Wn tjk r8tdYnX|rN|j|j kr|j f|j z | Wn"tj k r||YnX|j|_ t||W5QRSQRXdS)awGet 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. FrHz)Failed getting connection; pool exhaustedN)r?r:getr8EmptyrrZ is_connectedr;rRr(r7Z reconnectZInterfaceErrorrMrr&rrrget_connection$s$     z"MySQLConnectionPool.get_connectionc Cstd}|j}|rz |jdd}||d7}Wqtjk r^|YW5QRStjk rtYqtj k rYqXq|W5QRSQRXdS)zClose all connections This method closes all connections. It returns the number of connections it closed. Used mostly for tests. Returns int. rFrHrN) r?r:qsizerSZ disconnectr8rTrrError)rr>Zcnxqrrrr_remove_connectionsGs   z'MySQLConnectionPool._remove_connections)r1NT)N)r,r-r.r/r r0r*r=r$r<r5r6rMr%rUrXrrrrrs     -#r)r/reuuidrr8 ImportErrorr9 threadingr connectionrRLockr?rBrFcompilerCrobjectrrrrrrs      ?