Skip to content

Commit

Permalink
attempt at fixing crashes when db connection drops (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
wo committed Sep 25, 2016
1 parent 5985cdf commit a5fc78b
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions opp/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,23 @@
import logging

_conn = None
_cur = None
_dictcur = None

def connection(host=config['mysql']['host'],
db=config['mysql']['db'],
user=config['mysql']['user'],
passwd=config['mysql']['pass']):
def connect(host=config['mysql']['host'],
db=config['mysql']['db'],
user=config['mysql']['user'],
passwd=config['mysql']['pass']):
global _conn
if not _conn or not _conn.open:
_conn = MySQLdb.connect(host=host, db=db, user=user, passwd=passwd,
use_unicode=True, charset='UTF8')
return _conn

def cursor():
global _cur
if not _cur:
_cur = connection().cursor()
return _cur
# not cached so we can reconnect if db connection is gone
return connect().cursor()

def dict_cursor():
global _dictcur
if not _dictcur:
_dictcur = connection().cursor(MySQLdb.cursors.DictCursor)
return _dictcur
return connect().cursor(MySQLdb.cursors.DictCursor)

def commit():
global _conn
Expand Down

0 comments on commit a5fc78b

Please sign in to comment.