-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
30 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
from dbutils.pooled_db import PooledDB | ||
import pymysql | ||
|
||
def connect_app(app, config): | ||
print(f"Connecting to database {config['database']} on {config['host']}:{config['port']} as {config['username']}") | ||
app.config['db_connection'] = pymysql.connect( | ||
|
||
app.config['db_pool'] = PooledDB( | ||
creator=pymysql, # the module to use for connecting to the database | ||
host=config['host'], | ||
port=config['port'], | ||
user=config['username'], | ||
password=config['password'], | ||
db=config['database'], | ||
charset='utf8mb4', | ||
cursorclass=pymysql.cursors.DictCursor | ||
cursorclass=pymysql.cursors.DictCursor, | ||
blocking=True, # block and wait for a connection | ||
maxconnections=5 # max number of connections in the pool | ||
) | ||
|
||
# Sometimes db disconnects, this function will reconnect if necessary | ||
def ensure_app_connection(app): | ||
def get_connection(app): | ||
try: | ||
app.config['db_connection'].ping(reconnect=True) | ||
return app.config['db_connection'] | ||
connection = app.config['db_pool'].connection() | ||
return connection | ||
except AttributeError: | ||
raise Exception("Trying to reconnect to database, but no connection exists") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
DBUtils==3.0.3 | ||
Flask==3.0.0 | ||
Flask_Cors==4.0.0 | ||
PyMySQL==1.1.0 |