use python locks instead of db locks for global lock

This commit is contained in:
sqlcoder 2009-07-18 22:21:29 +01:00
parent 9d9f850753
commit 6a69bf512e

View File

@ -17,6 +17,7 @@
import os import os
import sys import sys
import threading
import Options import Options
import string import string
cl_options = string.join(sys.argv[1:]) cl_options = string.join(sys.argv[1:])
@ -116,11 +117,13 @@ class fpdb:
def dia_create_del_database(self, widget, data=None): def dia_create_del_database(self, widget, data=None):
print "todo: implement dia_create_del_database" print "todo: implement dia_create_del_database"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_create_del_database #end def dia_create_del_database
def dia_create_del_user(self, widget, data=None): def dia_create_del_user(self, widget, data=None):
print "todo: implement dia_create_del_user" print "todo: implement dia_create_del_user"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_create_del_user #end def dia_create_del_user
def dia_database_stats(self, widget, data=None): def dia_database_stats(self, widget, data=None):
@ -137,16 +140,19 @@ class fpdb:
def dia_delete_db_parts(self, widget, data=None): def dia_delete_db_parts(self, widget, data=None):
print "todo: implement dia_delete_db_parts" print "todo: implement dia_delete_db_parts"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_delete_db_parts #end def dia_delete_db_parts
def dia_edit_profile(self, widget=None, data=None, create_default=False, path=None): def dia_edit_profile(self, widget=None, data=None, create_default=False, path=None):
print "todo: implement dia_edit_profile" print "todo: implement dia_edit_profile"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_edit_profile #end def dia_edit_profile
def dia_export_db(self, widget, data=None): def dia_export_db(self, widget, data=None):
print "todo: implement dia_export_db" print "todo: implement dia_export_db"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_export_db #end def dia_export_db
def dia_get_db_root_credentials(self): def dia_get_db_root_credentials(self):
@ -172,6 +178,7 @@ class fpdb:
def dia_import_db(self, widget, data=None): def dia_import_db(self, widget, data=None):
print "todo: implement dia_import_db" print "todo: implement dia_import_db"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_import_db #end def dia_import_db
def dia_licensing(self, widget, data=None): def dia_licensing(self, widget, data=None):
@ -180,7 +187,7 @@ class fpdb:
def dia_load_profile(self, widget, data=None): def dia_load_profile(self, widget, data=None):
"""Dialogue to select a file to load a profile from""" """Dialogue to select a file to load a profile from"""
if self.obtain_global_lock() == 0: # returns 0 if successful if self.obtain_global_lock(): # returns true if successful
#try: #try:
# chooser = gtk.FileChooserDialog(title="Please select a profile file to load", # chooser = gtk.FileChooserDialog(title="Please select a profile file to load",
# action=gtk.FILE_CHOOSER_ACTION_OPEN, # action=gtk.FILE_CHOOSER_ACTION_OPEN,
@ -195,15 +202,18 @@ class fpdb:
# print 'User cancelled loading profile' # print 'User cancelled loading profile'
#except: #except:
# pass # pass
try:
self.load_profile() self.load_profile()
except:
pass
self.release_global_lock() self.release_global_lock()
#end def dia_load_profile #end def dia_load_profile
def dia_recreate_tables(self, widget, data=None): def dia_recreate_tables(self, widget, data=None):
"""Dialogue that asks user to confirm that he wants to delete and recreate the tables""" """Dialogue that asks user to confirm that he wants to delete and recreate the tables"""
if self.obtain_global_lock() in (0,2): # returns 0 if successful, 2 if Hands table does not exist if self.obtain_global_lock(): # returns true if successful
lock_released = False #lock_released = False
try: try:
dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING, dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING,
buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm deleting and recreating tables") buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm deleting and recreating tables")
@ -214,27 +224,28 @@ class fpdb:
response = dia_confirm.run() response = dia_confirm.run()
dia_confirm.destroy() dia_confirm.destroy()
if response == gtk.RESPONSE_YES: if response == gtk.RESPONSE_YES:
if self.db.fdb.backend == self.fdb_lock.fdb.MYSQL_INNODB: #if self.db.fdb.backend == self.fdb_lock.fdb.MYSQL_INNODB:
# mysql requires locks on all tables or none - easier to release this lock # mysql requires locks on all tables or none - easier to release this lock
# than lock all the other tables # than lock all the other tables
# ToDo: lock all other tables so that lock doesn't have to be released # ToDo: lock all other tables so that lock doesn't have to be released
self.release_global_lock() # self.release_global_lock()
lock_released = True # lock_released = True
self.db.fdb.recreate_tables() self.db.fdb.recreate_tables()
else: #else:
# for other dbs use same connection as holds global lock # for other dbs use same connection as holds global lock
self.fdb_lock.fdb.recreate_tables() # self.fdb_lock.fdb.recreate_tables()
elif response == gtk.RESPONSE_NO: elif response == gtk.RESPONSE_NO:
print 'User cancelled recreating tables' print 'User cancelled recreating tables'
except: except:
pass pass
if not lock_released: #if not lock_released:
self.release_global_lock() self.release_global_lock()
#end def dia_recreate_tables #end def dia_recreate_tables
def dia_regression_test(self, widget, data=None): def dia_regression_test(self, widget, data=None):
print "todo: implement dia_regression_test" print "todo: implement dia_regression_test"
self.obtain_global_lock() self.obtain_global_lock()
self.release_global_lock()
#end def dia_regression_test #end def dia_regression_test
def dia_save_profile(self, widget, data=None): def dia_save_profile(self, widget, data=None):
@ -353,6 +364,7 @@ class fpdb:
"""Loads profile from the provided path name.""" """Loads profile from the provided path name."""
self.config = Configuration.Config(file=options.config, dbname=options.dbname) self.config = Configuration.Config(file=options.config, dbname=options.dbname)
self.settings = {} self.settings = {}
self.settings['global_lock'] = self.lock
if (os.sep=="/"): if (os.sep=="/"):
self.settings['os']="linuxmac" self.settings['os']="linuxmac"
else: else:
@ -405,10 +417,15 @@ class fpdb:
#end def not_implemented #end def not_implemented
def obtain_global_lock(self): def obtain_global_lock(self):
print "\nTaking global lock ..." ret = self.lock.acquire(False) # will return false if lock is already held
self.fdb_lock = Database.Database(self.config, sql = self.sql) if ret:
self.fdb_lock.do_connect(self.config) print "\nGlobal lock taken ..."
return self.fdb_lock.fdb.get_global_lock() else:
print "\nFailed to get global lock."
return ret
# need to release it later:
# self.lock.release()
#end def obtain_global_lock #end def obtain_global_lock
def quit(self, widget, data=None): def quit(self, widget, data=None):
@ -419,8 +436,7 @@ class fpdb:
#end def quit_cliecked #end def quit_cliecked
def release_global_lock(self): def release_global_lock(self):
self.fdb_lock.fdb.db.rollback() self.lock.release()
self.fdb_lock.fdb.disconnect()
print "Global lock released.\n" print "Global lock released.\n"
#end def release_global_lock #end def release_global_lock
@ -487,6 +503,7 @@ This program is licensed under the AGPL3, see docs"""+os.sep+"agpl-3.0.txt")
def __init__(self): def __init__(self):
self.threads = [] self.threads = []
self.lock = threading.Lock()
self.db = None self.db = None
self.status_bar = None self.status_bar = None