NpgsqlOperationInProgressException - PullRequest
       21

NpgsqlOperationInProgressException

0 голосов
/ 02 апреля 2020

У меня есть консольное приложение C#, которое подключается к каналу валютных данных.

Каждый раз, когда обновляется цена, я получаю новую цену, которая затем обновляется или вставляется в запись базы данных. Эти записи передаются очень быстро, каждую секунду, если не быстрее.

Я получаю сообщение об ошибке 'Npg sql .NpgsqlOperationInProgressException:' Команда уже выполняется: '

void qc_OnQuote(object sender, QuoteEventArgs args)
    {
        Console.WriteLine(args.Symbol + " " + args.Time + " " + args.Bid);

        var dic = File.ReadAllLines("parameters.txt")
             .Select(l => l.Split(new[] { '=' }))
             .ToDictionary(s => s[0].Trim(), s => s[1].Trim());

        int minutes_offset = int.Parse(dic["MINUTES_OFFSET"]);
        float chart_point_adjuster = float.Parse(dic["CHART_POINT_ADJUSTER"]);

        if (conn.State == System.Data.ConnectionState.Closed)
        {
            Console.WriteLine("Lost DB connection - reconnecting");
            conn.Open();
        }

        double tick = args.Bid;

        tick += chart_point_adjuster;

        DateTime dateTime = args.Time;
        //Strip seconds from the server time
        dateTime = dateTime.AddSeconds(-dateTime.Second);

        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        TimeSpan diff = dateTime.ToUniversalTime() - origin;
        double epoch = Math.Floor(diff.TotalSeconds);
        epoch += (60 * minutes_offset);

        Console.WriteLine(epoch);

        if (epoch != currentTime)
        {
            Console.WriteLine("New time");
            currentTime = epoch;
            open = tick;
            high = tick;
            low = tick;
            close = tick;
            Console.WriteLine(currentTime + " " + open + " " + high + " " + low + " " + close);

            //Create new bar - unmodified
            using (var command = new NpgsqlCommand("INSERT INTO uk100_m1 (open, high, low, close, category) VALUES (@n1, @q1, @r1, @s1, @t1)", conn))
            {
                command.Parameters.AddWithValue("n1", open);
                command.Parameters.AddWithValue("q1", high);
                command.Parameters.AddWithValue("r1", low);
                command.Parameters.AddWithValue("s1", close);
                command.Parameters.AddWithValue("t1", currentTime);

                int nRows = command.ExecuteNonQuery();
                Console.Out.WriteLine(String.Format("Number of rows inserted={0}", nRows));
            }

            //Create new bar - closed gap
            if(lastClose>0)
            {
                open = lastClose;
            } else
            {
                open = open;
            }

            //Create new bar - closed gap
            using (var command = new NpgsqlCommand("INSERT INTO uk100_m1_closed (open, high, low, close, category) VALUES (@n1, @q1, @r1, @s1, @t1)", conn))
            {
                command.Parameters.AddWithValue("n1", open);
                command.Parameters.AddWithValue("q1", high);
                command.Parameters.AddWithValue("r1", low);
                command.Parameters.AddWithValue("s1", close);
                command.Parameters.AddWithValue("t1", currentTime);

                int nRows = command.ExecuteNonQuery();
                Console.Out.WriteLine(String.Format("Number of rows inserted={0}", nRows));
            }


        }
        else
        {
            Console.WriteLine("Update existing time");

            if (tick > high)
            {
                high = tick;
            }

            if (tick < low)
            {
                low = tick;
            }

            close = tick;

            Console.WriteLine(currentTime + " " + open + " " + high + " " + low + " " + close);

            //Update unmodified data
            using (var command = new NpgsqlCommand("UPDATE uk100_m1 SET high=@high, low=@low, close=@close WHERE category=@category", conn))
            {
                command.Parameters.AddWithValue("high", high);
                command.Parameters.AddWithValue("low", low);
                command.Parameters.AddWithValue("close", close);
                command.Parameters.AddWithValue("category", currentTime);
                int nRows = command.ExecuteNonQuery();
                Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
            }

            lastClose = close;

            //Update closed gap data
            using (var command = new NpgsqlCommand("UPDATE uk100_m1_closed SET high=@high, low=@low, close=@close WHERE category=@category", conn))
            {
                command.Parameters.AddWithValue("high", high);
                command.Parameters.AddWithValue("low", low);
                command.Parameters.AddWithValue("close", close);
                command.Parameters.AddWithValue("category", currentTime);
                int nRows = command.ExecuteNonQuery();
                Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
            }

        }


    }
...