
    >|h                      h    d Z ddlmZ  G d de      Z G d de      Z G d d      Z G d	 d
      Zy)a:  SimplePooledDB - a very simple DB-API 2 database connection pool.

Implements a pool of threadsafe cached DB-API 2 connections
to a database which are transparently reused.

This should result in a speedup for persistent applications
such as the "Webware for Python" AppServer.

For more information on the DB-API 2, see:
    https://www.python.org/dev/peps/pep-0249/
For more information on Webware for Python, see:
    https://webwareforpython.github.io/w4py/

Measures are taken to make the pool of connections threadsafe
regardless of whether the DB-API 2 module used is threadsafe
on the connection level (threadsafety > 1) or not.  It must only
be threadsafe on the module level (threadsafety = 1).  If the
DB-API 2 module is threadsafe, the connections will be shared
between threads (keep this in mind if you use transactions).

Usage:

The idea behind SimplePooledDB is that it's completely transparent.
After you have established your connection pool, stating the
DB-API 2 module to be used, the number of connections
to be cached in the pool and the connection parameters, e.g.

    import pgdb  # import used DB-API 2 module
    from dbutils.simple_pooled_db import PooledDB
    dbpool = PooledDB(pgdb, 5, host=..., database=..., user=..., ...)

you can demand database connections from that pool,

    db = dbpool.connection()

and use them just as if they were ordinary DB-API 2 connections.
It's really just a proxy class.

db.close() will return the connection to the pool, it will not
actually close it.  This is so your existing code works nicely.

Ideas for improvement:

* Do not create the maximum number of connections on startup
already, but only a certain number and the rest on demand.
* Detect and transparently reset "bad" connections.
* Connections should have some sort of maximum usage limit
after which they should be automatically closed and reopened.
* Prefer or enforce thread-affinity for the connections,
allowing for both shareable and non-shareable connections.

Please note that these and other ideas have been already
implemented in in PooledDB, a more sophisticated version
of SimplePooledDB.  You might also consider using PersistentDB
instead for thread-affine persistent database connections.
SimplePooledDB may still serve as a very simple reference
and example implementation for developers.


Copyright, credits and license:

* Contributed as MiscUtils/DBPool for Webware for Python
  by Dan Green, December 2000
* Thread safety bug found by Tom Schwaller
* Fixes by Geoff Talvola (thread safety in _threadsafe_getConnection())
* Clean up by Chuck Esterbrook
* Fix unthreadsafe functions which were leaking, Jay Love
* Eli Green's webware-discuss comments were lifted for additional docs
* Clean-up and detailed commenting, rename and move to DBUtils
  by Christoph Zwerschke in September 2005

Licensed under the MIT license.
   )__version__c                       e Zd ZdZy)PooledDBErrorzGeneral PooledDB error.N__name__
__module____qualname____doc__     W/var/www/html/test/engine/venv/lib/python3.12/site-packages/dbutils/simple_pooled_db.pyr   r   N   s    !r   r   c                       e Zd ZdZy)NotSupportedErrorz(DB-API module not supported by PooledDB.Nr   r   r   r   r   r   R   s    2r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)PooledDBConnectionzA proxy class for pooled database connections.

    You don't normally deal with this class directly,
    but use PooledDB to get new connections.
    c                      || _         || _        y)zInitialize pooled connection.N)_con_pool)selfpoolcons      r   __init__zPooledDBConnection.__init__]   s    	
r   c                 v    | j                   -| j                  j                  | j                          d| _         yy)zClose the pooled connection.N)r   r   returnConnectionr   s    r   closezPooledDBConnection.closeb   s0     99 JJ''		2DI !r   c                 .    t        | j                  |      S )z&Get the attribute with the given name.)getattrr   )r   names     r   __getattr__zPooledDBConnection.__getattr__j   s     tyy$''r   c                 $    | j                          y)zDelete the pooled connection.N)r   r   s    r   __del__zPooledDBConnection.__del__o   s    

r   N)r   r   r	   r
   r   r   r    r"   r   r   r   r   r   V   s    
(
r   r   c                   >    e Zd ZdZeZd Zd Zd Zd Z	d Z
d Zd Zy	)
PooledDBzA very simple database connection pool.

    After you have created the connection pool,
    you can get connections using getConnection().
    c                     	 |j                   }|dk(  rt        d      |dk(  rGddlm}  ||      | _        | j                  | _        | j                  | _	        | j                  | _        nc|dv rTddlm}  |       | _        d| _        g | _        | j"                  | _        | j$                  | _	        | j&                  | _        nt        d      t)        |      D ]#  }| j                   |j*                  |i |       % y# t        $ r d}Y w xY w)	a*  Set up the database connection pool.

        dbapi: the DB-API 2 compliant module you want to use
        maxconnections: the number of connections cached in the pool
        args, kwargs: the parameters that shall be used to establish
            the database connections using connect()
        N    z8Database module does not support any level of threading.r   )Queue)      )Lockz7Database module threading support cannot be determined.)threadsafety	Exceptionr   queuer'   _queue_unthreadsafe_get_connection
connection_unthreadsafe_add_connectionaddConnection_unthreadsafe_return_connectionr   	threadingr*   _lock_nextConnection_connections_threadsafe_get_connection_threadsafe_add_connection_threadsafe_return_connectionrangeconnect)	r   dbapimaxconnectionsargskwargsr+   r'   r*   _is	            r   r   zPooledDB.__init__}   s   	  --L 1#JL L1 $/DK"??DO!%!B!BD$($H$HD!V# 'DJ#$D  "D"==DO!%!@!@D$($F$FD!#IK K ' 	?B}u}}d=f=>	?=  	 L	 s   C? ?DDc                 J    t        | | j                  j                               S )Get a connection from the pool.)r   r.   getr   s    r   r/   z%PooledDB._unthreadsafe_get_connection   s    !$(9::r   c                 :    | j                   j                  |       yzAdd a connection to the pool.N)r.   putr   r   s     r   r1   z%PooledDB._unthreadsafe_add_connection   s    r   c                 &    | j                  |       y)a  Return a connection to the pool.

        In this case, the connections need to be put
        back into the queue after they have been used.
        This is done automatically when the connection is closed
        and should never be called explicitly outside of this module.
        N)r1   rH   s     r   r3   z(PooledDB._unthreadsafe_return_connection   s     	))#.r   c                     | j                   5  | j                  }t        | | j                  |         }|dz  }|t	        | j                        k\  rd}|| _        |cddd       S # 1 sw Y   yxY w)rC   r   r&   N)r5   r6   r   r7   len)r   next_conr   s      r   r8   z#PooledDB._threadsafe_get_connection   sl    ZZ 	++H$T4+<+<X+FGCMH3t0011#+D 	 	 	s   AA$$A-c                 :    | j                   j                  |       yrF   )r7   appendrH   s     r   r9   z#PooledDB._threadsafe_add_connection   s      %r   c                      y)zReturn a connection to the pool.

        In this case, the connections always stay in the pool,
        so there is no need to do anything here.
        Nr   rH   s     r   r:   z&PooledDB._threadsafe_return_connection   s    r   N)r   r   r	   r
   r   versionr   r/   r1   r3   r8   r9   r:   r   r   r   r$   r$   t   s2     G)?b;/	&r   r$   N)r
    r   r,   r   r   r   r$   r   r   r   <module>rR      s?   HT "I "3 3 <e er   