private WebServiceDataServer GetDatabase(Boolean refresh)
 {
     if (_database.IsNull() || refresh)
     {
         if (_database.IsNotNull())
         {
             _database.Dispose();
         }
         _database = new TaxonServer();
         _database.BeginTransaction();
     }
     return(_database);
 }
        private WebServiceDataServer GetDatabase(Boolean refresh = false)
        {
            if (_database.IsNull() || refresh)
            {
                if (_database.IsNotNull())
                {
                    _database.Dispose();
                }

                _database = new SpeciesObservationHarvestServer();
            }

            return(_database);
        }
Exemple #3
0
        /// <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);
            }
        }
Exemple #4
0
        /// <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();
        }
Exemple #5
0
        /// <summary>
        /// Implementation of the IDisposable interface.
        /// Releases all resources related to this request.
        /// </summary>
        public void Dispose()
        {
            Int32    index;
            String   method;
            TimeSpan duration;

            if (_isTracing)
            {
                if (_traceUsers.IsEmpty() ||
                    _traceUsers.ContainsKey(ClientToken.UserName))
                {
                    duration = DateTime.Now - _traceStart;
                    method   = Environment.StackTrace;
                    index    = method.IndexOf(":line"); // English.
                    if (index < 0)
                    {
                        index = method.IndexOf(":rad"); // Swedish.
                    }

                    if (0 <= index)
                    {
                        method = method.Substring(index);
                        method = method.Substring(method.IndexOf("."));
                        method = method.Substring(1, method.IndexOf("(") - 1) + "()";
                    }
                    WebServiceData.LogManager.Log(this, method + ", duration = " + duration.Milliseconds + " milliseconds.", LogType.Trace, null);
                }
            }

            if (_database.IsNotNull())
            {
                try
                {
                    _database.Disconnect();
                }
                catch
                {
                }
                _database = null;
            }
        }
Exemple #6
0
        /// <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();
                }
            }
        }