From 5b96769de3ca7197394befbc8c63daad3fc76eb2 Mon Sep 17 00:00:00 2001 From: sqlcoder Date: Sun, 31 May 2009 21:59:01 +0100 Subject: [PATCH] implement global lock by locking hands table --- pyfpdb/fpdb.py | 58 +++++++++++++++++++++++++------------------ pyfpdb/fpdb_simple.py | 21 ++++++++++++++++ 2 files changed, 55 insertions(+), 24 deletions(-) diff --git a/pyfpdb/fpdb.py b/pyfpdb/fpdb.py index 379b4e44..8a26462d 100755 --- a/pyfpdb/fpdb.py +++ b/pyfpdb/fpdb.py @@ -174,35 +174,36 @@ class fpdb: def dia_load_profile(self, widget, data=None): """Dialogue to select a file to load a profile from""" - self.obtain_global_lock() - chooser = gtk.FileChooserDialog(title="Please select a profile file to load", - action=gtk.FILE_CHOOSER_ACTION_OPEN, - buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) - chooser.set_filename(self.profile) + if self.obtain_global_lock(): + chooser = gtk.FileChooserDialog(title="Please select a profile file to load", + action=gtk.FILE_CHOOSER_ACTION_OPEN, + buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) + chooser.set_filename(self.profile) - response = chooser.run() - chooser.destroy() - if response == gtk.RESPONSE_OK: - self.load_profile(chooser.get_filename()) - elif response == gtk.RESPONSE_CANCEL: - print 'User cancelled loading profile' + response = chooser.run() + chooser.destroy() + if response == gtk.RESPONSE_OK: + self.load_profile(chooser.get_filename()) + elif response == gtk.RESPONSE_CANCEL: + print 'User cancelled loading profile' #end def dia_load_profile def dia_recreate_tables(self, widget, data=None): """Dialogue that asks user to confirm that he wants to delete and recreate the tables""" - self.obtain_global_lock() + if self.obtain_global_lock(): - dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING, - buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm deleting and recreating tables") - diastring = "Please confirm that you want to (re-)create the tables. If there already are tables in the database "+self.db.database+" on "+self.db.host+" they will be deleted." - dia_confirm.format_secondary_text(diastring)#todo: make above string with bold for db, host and deleted + dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING, + buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm deleting and recreating tables") + diastring = "Please confirm that you want to (re-)create the tables. If there already are tables in the database "+self.db.database+" on "+self.db.host+" they will be deleted." + dia_confirm.format_secondary_text(diastring)#todo: make above string with bold for db, host and deleted - response = dia_confirm.run() - dia_confirm.destroy() - if response == gtk.RESPONSE_YES: - self.db.recreate_tables() - elif response == gtk.RESPONSE_NO: - print 'User cancelled recreating tables' + response = dia_confirm.run() + dia_confirm.destroy() + if response == gtk.RESPONSE_YES: + self.db.recreate_tables() + elif response == gtk.RESPONSE_NO: + print 'User cancelled recreating tables' + self.release_global_lock() #end def dia_recreate_tables def dia_regression_test(self, widget, data=None): @@ -371,7 +372,14 @@ class fpdb: #end def not_implemented def obtain_global_lock(self): - print "todo: implement obtain_global_lock (users: pls ignore this)" + print "\nTaking global lock ..." + self.fdb_lock = fpdb_db.fpdb_db() + self.fdb_lock.connect(self.settings['db-backend'], + self.settings['db-host'], + self.settings['db-databaseName'], + self.settings['db-user'], + self.settings['db-password']) + return fpdb_simple.get_global_lock(self.fdb_lock) #end def obtain_global_lock def quit(self, widget): @@ -382,7 +390,9 @@ class fpdb: #end def quit_cliecked def release_global_lock(self): - print "todo: implement release_global_lock" + self.fdb_lock.db.rollback() + self.fdb_lock.disconnect() + print "Global lock released." #end def release_global_lock def tab_abbreviations(self, widget, data=None): diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index cecaf09b..c6f79d9d 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -367,6 +367,27 @@ def analyzeDB(fdb): fdb.db.commit() #end def analyzeDB +def get_global_lock(fdb): + if fdb.backend == MYSQL_INNODB: + try: + fdb.cursor.execute( "lock tables Hands write" ) + except: + print "Error! failed to obtain global lock. Close all programs accessing " \ + + "database (including fpdb) and try again (%s)." \ + % ( str(sys.exc_value).rstrip('\n'), ) + return(False) + elif fdb.backend == PGSQL: + try: + fdb.cursor.execute( "lock table Hands in exclusive mode nowait" ) + #print "... after lock table, status =", fdb.cursor.statusmessage + except: + print "Error! failed to obtain global lock. Close all programs accessing " \ + + "database (including fpdb) and try again (%s)." \ + % ( str(sys.exc_value).rstrip('\n'), ) + return(False) + return(True) + + class DuplicateError(Exception): def __init__(self, value): self.value = value