a
    %ßi!  ã                   @   s\   d Z ddlZddlmZ ddlmZ ddlZdd„ Zdd„ Zdad	d
„ Z	dd„ Z
dd„ ZdS )z[
Database connection pooling.

Uses mysql-connector-python pool for efficient connections.
é    N)Úpooling)Úconfigc                 C   s   t d| › �ƒ d S )Nz[INFO] ©Úprint©Úmsg© r   ú5/var/www/lichun.app/lichun/ws/database/../database.pyÚlog_info   s    r
   c                 C   s   t d| › �ƒ d S )Nz[ERROR] r   r   r   r   r	   Ú	log_error   s    r   c               
   C   s„   t du r€zBtjdttjdƒdtjtjtjtj	tj
d�a tdtj› d�ƒ W n4 ty~ }  ztd| › �ƒ ‚ W Y d} ~ n
d} ~ 0 0 t S )	z]
    Get or create database connection pool.

    Returns:
        MySQL connection pool
    NZbaolife_poolé    T)Ú	pool_nameÚ	pool_sizeÚpool_reset_sessionÚhostÚportÚuserÚpasswordÚdatabasez(Database connection pool created (size: ú)z"Failed to create connection pool: )Ú_connection_poolr   ÚMySQLConnectionPoolÚminr   ÚMAX_CONNECTIONSÚDB_HOSTÚDB_PORTÚDB_USERÚDB_PASSWORDÚDB_NAMEr
   Ú	Exceptionr   )Úer   r   r	   Úget_connection_pool   s"    	
ø
r!   c               
   C   sJ   zt ƒ } |  ¡ W S  tyD } ztd|› �ƒ ‚ W Y d}~n
d}~0 0 dS )a  
    Get database connection from pool.

    Returns:
        MySQL connection

    Usage:
        conn = get_database_connection()
        try:
            cursor = conn.cursor()
            cursor.execute("SELECT ...")
        finally:
            conn.close()  # Returns to pool
    z#Failed to get database connection: N)r!   Úget_connectionr   r   )Úpoolr    r   r   r	   Úget_database_connection4   s    
r$   c                   C   s   t rda tdƒ dS )z+Close all connections in pool (for cleanup)NzDatabase connection pool closed)r   r
   r   r   r   r	   Úclose_connection_poolK   s    r%   )Ú__doc__Úmysql.connectorÚmysqlr   r   Úloggingr
   r   r   r!   r$   r%   r   r   r   r	   Ú<module>   s   