Exemplo n.º 1
0
        private void insertToDatabase(ORModel[] oRModels)
        {
            const int MAX_ATTEMPTS = 10;

            for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++)
            {
                try
                {
                    Trace.TraceInformation("Inserting batch into DB. Attemt num.:" + attempt);
                    dataAdapter.BeginTransaction(TRANSACTION_NAME);

                    dataAdapter.Process(Operation.Insert, "someCommand", oRModels);

                    dataAdapter.CommitTransaction(TRANSACTION_NAME);

                    return;
                }
                catch (Exception e)
                {
                    if (attempt < MAX_ATTEMPTS)
                    {
                        Trace.TraceInformation("Inserting failed.");
                        Trace.TraceError(e.ToString());
                        dataAdapter.RollbackTransaction(TRANSACTION_NAME);
                    }
                    else
                    {
                        throw new Exception("Insert to DB not succesful. Total attempts: " + attempt);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var currentPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            Console.WriteLine($"Current path {currentPath}.");
            var pathInput = Path.Combine(currentPath, "../../Data");

            Console.WriteLine($"Looking for source files in {new DirectoryInfo(pathInput).FullName}.");

            TextWriterTraceListener tl = new TextWriterTraceListener(System.Console.Out);

            Debug.Listeners.Add(tl);

            DebugHelper.MeasureTime($"Creating test file of {numberOfTrades}", () =>
            {
                new Tester().CreateTestFile(pathInput, numberOfTrades);
            });

            string data = null;

            DebugHelper.MeasureTime("Reading file", () =>
            {
                data = File.ReadAllText(Path.Combine(pathInput, fileName));
            });

            // Worse performance than Custom deserialization but less code
            //IEnumerable<XmlTrade> trades2 = null;
            //DebugHelper.MeasureTime("Deserializing API", () =>
            //{
            //    trades2 = ApiDeserialize(data);
            //});

            IEnumerable <XmlTrade> trades = null;

            DebugHelper.MeasureTime("Parsing XML + Custom deserializing", () =>
            {
                var document = XDocument.Parse(data);
                trades       = CustomDeserialize(document.Root);
            });

            var pathOutput = Path.Combine(currentPath, "../../Output");
            var adapter    = new DataAdapter(pathOutput);

            DebugHelper.MeasureTime("PROCESS DATABASE", () =>
            {
                var globalRetries = 0;
                trades.Grouped(groupBy).Each((tradesGroup, index) =>
                {
                    var transactionName = $"no {index.ToString()}";

                    var failOrRetry = true;
                    var retry       = 0;
                    while (failOrRetry)
                    {
                        adapter.BeginTransaction(transactionName);
                        try
                        {
                            tradesGroup.Each(trade =>
                                             adapter.Process(Operation.Insert, "INSERT INTO dbo.Trades(ISIN, Quantity, Price, Direction) " +
                                                             $"VALUES({trade.ISIN}, {trade.Quantity}, {trade.Price}, {trade.Direction});"));
                            adapter.CommitTransaction(transactionName);
                            if (retry == 0)
                            {
                                Debug.WriteLine($"Successful transaction {transactionName}.");
                            }
                            else
                            {
                                Debug.WriteLine($"Successful transaction {transactionName} on {retry}. retry.");
                            }

                            failOrRetry = false;
                        }
                        catch (Exception e)
                        {
                            adapter.RollbackTransaction(transactionName);
                            Debug.WriteLine($"Exception {e.Message} in transaction {transactionName}/{retry}.");

                            retry++;
                            globalRetries++;
                            if (retry >= numberOfRetries)
                            {
                                failOrRetry = false;
                                Console.WriteLine($"Uncompleted transaction {transactionName}.");
                            }
                        }
                    }
                });

                Debug.Flush();
                Console.WriteLine($"Transactions/No of retries/Operations: {numberOfTrades / groupBy}/{globalRetries}/{numberOfTrades / groupBy + globalRetries}");
            });

            // best trades
            DebugHelper.MeasureTime("Best BUYS / from lower", () =>
            {
                BestTrades("B", trades,
                           (buys, orderBySelector) => buys.OrderBy(orderBySelector),
                           (bestTrades, orderBySelector) => bestTrades.OrderBy(orderBySelector));
            });

            DebugHelper.MeasureTime("Best SELS / from higher", () =>
            {
                BestTrades("S", trades,
                           (sels, orderBySelector) => sels.OrderByDescending(orderBySelector),
                           (bestTrades, orderBySelector) => bestTrades.OrderByDescending(orderBySelector));
            });
        }