Exemple #1
0
        /// <summary>
        /// Writes all data points from the queue.
        /// </summary>
        public void FlushPoints()
        {
            if (Connection == null)
            {
                throw new InvalidOperationException("Connection must not be null.");
            }

            if (dataQueue.Count == 0)
            {
                return;
            }

            NpgsqlTransaction trans = null;

            try
            {
                Connection.Open();
                trans = Connection.BeginTransaction();
                command.Connection  = Connection;
                command.Transaction = trans;

                lock (SyncRoot)
                {
                    while (dataQueue.Count > 0)
                    {
                        SetCommandParams(dataQueue.Dequeue());
                        command.ExecuteNonQuery();
                    }
                }

                ArcLog?.WriteInfo(ServerPhrases.QueueBecameEmpty);
                trans.Commit();
                HasError = false;
            }
            catch (Exception ex)
            {
                DbUtils.SafeRollback(trans);
                HasError = true;
                AppLog?.WriteError(ex, ServerPhrases.ArchiveMessage, ArchiveCode, ServerPhrases.WriteDbError);
                ArcLog?.WriteError(ex, ServerPhrases.WriteDbError);
            }
            finally
            {
                Connection.Close();
            }
        }
Exemple #2
0
        /// <summary>
        /// Inserts or updates data points from the queue.
        /// </summary>
        public void InsertPoints()
        {
            if (Connection == null)
            {
                throw new InvalidOperationException("Connection must not be null.");
            }

            NpgsqlTransaction trans = null;

            try
            {
                Connection.Open();
                trans = Connection.BeginTransaction();
                command.Connection  = Connection;
                command.Transaction = trans;

                for (int i = 0; i < DbUtils.BundleSize; i++)
                {
                    // retrieve a data point from the queue
                    CnlDataPoint point;

                    lock (SyncRoot)
                    {
                        if (dataQueue.Count > 0)
                        {
                            point = dataQueue.Dequeue();
                        }
                        else
                        {
                            break;
                        }
                    }

                    try
                    {
                        // write the data point
                        SetCommandParams(point);
                        command.ExecuteNonQuery();
                    }
                    catch
                    {
                        // return the unwritten data point to the queue
                        if (ReturnOnError)
                        {
                            lock (SyncRoot)
                            {
                                dataQueue.Enqueue(point);
                            }
                        }

                        throw;
                    }
                }

                if (dataQueue.Count == 0)
                {
                    ArcLog?.WriteInfo(ServerPhrases.QueueBecameEmpty);
                }

                trans.Commit();
                HasError = false;
            }
            catch (Exception ex)
            {
                DbUtils.SafeRollback(trans);
                HasError = true;
                AppLog?.WriteError(ex, ServerPhrases.ArchiveMessage, ArchiveCode, ServerPhrases.WriteDbError);
                ArcLog?.WriteError(ex, ServerPhrases.WriteDbError);
                Thread.Sleep(DbUtils.ErrorDelay);
            }
            finally
            {
                Connection.Close();
            }
        }