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 rzBt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   