Skip to content

Instantly share code, notes, and snippets.

@marcsello
Created September 7, 2018 15:03
Show Gist options
  • Save marcsello/217ea1f5581fde3ddbbdb8f04c8f0506 to your computer and use it in GitHub Desktop.
Save marcsello/217ea1f5581fde3ddbbdb8f04c8f0506 to your computer and use it in GitHub Desktop.
Auto (re)connecting DB connection thingy. For stuff that should be always connected to a db.
#!/usr/bin/env python3
import MySQLdb
import _mysql_exceptions
import time
class AutoconnectDB():
def __init__(self,user,password,db,host=None,unix_socket=None):
self.__user = user
self.__password = password
self.__db = db
def __connect(self):
self.__conn = MySQLdb.connect(user=self.__user, passwd=self.__password, db=self.__db, unix_socket="/var/lib/mysql/mysql.sock", connect_timeout=1)
self.__conn.autocommit(True)
def doQuery(self,query,input=None,max_retry=5): # tries to do query, auto-reconnect on failure
tries = 0
cursor = None
while tries < max_retry:
try:
cursor = self.__conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query,input)
break
except (MySQLdb.OperationalError,AttributeError): # db.OpErr = Last connection was cool and good, but the server disconnected since, Attr = self.__conn is None (not connected yet)
try:
self.__connect()
except _mysql_exceptions.OperationalError: # connection failed
pass
if tries > 0: # not the first connection attempt
time.sleep(0.5)
tries += 1
return cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment