Why using ADO DB connection is much slower if run directly by MAS?

SOLVED

First of all, this is for Sage 100 ERP (5.00.2.0) and SQL Server 2008 R2

I've been scratching my head.  I have a simple code that is triggered at post-write in SO_SalesOrderHeader, i.e. a user hitting "Accept" in Order Entry window to execute. 

The code simply creates ADODB connection object, and execute a stored procedure (which pulls data using OpenQuery from MAS and updates the SQL server using Merge statement).  Easy peasy.  Below is the original code.


Dim ObjConnection

Dim strSalesOrderNo

Dim strSQL

Set objConnection = CreateObject("ADODB.Connection")
strSQL = "EXEC spProc @spOrderNo = '" + strOrderNo + "'"

On Error Resume Next

ObjConnection.Open "DSN=DBname;"
ObjConnection.CommandTimeout = 0
ObjConnection.Execute (strSQL)
ObjConnection.Close

set objConnection = Nothing


While it runs fine, I noticed that this code incurs nearly 1.5 second pause...Normally, clicking Accept button makes everything grayed out instantly and give the control back to user.  But it's now noticeably, oddly, long.  So I experimented by using 2 separate script files; first one creates a connection object to SQL server and executes the stored proc, another creates Wscript.shell object and triggers the first script file using cscript.exe.   Code below is for a trigger, again nothing fancy.   I'll omit the execution code since it's pretty much a repeat.


Dim strOrderNo

strFileName = Chr(34) & "\\server\Script_Exec.vbs" & Chr(34)
strOrderNo = ""
retval = oBusobj.getvalue("SalesOrderNo$", strOrderNo)

OnErrorResumenext
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "cscript " & strFileName & " " & strOrderNo, , false
Set WSHShell = Nothing

OnErrorGoto0


Now here are the question. 

  1. Why does this 2-script-file method perform much faster than the original even though the original deals with only one object and the new method has 2 objects?
  2. Also, can this 2-script-file method in any way cause some of the executed stored proc/statement to be indefinitely running but waiting with OLEDB wait type?  (I have 3 sessions that have been on running/waiting status for 3 days now.  Killing them only changes the status and keeps them hanging indefinitely)

I hope you can at least give me a hint for the 1st question!  Thanks in advance :)

  • 0
    verified answer

    It seems to me that the speed difference is because in the first script, the UI waits for the query to execute before it moves to the next line and then completes the script.  When you kick off a wscript.shell command, it executes the command to kick off the script and then immediately goes to the next line.  It has no relationship or connection to the actual script that's running, so that script may or may not ever finish but the UI is immediately released.  Depending on what your stored procedure is doing, that may or may not be a problem for you.

  • 0 in reply to hyanaga

    Thanks for the response hyanaga!

    Your input made me realize that there was an asynchronous option so i changed it.  It works great now.   Hopefully this was the culprit for the wait issue as well.   Thanks again!


    Const adAsyncExecute = 16

    ObjConnection.Execute (strSQL),,adAsyncExecute
     

    ----Edited 6/5/2015--


    It turned out AsyncExdcute wasn't really helpful after all.  Since the connection can't be closed while something is being executed, I found a new issues with error being raised when the script executes .close command.  Back to drawing board....

  • 0 in reply to msgking

    Update  on the 2nd issue I was having (though still hoping that asynchronous execution would have solved it).   All of the OELDB wait happened on 2 separate stored proc, both of which use Openquery statement against MAS Server.  

    While I still can't confirm the fix since i was never able to recreate the issue at will, I found that certain queries are susceptible to infinite wait or loong wait.

    For example, WHILE someone is still creating a new order no "0012345", below would complete and return a row (mostly null for dirty read).  Easy, right?


    OPENQUERY (MAS90_LINK,

    'Select SalesorderNo From SO_SalesOrderheader Where SalesOrderNo = ''0012345''')


    But below would result in an infinite wait  at least 2 hour wait after the record on MAS side has been committed or rolled back. 


    OPENQUERY(MAS90_LINK, 

    Select Columns 

    From
    SO_SalesOrderHeader SOH,
    SY_Country SYC1,
    SY_Country SYC2,
    {OJ AR_Customer LEFT OUTER JOIN AR_CustomerContact
    ON
    AR_Customer.ContactCode = AR_CustomerContact.ContactCode AND
    AR_Customer.CustomerNo = AR_CustomerContact.CustomerNo}

    WHERE
    SOH.SalesOrderNo = ''0012345'' and
    (SOH.OrderType IN (''B'', ''S'')) and
    SOH.ShipToCountryCode = SYC1.CountryCode AND
    SOH.BillToCountryCode = SYC2.CountryCode AND
    SOH.CustomerNo = AR_Customer.CustomerNo


    And here is the kicker.  Interestingly, If i omit either the outer join or SY_country joins (both), the query completes with no rows returned.  

    Anybody has any idea on

    1) why the infinite wait?  

    2) why it completes with seemingly meaningless change in joins?

    3) should i be asking this in different way (like discussion or somewhere else)?