Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:39:06

0001 import sys
0002 from threading import Lock
0003 
0004 from pandacommon.pandalogger.PandaLogger import PandaLogger
0005 
0006 from pandaserver.config import panda_config
0007 
0008 _logger = PandaLogger().getLogger("Initializer")
0009 
0010 
0011 class Initializer:
0012     """
0013     Initialize a dummy database connection.
0014 
0015     Returns:
0016         bool: True if the initialization is successful, False otherwise.
0017     """
0018 
0019     def __init__(self):
0020         self.lock = Lock()
0021         self.first = True
0022 
0023     def init(self):
0024         _logger.debug(f"init new={self.first}")
0025         # do nothing when nDBConnection is 0
0026         if panda_config.nDBConnection == 0:
0027             return True
0028         # lock
0029         self.lock.acquire()
0030         if self.first:
0031             self.first = False
0032             try:
0033                 _logger.debug("connect")
0034                 # connect
0035                 if panda_config.backend == "oracle":
0036                     import oracledb
0037 
0038                     oracledb.init_oracle_client()
0039                     conn = oracledb.connect(user=panda_config.dbuser, password=panda_config.dbpasswd, dsn=panda_config.dbhost)
0040 
0041                 elif panda_config.backend == "postgres":
0042                     import psycopg2
0043 
0044                     conn = psycopg2.connect(
0045                         host=panda_config.dbhost,
0046                         dbname=panda_config.dbname,
0047                         port=panda_config.dbport,
0048                         connect_timeout=panda_config.dbtimeout,
0049                         user=panda_config.dbuser,
0050                         password=panda_config.dbpasswd,
0051                     )
0052 
0053                 else:
0054                     import MySQLdb
0055 
0056                     conn = MySQLdb.connect(
0057                         host=panda_config.dbhost,
0058                         db=panda_config.dbname,
0059                         port=panda_config.dbport,
0060                         connect_timeout=panda_config.dbtimeout,
0061                         user=panda_config.dbuser,
0062                         passwd=panda_config.dbpasswd,
0063                     )
0064 
0065                 # close
0066                 conn.close()
0067                 _logger.debug("done")
0068 
0069             except Exception:
0070                 self.lock.release()
0071                 exception_type, exception_value, traceback = sys.exc_info()
0072                 _logger.error(f"connect : {exception_type} {exception_value}")
0073                 return False
0074 
0075         # release the lock
0076         self.lock.release()
0077         return True
0078 
0079 
0080 # singleton
0081 initializer = Initializer()
0082 del Initializer