diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 5fe97be5..b739f159 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -273,6 +273,7 @@ class Database: self.db_path = '' gen = c.get_general_params() self.day_start = 0 + self._has_lock = False if 'day_start' in gen: self.day_start = float(gen['day_start']) @@ -1719,6 +1720,34 @@ class Database: # however the calling prog requires. Main aims: # - existing static routines from fpdb_simple just modified + def setThreadId(self, threadid): + self.threadId = threadid + + def acquireLock(self, wait=True, retry_time=.01): + while not self._has_lock: + cursor = self.get_cursor() + cursor.execute(self.sql.query['selectLock']) + record = cursor.fetchall() + self.commit() + if not len(record): + cursor.execute(self.sql.query['switchLock'], (True, self.threadId)) + self.commit() + self._has_lock = True + return True + else: + cursor.execute(self.sql.query['missedLock'], (1, self.threadId)) + self.commit() + if not wait: + return False + sleep(retry_time) + + def releaseLock(self): + if self._has_lock: + cursor = self.get_cursor() + num = cursor.execute(self.sql.query['switchLock'], (False, self.threadId)) + self.commit() + self._has_lock = False + def lock_for_insert(self): """Lock tables in MySQL to try to speed inserts up""" try: diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index f7f84a98..ec3c03df 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -107,6 +107,15 @@ class Sql: elif db_server == 'sqlite': self.query['createSettingsTable'] = """CREATE TABLE Settings (version INTEGER NOT NULL) """ + + ################################ + # Create InsertLock + ################################ + if db_server == 'mysql': + self.query['createLockTable'] = """CREATE TABLE InsertLock ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + locked BOOLEAN NOT NULL DEFAULT FALSE) + ENGINE=INNODB""" ################################ # Create RawHands (this table is all but identical with RawTourneys) @@ -4501,6 +4510,25 @@ class Sql: self.query['analyze'] = "analyze" elif db_server == 'sqlite': self.query['analyze'] = "analyze" + + if db_server == 'mysql': + self.query['selectLock'] = """ + SELECT locked + FROM InsertLock + WHERE locked=True + LOCK IN SHARE MODE""" + + if db_server == 'mysql': + self.query['switchLock'] = """ + UPDATE InsertLock SET + locked=%s + WHERE id=%s""" + + if db_server == 'mysql': + self.query['missedLock'] = """ + UPDATE InsertLock SET + missed=missed+%s + WHERE id=%s""" if db_server == 'mysql': self.query['lockForInsert'] = """