Kentico 12 MVC timeout error

S T asked on October 25, 2023 14:17

Hi, I'm using kentico v12.0.101 and I'm using a custom handler that, when triggered, goes thru an excel file and creates categories using the API found here: https://docs.xperience.io/api12sp/configuration/categories. The excel file has 250 entries but the transaction fails due to a timeout failure every 10 minutes and returns the following errors:

  1. "The operation is not valid for the state of the transaction"

  2. "This SqlTransaction has completed; it is no longer usable."

  3. "the transaction associated with the current connection has completed but has not been disposed. the transaction must be disposed before the connection can be used to execute sql statements."

with InnerExceptions as "Transaction Timeout".

This occurs even tho I don't have nested transaction scopes and the one that I do have is set with a timespan of 1H (using (var scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(1, 0, 0))))

Where do I look to expand the timeout or what can be causing the timeout?

Regards

Recent Answers


Jono Rickard answered on October 25, 2023 20:46

Heya,

Its a bit hard without seeing some more code - but I would suggest a few things generally;

  • keep the SQL transaction scope as small as possible - so in this case, depending on how you're processing the excel file, you might be able to do a single insert & transaction per row. Its fine to have a bunch of small transactions.
  • If you're stilling having issues, you can also increase the timeout in the connectionString in the web.config (See Connect Timeout below, its in seconds) eg Data Source=<sql-server>;Initial Catalog=<db-name>;Integrated Security=False;Persist Security Info=False;User ID=<username>;Password=<password>;Connect Timeout=60;Encrypt=False;Current Language=English;

Cheers Jono

0 votesVote for this answer Mark as a Correct answer

S T answered on October 26, 2023 11:00

Hi @Jono The connect timeout was set to 60 in the config and after I added more time it still broke and timed out after 10 minutes. Is there another solution, I'll post the code example below.

        {using (var scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(1, 0, 0)))
        {
            try
            {
                string dir = HttpContext.Current.Server.MapPath("\\");

                HSSFWorkbook hssfwb;
                using (FileStream file = new FileStream(dir + @"\DataImport\SC_Import.xls", FileMode.Open, FileAccess.Read))
                {
                    hssfwb = new HSSFWorkbook(file);
                }

                List<SolutionCategoryItem> solutionCategories = new List<SolutionCategoryItem>();

                //Excel.Range range = worksheet.UsedRange;
                //int rowCount = range.Rows.Count;

                ISheet sheet = hssfwb.GetSheetAt(0);
                for (int row = 1; row <= sheet.LastRowNum; row++)
                {
                    if (sheet.GetRow(row) != null) //null is when the row only contains empty cells 
                    {
                        var ifpCategory1 = sheet.GetRow(row).GetCell(0).StringCellValue;
                        var titleValue = sheet.GetRow(row).GetCell(1).StringCellValue;
                        var descriptionValue = sheet.GetRow(row).GetCell(2).StringCellValue;

                        SolutionCategoryItem item = new SolutionCategoryItem
                        {
                            IFPCategory1 = ifpCategory1,
                            Title = titleValue,
                            Description = descriptionValue
                        };

                        solutionCategories.Add(item);
                    }
                }

                //ADD TO CATEGORIES MODULE
                // Gets the parent category
                CategoryInfo parentCategory = CategoryInfoProvider.GetCategoryInfo("SolutionCategories", SiteContext.CurrentSiteName);

                if (parentCategory != null)
                {
                    if (solutionCategories.Any())
                    {
                        foreach (var item in solutionCategories)
                        {
                            if (item != null)
                            {
                                // Creates a new category object
                                CategoryInfo subcategory = new CategoryInfo();

                                // Sets basic properties
                                subcategory.CategoryDisplayName = item.Title;
                                //subcategory.CategoryName = item.Title.Replace(" ", "");
                                var tempStr = Regex.Replace(item.Title, "[^A-Za-z0-9]", "");
                                subcategory.CategoryName = tempStr;
                                subcategory.CategoryDescription = item.Description;
                                subcategory.CategorySiteID = SiteContext.CurrentSiteID;
                                subcategory.CategoryEnabled = true;

                                // Assigns to the parent category
                                if (!string.IsNullOrEmpty(item.IFPCategory1))
                                {
                                    CategoryInfo SolutionCategoryParent = CategoryInfoProvider.GetCategoryInfo(item.IFPCategory1, SiteContext.CurrentSiteName);
                                    if (SolutionCategoryParent != null)
                                    {
                                        subcategory.CategoryParentID = SolutionCategoryParent.CategoryID;
                                    }

                                }


                                // Saves the category to the database
                                CategoryInfoProvider.SetCategoryInfo(subcategory);


                            }

                        }
                    }

                    scope.Complete();

                    HttpContext.Current.Response.ContentType = "text/html";
                    HttpContext.Current.Response.Write("Solution categories added successfully...");
                }


            }
            catch (Exception e)
            {
                HttpContext.Current.Response.ContentType = "text/html";
                HttpContext.Current.Response.Write(e.Message);
            }
        }
        }
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.