database connection pooling
This commit is contained in:
parent
2a49e280c2
commit
b3054b3dda
@ -1,10 +1,15 @@
|
|||||||
import mysql.connector
|
import mysql.connector.pooling
|
||||||
from flask import jsonify, request, current_app
|
from flask import jsonify, request, current_app
|
||||||
from ktvmanager.lib.checker import single_account_check
|
from ktvmanager.lib.checker import single_account_check
|
||||||
from ktvmanager.lib.encryption import encrypt_password, decrypt_password
|
from ktvmanager.lib.encryption import encrypt_password, decrypt_password
|
||||||
|
|
||||||
def _create_connection():
|
db_pool = None
|
||||||
return mysql.connector.connect(
|
|
||||||
|
def initialize_db_pool():
|
||||||
|
global db_pool
|
||||||
|
db_pool = mysql.connector.pooling.MySQLConnectionPool(
|
||||||
|
pool_name="ktv_pool",
|
||||||
|
pool_size=5,
|
||||||
host=current_app.config["DBHOST"],
|
host=current_app.config["DBHOST"],
|
||||||
user=current_app.config["DBUSER"],
|
user=current_app.config["DBUSER"],
|
||||||
password=current_app.config["DBPASS"],
|
password=current_app.config["DBPASS"],
|
||||||
@ -13,7 +18,7 @@ def _create_connection():
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _execute_query(query, params=None):
|
def _execute_query(query, params=None):
|
||||||
conn = _create_connection()
|
conn = db_pool.get_connection()
|
||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor(dictionary=True)
|
||||||
try:
|
try:
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
@ -33,7 +38,6 @@ def get_user_id_from_username(username):
|
|||||||
if result:
|
if result:
|
||||||
return result[0]['id']
|
return result[0]['id']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_user_accounts(user_id):
|
def get_user_accounts(user_id):
|
||||||
query = "SELECT * FROM userAccounts WHERE userID = %s"
|
query = "SELECT * FROM userAccounts WHERE userID = %s"
|
||||||
accounts = _execute_query(query, (user_id,))
|
accounts = _execute_query(query, (user_id,))
|
||||||
|
@ -3,6 +3,7 @@ from flask import Flask, jsonify
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from ktvmanager.config import DevelopmentConfig, ProductionConfig
|
from ktvmanager.config import DevelopmentConfig, ProductionConfig
|
||||||
from routes.api import api_blueprint
|
from routes.api import api_blueprint
|
||||||
|
from ktvmanager.lib.database import initialize_db_pool
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -13,6 +14,9 @@ def create_app():
|
|||||||
else:
|
else:
|
||||||
app.config.from_object(DevelopmentConfig)
|
app.config.from_object(DevelopmentConfig)
|
||||||
|
|
||||||
|
with app.app_context():
|
||||||
|
initialize_db_pool()
|
||||||
|
|
||||||
# Register blueprints
|
# Register blueprints
|
||||||
app.register_blueprint(api_blueprint)
|
app.register_blueprint(api_blueprint)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user