/// <summary> /// Commit the transaction. Returns an integer representing the number of rows affected by the commit. /// </summary> public int Commit() { int result = 0; //Based on the transaction action switch (Request) { case Action.Read: { //Release the write access for all rows if the transaction is repeatable read or serialised read if (Type == TransactionType.RepeatableRead || Type == TransactionType.SerialisedRead) { ReleaseRowsWriteAccess(WorkingRows); } break; } case Action.Update: { //Pass the transaction rows to the TableManager for update and release the write access for all rows result += Manager.UpdateRows(WorkingRows); ReleaseRowsWriteAccess(WorkingRows); break; } case Action.Delete: { //Pass the transaction rows to the TableManager for delete and release the write access for all rows result += Manager.DeleteRows(WorkingRows); ReleaseRowsWriteAccess(WorkingRows); break; } case Action.Create: { result += WorkingRows.Count; //Pass the transaction rows to the TableManager for create and release the write access for all rows Manager.WriteRows(WorkingRows); ReleaseRowsWriteAccess(WorkingRows); break; } } //If the transaction is a serialised read, release the table's create access if (Type == TransactionType.SerialisedRead) { Manager.Table.ReleasedSerialisedAccess(); } //Clear the working rows and return the number affected WorkingRows.Clear(); return(result); }
/// <summary> /// Rollback the transaction. Returns an integer representing the number of rows affected by the rollback. /// </summary> public int RollBack() { int result = 0; foreach (Row row in WorkingRows) { //If the row is marked as deleted, return it to stable and release it's writer if (row.Action == Row.RowAction.Delete) { row.Action = Row.RowAction.Stable; result++; row.Lock.ReleaseWriter(); } //If the row is marked as update if (row.Action == Row.RowAction.Update) { //Revert it to stable row.Action = Row.RowAction.Stable; //Revert the data that reflects the update back to the state that it was before being flagged for update row.NewTextData = row.TextData; row.NewNumberData = row.NumberData; result++; //Release the writer row.Lock.ReleaseWriter(); } if (row.Action == Row.RowAction.Proposed) { //If it was proposed then remove the row from the table Manager.DeleteRowFromTable(row); result++; } } //If the transaction is a serialised read, release the table's create access if (Type == TransactionType.SerialisedRead) { Manager.Table.ReleasedSerialisedAccess(); } //Clear the working rows and return the number affected WorkingRows.Clear(); return(result); }