/// <summary> /// Can return NULL (if DbIsNotOperatable) /// </summary> /// <param name="tableName"></param> /// <param name="transactionThreadId"></param> /// <returns></returns> public LTrie GetTable_WRITE(string tableName, int transactionThreadId) { if (!this._engine.DBisOperable) { return(null); } TransactionUnit transactionUnit = this.GetTransactionUnit(transactionThreadId); if (transactionUnit != null) { if (Environment.CurrentManagedThreadId != transactionThreadId) { this.UnregisterTransaction(transactionThreadId); throw DBriizeException.Throw(DBriizeException.eDBriizeExceptions.TRANSACTION_CANBEUSED_FROM_ONE_THREAD); } //We must put Get_Table_Write through the same bottleneck as RegisterWriteTablesForTransaction this.RegisterWriteTablesForTransaction(transactionThreadId, new List <string> { tableName }, false); //it will wait here till table for writing, reserved by other thread is released LTrie tbl = null; try { tbl = this._engine.DBriizeSchema.GetTable(tableName); //Adding table to transaction unit with the ITransactable interface transactionUnit.AddTransactionWriteTable(tableName, tbl); //added together with ITransactable //TODO - THIS TABLE LTrie must be Interfaced //Telling to the table that transactionThreadId Thread will modify it tbl.ModificationThreadId(transactionThreadId); } catch (Exception ex) { //Exception must come from Schema, by in-ability to get the table this.UnregisterTransaction(transactionThreadId); //CIRCULAR PARTLY throw ex; } return(tbl); } else { throw DBriizeException.Throw(DBriizeException.eDBriizeExceptions.TRANSACTION_DOESNT_EXIST); } }
/// <summary> /// Access synchronizer. /// All calls of the WRITE LOCATOR come over this function. /// </summary> /// <param name="transactionThreadId"></param> /// <param name="tablesNames"></param> public void RegisterWriteTablesForTransaction(int transactionThreadId, List <string> tablesNames, bool calledBySynchronizer) { //in every transaction unit we got a list of reserved for WRITING tables //if we have in tablesNames one of the tables which is in this list we have to stop the thread with mre bool toWaitTillTransactionIsFinished = false; bool breakOuterLoop = false; TransactionUnit transactionUnit = null; bool deadlock = false; while (true) //loop till thread will get full access to write tables { toWaitTillTransactionIsFinished = false; breakOuterLoop = false; deadlock = false; //only tables required for writing or read-commited will have to go over this fast bottleneck lock (_sync_dl) { _sync_transactions.EnterReadLock(); try { this._transactions.TryGetValue(transactionThreadId, out transactionUnit); if (transactionUnit == null) { return; //transaction doesn't exist anymore, gracefully goes out } if (!calledBySynchronizer) { //Here we are in case if Registrator is called by WriteTableCall, so we check intersections //Between reserved Write tables and current table using patterns intersections technique. //If they intersect we let the thread to proceed if (DbUserTables.TableNamesIntersect(transactionUnit.GetTransactionWriteTablesNames(), tablesNames)) { return; } } //iterating over all open transactions except self, finding out if desired tables are locked by other threads. foreach (var tu in this._transactions.Where(r => r.Value.TransactionThreadId != transactionThreadId)) { foreach (string tableName in tu.Value.GetTransactionWriteTablesNames()) { //if (tablesNames.Contains(tableName)) if (DbUserTables.TableNamesContains(tablesNames, tableName)) { // //++++++++++++++ here we can register all tables which are waiting for write lock release transactionUnit.AddTransactionWriteTablesAwaitingReservation(tablesNames); //++++++++++++++ if thread, who has locked this table has another table in a "waiting for reservation" blocked by this thread - it's a deadlock //if (transactionUnit.GetTransactionWriteTablesNames().Intersect(tu.Value.GetTransactionWriteTablesAwaitingReservation()).Count() > 0) if (DbUserTables.TableNamesIntersect(transactionUnit.GetTransactionWriteTablesNames(), tu.Value.GetTransactionWriteTablesAwaitingReservation())) { //we got deadlock, we will stop this transaction with an exception deadlock = true; } //other thread has reserved table for the transaction we have to wait toWaitTillTransactionIsFinished = true; breakOuterLoop = true; if (!deadlock) { ThreadsGator.CloseGate(); //closing gate only if no deadlock situation //mreWriteTransactionLock.Reset(); //setting to signalled only in non-deadlock case } break; } } if (breakOuterLoop) { break; } } } catch (System.Exception ex) { this.UnregisterTransaction(transactionThreadId); throw DBriizeException.Throw(DBriizeException.eDBriizeExceptions.TRANSACTION_TABLE_WRITE_REGISTRATION_FAILED, ex); } finally { _sync_transactions.ExitReadLock(); } //if(true) this thread owns all table for modification lock if (!toWaitTillTransactionIsFinished) { //+++++++++++ Here we can clear all table names in the waiting reservation queue transactionUnit.ClearTransactionWriteTablesAwaitingReservation(tablesNames); //we have to reserve for our transaction all tables foreach (var tbn in tablesNames) { transactionUnit.AddTransactionWriteTable(tbn, null); } return; } }//end of lock if (deadlock) { this.UnregisterTransaction(transactionThreadId); throw DBriizeException.Throw(DBriizeException.eDBriizeExceptions.TRANSACTION_IN_DEADLOCK); } if (toWaitTillTransactionIsFinished) { //blocking thread which requires busy tables for writing, till they are released //ThreadsGator.PutGateHere(20000); //every 20 second (or by Gate open we give a chance to re-try, for safety reasons of hanged threads, if programmer didn't dispose DBriize process after the programm end) ThreadsGator.PutGateHere(); //mreWriteTransactionLock.WaitOne(); } } }