42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import mysql.connector
|
|
import pydapper
|
|
import logging
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
config_path = None
|
|
|
|
def initPool(instancePath):
|
|
try:
|
|
global config_path
|
|
if(config_path == None):
|
|
config_path = os.path.join(instancePath,'../../../secure/connection_data_test.json');
|
|
|
|
print (config_path)
|
|
|
|
if not os.path.exists(config_path):
|
|
print ('cannot find ' + config_path)
|
|
print("instance path", instancePath)
|
|
sys.exit(1)
|
|
|
|
f = open(config_path);
|
|
connection_data = json.load(f)
|
|
|
|
conn_from_pool = mysql.connector.connect(**connection_data)
|
|
|
|
commands = pydapper.using(conn_from_pool)
|
|
data = commands.query("SELECT id from `user`")
|
|
print("DB connection successful")
|
|
conn_from_pool.close()
|
|
except mysql.connector.PoolError as e:
|
|
logging.error(f"Failed to create connection pool: {e}")
|
|
print(e)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
def getPoolConnection():
|
|
global config_path
|
|
f = open(config_path);
|
|
connection_data = json.load(f)
|
|
return mysql.connector.connect(**connection_data) |