-1

Here is my (simplified) flask code

from flask import Flask, request, redirect
from api_v1.api import api as api_blueprint

app = Flask(__name__)
app.register_blueprint(api_blueprint, url_prefix='/api/v1')

if __name__ == '__main__':
    app.run(port=5001, debug=True, host="0.0.0.0")

here is api.py

from flask import Blueprint, request
from api_v1.db.connect_db import Dbs


db = Dbs() api = Blueprint('api/v1/', __name__)

here is connect_db.py

import mysql.connector


class Dbs:
    def __init__(self):
        print("connecting")
        self.source_db = mysql.connector.connect(
            # credentials goes here
        )
        self.main_db = mysql.connector.connect(
           # credentials goes here
        )
        self.source_cursor = self.source_db.cursor()
        self.main_cursor = self.main_db.cursor()
        print("connected")

output:

connecting

connected

  • Serving Flask app "app" (lazy loading)
  • Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
  • Debug mode: on
  • Restarting with stat

connecting

connected

  • Debugger is active!
  • Debugger PIN: 000-00-000 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

The problem is, THIS CONNECTS TWICE TO THE DATABASES

And it's showing duplicate connections from the app in my mysql server

  1. Why is this happening?
  2. Anyone know a solution ?

1 Answers1

0

You are clearly connecting twice as in:

self.source_db = mysql.connector.connect(
            # credentials goes here
        )
self.main_db = mysql.connector.connect(
       # credentials goes here
    )

Connect once and then run USE db_name to change database MySQL Use statement

Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8