Exemplo n.º 1
0
        public async void pomocnaMetoda(string tableName, string condition, string action)
        {
            int rezultat = 0;
            // bool provera = false;
            int       timeOut   = 2000;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            /*try
             * {
             *  //OtvoriKonekciju();
             *
             *  if (konekcija.State == ConnectionState.Closed)
             *  {
             *      konekcija.Open();
             *  }
             *  PocniTransakciju();
             *  //konekcija.Open();
             *
             *  SqlCommand command = new SqlCommand();
             *  command = konekcija.CreateCommand();
             *  // transakcija = konekcija.BeginTransaction();
             *  command.Connection = konekcija;
             *  command.Transaction = transakcija;
             *  //"SELECT COUNT(" + txtCondition.Text + ") FROM " + cmbAssociatedForms.SelectedItem.ToString() + "";
             *  // + cmbAssociatedForms.SelectedItem.ToString() +
             *
             *  command.CommandText = "SELECT COUNT(*) FROM " + tableName + " WHERE " + condition + "";
             *
             *  rezultat = (int)command.ExecuteNonQuery(); //rezultat = (int)command.ExecuteScalar();
             *
             *
             *  if (rezultat != 0)
             *  {
             *      command.CommandText = action;
             *      //SqlCommand komanda2 = new SqlCommand(upit, konekcija, transakcija);
             *      command.ExecuteNonQuery(); //rezultat = command.ExecuteNonQuery();
             *      transakcija.Commit();
             *      log.WriteLine("Both records are written to database.");
             *  }
             * }
             *
             * catch (Exception ex)
             * {
             *  log.WriteLine("Commit Exception Type: {0}", ex.GetType());
             *  log.WriteLine("  Message: {0}", ex.Message);
             *  try
             *  {
             *      // PonistiTransakciju();
             *      transakcija.Rollback();
             *  }
             *  catch (Exception ex2)
             *  {
             *
             *      log.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
             *      log.WriteLine("  Message: {0}", ex2.Message);
             *  }
             *
             * }
             * finally
             * {
             *  konekcija.Close();
             * }
             * }*/
            while (true)
            {
                try
                {
                    lock (locker)
                    {
                        //OtvoriKonekciju();
                        if (konekcija.State == ConnectionState.Closed)
                        {
                            konekcija.Open();
                        }

                        //PocniTransakciju();
                        SqlCommand command = new SqlCommand();
                        command             = konekcija.CreateCommand();
                        transakcija         = konekcija.BeginTransaction();
                        command.Connection  = konekcija;
                        command.Transaction = transakcija;
                        //"SELECT COUNT(" + txtCondition.Text + ") FROM " + cmbAssociatedForms.SelectedItem.ToString() + "";
                        // + cmbAssociatedForms.SelectedItem.ToString() +

                        command.CommandText = "SELECT * FROM " + tableName + " WHERE " + condition + "";
                        rezultat            = (int)command.ExecuteScalar(); //rezultat = (int)command.ExecuteScalar();

                        if (rezultat != 0)
                        {
                            command.CommandText = action;
                            //SqlCommand komanda2 = new SqlCommand(upit, konekcija, transakcija);
                            command.ExecuteNonQuery(); //rezultat = command.ExecuteNonQuery();
                            transakcija.Commit();
                            //log.WriteLine("Both records are written to database.");
                            log.WriteLine("SUCCESS :" + action);
                        }
                    }
                    //konekcija.Close();
                    break;
                }
                catch (Exception ex)
                {
                    log.WriteLine("Commit Exception Type: {​​​​0}​​​​" + ex.GetType() + action);
                    log.WriteLine(" Message: {​​​​0}​​​​" + ex.Message + action);
                    try
                    {
                        // PonistiTransakciju();
                        transakcija.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        log.WriteLine("Rollback Exception Type: {​​​​0}​​​​" + ex2.GetType() + action);
                        log.WriteLine(" Message: {​​​​0}​​​​" + ex2.Message + action);

                        if (stopwatch.ElapsedMilliseconds > timeOut)
                        {
                            //Give up.
                            break;
                        }
                        konekcija.Close();
                        await Task.Delay(2);
                    }
                }
            }
            stopwatch.Stop();
        }