improve handling of missing / unavailable db errors

This commit is contained in:
sqlcoder 2010-08-12 22:16:27 +01:00
parent d6c7ccaeff
commit be7705226d
2 changed files with 25 additions and 15 deletions

View File

@ -265,7 +265,7 @@ class Database:
#ISOLATION_LEVEL_SERIALIZABLE = 2 #ISOLATION_LEVEL_SERIALIZABLE = 2
if self.backend == self.SQLITE and self.database == ':memory:' and self.wrongDbVersion: if self.backend == self.SQLITE and self.database == ':memory:' and self.wrongDbVersion and self.is_connected():
log.info("sqlite/:memory: - creating") log.info("sqlite/:memory: - creating")
self.recreate_tables() self.recreate_tables()
self.wrongDbVersion = False self.wrongDbVersion = False
@ -289,6 +289,7 @@ class Database:
self.saveActions = False if self.import_options['saveActions'] == False else True self.saveActions = False if self.import_options['saveActions'] == False else True
if self.is_connected():
self.connection.rollback() # make sure any locks taken so far are released self.connection.rollback() # make sure any locks taken so far are released
#end def __init__ #end def __init__
@ -342,7 +343,6 @@ class Database:
self.db_server = db_params['db-server'] self.db_server = db_params['db-server']
self.database = db_params['db-databaseName'] self.database = db_params['db-databaseName']
self.host = db_params['db-host'] self.host = db_params['db-host']
self.__connected = True
def connect(self, backend=None, host=None, database=None, def connect(self, backend=None, host=None, database=None,
user=None, password=None, create=False): user=None, password=None, create=False):
@ -363,6 +363,7 @@ class Database:
MySQLdb = pool.manage(MySQLdb, pool_size=5) MySQLdb = pool.manage(MySQLdb, pool_size=5)
try: try:
self.connection = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True) self.connection = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True)
self.__connected = True
#TODO: Add port option #TODO: Add port option
except MySQLdb.Error, ex: except MySQLdb.Error, ex:
if ex.args[0] == 1045: if ex.args[0] == 1045:
@ -384,20 +385,21 @@ class Database:
# flat out wrong # flat out wrong
# sqlcoder: This database only connect failed in my windows setup?? # sqlcoder: This database only connect failed in my windows setup??
# Modifed it to try the 4 parameter style if the first connect fails - does this work everywhere? # Modifed it to try the 4 parameter style if the first connect fails - does this work everywhere?
connected = False self.__connected = False
if self.host == "localhost" or self.host == "127.0.0.1": if self.host == "localhost" or self.host == "127.0.0.1":
try: try:
self.connection = psycopg2.connect(database = database) self.connection = psycopg2.connect(database = database)
connected = True self.__connected = True
except: except:
# direct connection failed so try user/pass/... version # direct connection failed so try user/pass/... version
pass pass
if not connected: if not self.is_connected():
try: try:
self.connection = psycopg2.connect(host = host, self.connection = psycopg2.connect(host = host,
user = user, user = user,
password = password, password = password,
database = database) database = database)
self.__connected = True
except Exception, ex: except Exception, ex:
if 'Connection refused' in ex.args[0]: if 'Connection refused' in ex.args[0]:
# meaning eg. db not running # meaning eg. db not running
@ -426,6 +428,7 @@ class Database:
log.info("Connecting to SQLite: %(database)s" % {'database':self.db_path}) log.info("Connecting to SQLite: %(database)s" % {'database':self.db_path})
if os.path.exists(database) or create: if os.path.exists(database) or create:
self.connection = sqlite3.connect(self.db_path, detect_types=sqlite3.PARSE_DECLTYPES ) self.connection = sqlite3.connect(self.db_path, detect_types=sqlite3.PARSE_DECLTYPES )
self.__connected = True
sqlite3.register_converter("bool", lambda x: bool(int(x))) sqlite3.register_converter("bool", lambda x: bool(int(x)))
sqlite3.register_adapter(bool, lambda x: "1" if x else "0") sqlite3.register_adapter(bool, lambda x: "1" if x else "0")
self.connection.create_function("floor", 1, math.floor) self.connection.create_function("floor", 1, math.floor)
@ -443,6 +446,7 @@ class Database:
else: else:
raise FpdbError("unrecognised database backend:"+str(backend)) raise FpdbError("unrecognised database backend:"+str(backend))
if self.is_connected():
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
self.cursor.execute(self.sql.query['set tx level']) self.cursor.execute(self.sql.query['set tx level'])
self.check_version(database=database, create=create) self.check_version(database=database, create=create)
@ -499,6 +503,10 @@ class Database:
self.connection.rollback() self.connection.rollback()
def connected(self): def connected(self):
""" now deprecated, use is_connected() instead """
return self.__connected
def is_connected(self):
return self.__connected return self.__connected
def get_cursor(self): def get_cursor(self):

View File

@ -105,7 +105,7 @@ except:
import GuiPrefs import GuiPrefs
import GuiLogView import GuiLogView
#import GuiDatabase import GuiDatabase
import GuiBulkImport import GuiBulkImport
import GuiImapFetcher import GuiImapFetcher
import GuiRingPlayerStats import GuiRingPlayerStats
@ -838,7 +838,7 @@ class fpdb:
('hudConfigurator', None, '_HUD Configurator', '<control>H', 'HUD Configurator', self.diaHudConfigurator), ('hudConfigurator', None, '_HUD Configurator', '<control>H', 'HUD Configurator', self.diaHudConfigurator),
('graphs', None, '_Graphs', '<control>G', 'Graphs', self.tabGraphViewer), ('graphs', None, '_Graphs', '<control>G', 'Graphs', self.tabGraphViewer),
('ringplayerstats', None, 'Ring _Player Stats (tabulated view)', '<control>P', 'Ring Player Stats (tabulated view)', self.tab_ring_player_stats), ('ringplayerstats', None, 'Ring _Player Stats (tabulated view)', '<control>P', 'Ring Player Stats (tabulated view)', self.tab_ring_player_stats),
('tourneyplayerstats', None, '_Tourney Player Stats (tabulated view, mysql only)', '<control>T', 'Tourney Player Stats (tabulated view, mysql only)', self.tab_tourney_player_stats), ('tourneyplayerstats', None, '_Tourney Player Stats (tabulated view)', '<control>T', 'Tourney Player Stats (tabulated view, mysql only)', self.tab_tourney_player_stats),
('tourneyviewer', None, 'Tourney _Viewer', None, 'Tourney Viewer)', self.tab_tourney_viewer_stats), ('tourneyviewer', None, 'Tourney _Viewer', None, 'Tourney Viewer)', self.tab_tourney_viewer_stats),
('posnstats', None, 'P_ositional Stats (tabulated view, not on sqlite)', '<control>O', 'Positional Stats (tabulated view)', self.tab_positional_stats), ('posnstats', None, 'P_ositional Stats (tabulated view, not on sqlite)', '<control>O', 'Positional Stats (tabulated view)', self.tab_positional_stats),
('sessionstats', None, 'Session Stats', None, 'Session Stats', self.tab_session_stats), ('sessionstats', None, 'Session Stats', None, 'Session Stats', self.tab_session_stats),
@ -894,7 +894,7 @@ class fpdb:
self.settings.update(self.config.get_import_parameters()) self.settings.update(self.config.get_import_parameters())
self.settings.update(self.config.get_default_paths()) self.settings.update(self.config.get_default_paths())
if self.db is not None and self.db.connected: if self.db is not None and self.db.is_connected():
self.db.disconnect() self.db.disconnect()
self.sql = SQL.Sql(db_server = self.settings['db-server']) self.sql = SQL.Sql(db_server = self.settings['db-server'])
@ -917,6 +917,8 @@ class fpdb:
if err_msg is not None: if err_msg is not None:
self.db = None self.db = None
self.warning_box(err_msg) self.warning_box(err_msg)
if self.db is not None and not self.db.is_connected():
self.db = None
# except FpdbMySQLFailedError: # except FpdbMySQLFailedError:
# self.warning_box("Unable to connect to MySQL! Is the MySQL server running?!", "FPDB ERROR") # self.warning_box("Unable to connect to MySQL! Is the MySQL server running?!", "FPDB ERROR")
@ -957,7 +959,7 @@ class fpdb:
self.main_vbox.pack_end(self.status_bar, False, True, 0) self.main_vbox.pack_end(self.status_bar, False, True, 0)
self.status_bar.show() self.status_bar.show()
if self.db is not None and self.db.connected: if self.db is not None and self.db.is_connected():
self.status_bar.set_text("Status: Connected to %s database named %s on host %s" self.status_bar.set_text("Status: Connected to %s database named %s on host %s"
% (self.db.get_backend_name(),self.db.database, self.db.host)) % (self.db.get_backend_name(),self.db.database, self.db.host))
# rollback to make sure any locks are cleared: # rollback to make sure any locks are cleared:
@ -991,12 +993,12 @@ class fpdb:
if self.db!=None: if self.db!=None:
if self.db.backend==self.db.MYSQL_INNODB: if self.db.backend==self.db.MYSQL_INNODB:
try: try:
if self.db is not None and self.db.connected(): if self.db is not None and self.db.is_connected():
self.db.disconnect() self.db.disconnect()
except _mysql_exceptions.OperationalError: # oh, damn, we're already disconnected except _mysql_exceptions.OperationalError: # oh, damn, we're already disconnected
pass pass
else: else:
if self.db is not None and self.db.connected(): if self.db is not None and self.db.is_connected():
self.db.disconnect() self.db.disconnect()
else: else:
pass pass