cancel
Showing results for 
Search instead for 
Did you mean: 

Could not commit transaction: Deadlock(-2038) detected during transaction

dilipkumbhar
Participant
0 Kudos

I have 5500 items and 450 BOM's. I want to update the price of all the items and then I will update the price of BOM. When I run the add-on, sometimes SAP gets hanged and sometimes I receive the following error -

Could not commit transaction: Deadlock(-2038) detected during transaction

Accepted Solutions (0)

Answers (2)

Answers (2)

edy_simon
Active Contributor
0 Kudos

Hi Dilip,

Below a quote from the SDK Help file

SAP Business One SDK 9.0

Avoiding Database Deadlocks

In a landscape with more than one client, it is possible that database deadlocks will occur. In most cases, an add-on can handle potential deadlocks simply by retrying the operation.

When handling deadlocks, the following rules apply:

  • Any variable read from the API in a transaction can no longer be used because the data is already dirty.
  • Keep retrying until the data is added.

The following is an example of handling potential deadlocks:

const long DEADLOCK_ERR_CODE = -2038;

long DeadlockTryTime = 100;

long i =0;

bool retCode = false;

for (i=0; i<DeadlockTryTime; i++)

{

    try

    {

        retCode = DoOperations(); //start end transaction are contained within the function.

        if (returnCode == success)

        {

            break;

        }

        else

        {

            // basic error handling

        }

    }

    catch (System.Runtime.InteropServices.COMException ex)

    {

        if (ex.ErrorCode == DEADLOCK_ERR_CODE)

        {

            continue;

        }

        else

        {

            // error handling for non-deadlock ComException exceptions

        }

    }

}

if (i == DeadlockTryTime)

{

    // Handle that deadlock retry failed

}

Regards

Edy

Johan_H
Active Contributor
0 Kudos

Hi,

-2038 is a common time-out / deadlock error. This can sometimes happen, especially when you perform a lot of transactions.

In your addon you should build in an error checking routine that detects the -2038 error, and that simply retries the transaction after a couple of milliseconds.

Regards,

Johan