
5dV                 @   s   d  Z  d d l Z d d l j Z Gd d   d e j  Z Gd d   d e  Z Gd d   d e  Z	 Gd	 d
   d
 e  Z
 Gd d   d e  Z d S)z`Connection pooling for psycopg2

This module implements thread-safe (and not) connection pools.
    Nc               @   s   e  Z d  Z d S)	PoolErrorN)__name__
__module____qualname__ r   r   1/tmp/pip-build-0jahl3lb/psycopg2/psycopg2/pool.pyr      s   r   c               @   sj   e  Z d  Z d Z d d   Z d d d  Z d d   Z d d	 d
  Z d d d d  Z d d   Z	 d S)AbstractConnectionPoolzGeneric key-based pooling code.c             O   s   t  |  |  _ t  |  |  _ d |  _ | |  _ | |  _ g  |  _ i  |  _ i  |  _ d |  _	 x! t
 |  j  D] } |  j   qm Wd S)zInitialize the connection pool.

        New 'minconn' connections are created immediately calling 'connfunc'
        with given parameters. The connection pool will support a maximum of
        about 'maxconn' connections.        
        Fr   N)intminconnmaxconnclosed_args_kwargs_pool_used_rused_keysrange_connect)selfr
   r   argskwargsir   r   r   __init__&   s    							zAbstractConnectionPool.__init__Nc             C   s[   t  j |  j |  j   } | d k	 rG | |  j | <| |  j t |  <n |  j j |  | S)z;Create a new connection and assign it to 'key' if not None.N)	psycopg2connectr   r   r   r   idr   append)r   keyconnr   r   r   r   <   s    zAbstractConnectionPool._connectc             C   s   |  j  d 7_  |  j  S)zReturn a new unique key.   )r   )r   r   r   r   _getkeyF   s    zAbstractConnectionPool._getkeyc             C   s   |  j  r t d   n  | d k r3 |  j   } n  | |  j k rM |  j | S|  j r |  j j   |  j | <} | |  j t |  <| St |  j  |  j	 k r t d   n  |  j
 |  Sd S)z9Get a free connection and assign it to 'key' if not None.zconnection pool is closedNzconnection pool exhausted)r   r   r!   r   r   popr   r   lenr   r   )r   r   r   r   r   r   _getconnK   s    	  	zAbstractConnectionPool._getconnFc             C   s*  |  j  r t d   n  | d k r? |  j j t |   } n  | sT t d   n  t |  j  |  j k  r | r | j  s | j   } | t	 j
 k r | j   q | t	 j k r | j   |  j j |  q |  j j |  q n
 | j   |  j  s	| |  j k r&|  j | =|  j t |  =n  d S)zPut away a connection.zconnection pool is closedNz trying to put unkeyed connection)r   r   r   getr   r#   r   r
   Zget_transaction_status_extZTRANSACTION_STATUS_UNKNOWNcloseZTRANSACTION_STATUS_IDLErollbackr   r   )r   r   r   r'   statusr   r   r   _putconn\   s&    	  	


zAbstractConnectionPool._putconnc          
   C   se   |  j  r t d   n  x= |  j t |  j j    D] } y | j   Wq5 Yq5 Xq5 Wd |  _  d S)zClose all connections.

        Note that this can lead to some code fail badly when trying to use
        an already closed connection. If you call .closeall() make sure
        your code can deal with it.
        zconnection pool is closedTN)r   r   r   listr   valuesr'   )r   r   r   r   r   	_closeall}   s    	 #	z AbstractConnectionPool._closeall)
r   r   r   __doc__r   r   r!   r$   r*   r-   r   r   r   r   r   #   s   
!r   c               @   s1   e  Z d  Z d Z e j Z e j Z e j	 Z
 d S)SimpleConnectionPoolz@A connection pool that can't be shared across different threads.N)r   r   r   r.   r   r$   getconnr*   putconnr-   closeallr   r   r   r   r/      s   		r/   c               @   sR   e  Z d  Z d Z d d   Z d d d  Z d d d d d	  Z d
 d   Z d S)ThreadedConnectionPoolz7A connection pool that works with the threading module.c             O   s8   d d l  } t j |  | | | |  | j   |  _ d S)zInitialize the threading lock.r   N)	threadingr   r   Lock_lock)r   r
   r   r   r   r4   r   r   r   r      s    zThreadedConnectionPool.__init__Nc          
   C   s3   |  j  j   z |  j |  SWd |  j  j   Xd S)z9Get a free connection and assign it to 'key' if not None.N)r6   acquirer$   release)r   r   r   r   r   r0      s    zThreadedConnectionPool.getconnFc          
   C   s9   |  j  j   z |  j | | |  Wd |  j  j   Xd S)zPut away an unused connection.N)r6   r7   r*   r8   )r   r   r   r'   r   r   r   r1      s    zThreadedConnectionPool.putconnc          
   C   s0   |  j  j   z |  j   Wd |  j  j   Xd S)z6Close all connections (even the one currently in use.)N)r6   r7   r-   r8   )r   r   r   r   r2      s    zThreadedConnectionPool.closeall)r   r   r   r.   r   r0   r1   r2   r   r   r   r   r3      s
   r3   c               @   sL   e  Z d  Z d Z d d   Z d d   Z d d d d	  Z d
 d   Z d S)PersistentConnectionPoola  A pool that assigns persistent connections to different threads. 

    Note that this connection pool generates by itself the required keys
    using the current thread id.  This means that until a thread puts away
    a connection it will always get the same connection object by successive
    `!getconn()` calls. This also means that a thread can't use more than one
    single connection from the pool.
    c             O   si   d d l  } | j d t  d d l } t j |  | | | |  | j   |  _ d d l } t	 |  _
 d S)zInitialize the threading lock.r   Nz.deprecated: use ZPsycopgDA.pool implementation)warningswarnDeprecationWarningr4   r   r   r5   r6   _threadthread!_PersistentConnectionPool__thread)r   r
   r   r   r   r:   r4   r=   r   r   r   r      s    	z!PersistentConnectionPool.__init__c          
   C   sB   |  j  j   } |  j j   z |  j |  SWd |  j j   Xd S)z+Generate thread id and return a connection.N)r?   	get_identr6   r7   r$   r8   )r   r   r   r   r   r0      s
    z PersistentConnectionPool.getconnNFc          
   C   s^   |  j  j   } |  j j   z- | s5 |  j | } n  |  j | | |  Wd |  j j   Xd S)zPut away an unused connection.N)r?   r@   r6   r7   r   r*   r8   )r   r   r'   r   r   r   r   r1      s     z PersistentConnectionPool.putconnc          
   C   s0   |  j  j   z |  j   Wd |  j  j   Xd S)z6Close all connections (even the one currently in use.)N)r6   r7   r-   r8   )r   r   r   r   r2      s    z!PersistentConnectionPool.closeall)r   r   r   r.   r   r0   r1   r2   r   r   r   r   r9      s
   	
r9   )r.   r   Zpsycopg2.extensions
extensionsr&   Errorr   objectr   r/   r3   r9   r   r   r   r   <module>   s   j#