How to modify Query timeout in Acumatica

Hello everybody,

today I want to share with you some trick which I used for cases if I get error message that some select from Acumatica timed out. Sometime if you work with big tables ( ~2 millions of records ) you may face timeout errors. Especially it is troublesome for processing screens when you are ready to wait for some kind of functionality just in order to be complete. Let me introduce you to property DefaultQueryTimeout of PXDataBase.Provider static class. By default it's value is set to 90, which means that it will be executed by sql server for 90 seconds. I should admit that for majority of cases it is more then enough. Except for cases when you need to fix some bugs in code. So, I had a need to modify that's value to something bigger. And everything is fine besides the fact that this property is read only, and doesn't have a setter. Take a look what JetBrains decompiler says about it:

public int DefaultQueryTimeout
{
  get
  {
    if (this.queryTimeout <= 0)
      return 30;
    return this.queryTimeout;
  }
}

It says that you have access to queryTimeout property. In code there is hardcoded value 30 seconds. And also it is read only. How to modify inner variable queryTimeout? Let's see how it is declared:

protected int queryTimeout;

After seen all of this I've made a decision to make trick that I don't like but forced to do in such a cases: use reflection. For such a puprpose I've created following class:

public class ReflectionMethods
{
    public static void SetPrivatePropertyValue<T>(T obj, string propertyName, object newValue)
    {
        // add a check here that the object obj and propertyName string are not null
        foreach (FieldInfo fi in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
        {
            if (fi.Name.ToLower().Contains(propertyName.ToLower()))
            {
                fi.SetValue(obj, newValue);
                break;
            }
        }
    }
}

And then, later in mine code I've wrote the following line:

ReflectionMethods.SetPrivatePropertyValue(PXDatabase.Provider, "queryTimeout", 900);

Such trick allowed me to get some more time for waiting response from SQL server

No Comments

Add a Comment