/// <summary> /// Rollback a transaction. /// </summary> public void RollbackTransaction() { WebServiceDataServer transactionDatabase = null; lock (_transactionInformation) { // Undo transaction. if (_transactionInformation.ContainsKey(TransactionKey)) { transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]); _transactionInformation.Remove(TransactionKey); } if (_transactionInformation.ContainsKey(TransactionIdKey)) { _transactionInformation.Remove(TransactionIdKey); } } if (transactionDatabase.IsNotNull()) { lock (transactionDatabase) { transactionDatabase.RollbackTransaction(); transactionDatabase.Disconnect(); } // Cache is cleared since data changes that // is rollbacked could have been cached. ClearCache(false); } }
public void TestCleanup() { if (_database.IsNotNull()) { _database.RollbackTransaction(); _database.Dispose(); _database = null; } }
/// <summary> /// Start a transaction. /// </summary> /// <param name="timeout"> /// Time before transaction timeouts if has not already finished. /// Unit is seconds. /// </param> public void StartTransaction(Int32 timeout) { Boolean abortTransaction = false; WebServiceDataServer transactionDatabase = null; Thread transactionThread; _transactionTimeout = timeout; lock (_transactionInformation) { if (IsInTransaction()) { // StartTransaction should only be called once // to start the transaction. abortTransaction = true; // Undo transaction. if (_transactionInformation.ContainsKey(TransactionKey)) { transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]); _transactionInformation.Remove(TransactionKey); } if (_transactionInformation.ContainsKey(TransactionIdKey)) { _transactionInformation.Remove(TransactionIdKey); } } else { // Start transaction. transactionDatabase = WebServiceData.DatabaseManager.GetDatabase(this); transactionDatabase.CommandTimeout = _transactionTimeout; transactionDatabase.BeginTransaction(); _transactionInformation.Add(TransactionKey, transactionDatabase); _transactionInformation.Add(TransactionIdKey, RequestId); } } if (abortTransaction) { if (transactionDatabase.IsNotNull()) { lock (transactionDatabase) { transactionDatabase.RollbackTransaction(); transactionDatabase.Disconnect(); } } throw new ApplicationException("Transaction has already been started!"); } // Start thread that control time out of transaction. transactionThread = new Thread(CheckTransactionTimeOut); transactionThread.Priority = ThreadPriority.Highest; transactionThread.Start(); }
/// <summary> /// Check if transaction has timed out and should be rollbacked. /// </summary> private void CheckTransactionTimeOut() { WebServiceDataServer transactionDatabase = null; Int32 transactionId; Int32 transactionTimeout; // Get transaction information. transactionTimeout = _transactionTimeout; if (transactionTimeout > Settings.Default.TransactionMaxTimeout) { // Unit is seconds. transactionTimeout = Settings.Default.TransactionMaxTimeout; } lock (_transactionInformation) { if (_transactionInformation.ContainsKey(TransactionIdKey)) { transactionId = (Int32)(_transactionInformation[TransactionIdKey]); } else { // Transaction has already been commited or aborted. return; } } // Wait for timeout. Thread.Sleep(transactionTimeout * 1000); // Check if we have a transaction to abort. lock (_transactionInformation) { if (_transactionInformation.ContainsKey(TransactionIdKey)) { if (transactionId == ((Int32)(_transactionInformation[TransactionIdKey]))) { if (IsInTransaction()) { // Undo transaction. if (_transactionInformation.ContainsKey(TransactionKey)) { transactionDatabase = (WebServiceDataServer)(_transactionInformation[TransactionKey]); _transactionInformation.Remove(TransactionKey); } if (_transactionInformation.ContainsKey(TransactionIdKey)) { _transactionInformation.Remove(TransactionIdKey); } } else { _transactionInformation.Remove(TransactionIdKey); } } // else: A new transaction has started. } // else: Transaction id has been removed by another // transaction call from the same client. } if (transactionDatabase.IsNotNull()) { lock (transactionDatabase) { transactionDatabase.RollbackTransaction(); transactionDatabase.Disconnect(); } } }