Skip to content

Commit

Permalink
Try to re-establish the connection if it's gone
Browse files Browse the repository at this point in the history
Should fix #10

Signed-off-by: Marek Chalupa <xchalup4@fi.muni.cz>
  • Loading branch information
mchalupa committed Jun 12, 2018
1 parent 65cd029 commit 40f2438
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion brv/database/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def next(self):

class DatabaseConnection(object):
def __init__(self, conffile = None):
self._conn, self._cursor = _database_connect(conffile)
self._conffile = conffile
self._connect()

def __del__(self):
# if creating the database failed, then the attribute _conn
Expand All @@ -67,8 +68,18 @@ def __del__(self):
self._conn.close()
del self

def _connect(self):
self._conn, self._cursor = _database_connect(self._conffile)

def _check_conn(self):
# try to re-establish connection if it is down.
# NOTE: this will do rollback
if self._conn.open == 0:
self._conn.ping(True)

def _execute(self, q):
#dbg(q)
self._check_conn()
self._cursor.execute(q)

def query_unchecked(self, q):
Expand Down

0 comments on commit 40f2438

Please sign in to comment.