Skip to content

Commit

Permalink
Merge pull request #111 from openvar/develop_v3
Browse files Browse the repository at this point in the history
Added check for connection within cursor decorator
  • Loading branch information
Peter Causey-Freeman authored Nov 21, 2019
2 parents b937c5d + 1493b58 commit ec89acd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions VariantValidator/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import re
import copy
import mysql.connector

logger = logging.getLogger(__name__)

Expand All @@ -15,6 +16,12 @@ def handleCursor(func):
"""
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
try:
self.conn.ping(reconnect=True, attempts=3, delay=5)
except mysql.connector.Error:
logger.warning("MySQL connection lost. Reconnecting.")
self.init_db()

self.cursor = self.conn.cursor(buffered=True)
out = func(self, *args, **kwargs)
if self.cursor:
Expand Down
8 changes: 6 additions & 2 deletions VariantValidator/modules/vvDBInit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Mixin:
"""
def __init__(self, db_config):
self.conn = None
self.pool = None
# self.cursor will be none UNLESS you're wrapping a function in @handleCursor, which automatically opens and
# closes connections for you.
self.cursor = None
self.dbConfig = db_config

self.pool = mysql.connector.pooling.MySQLConnectionPool(pool_size=10, connect_timeout=1209600, **self.dbConfig)
self.conn = self.pool.get_connection()
self.init_db()

def __del__(self):
if self.conn.is_connected():
Expand All @@ -26,6 +26,10 @@ def __del__(self):
if self.pool:
self.pool = None

def init_db(self):
self.pool = mysql.connector.pooling.MySQLConnectionPool(pool_size=10, connect_timeout=1209600, **self.dbConfig)
self.conn = self.pool.get_connection()

# <LICENSE>
# Copyright (C) 2019 VariantValidator Contributors
#
Expand Down

0 comments on commit ec89acd

Please sign in to comment.