Synergex / HarmonyCore

Harmony Core is a framework that consists of libraries, CodeGen templates, and conventions that enable you to expose Synergy logic and data as a RESTful web service using OData and ASP.NET Core
BSD 2-Clause "Simplified" License
23 stars 14 forks source link

_DbContext.SaveChanges() not throwing proper exceptions #367

Open sshih-rts opened 5 months ago

sshih-rts commented 5 months ago

In our system, we have a C# program that uses the harmonycore API to fetch and update data from VMS. Occasionally we found record lock error on ISM file when the C# program updates ISM files on VMS. When that happened, the file is locked for all apps including DIBOL programs in VMS. The only way to unlock the file is to stop the C# program or restart xfserver in VMS.

I debug the module in API that updates VMS data and found some issues:

Here're some questions:

  1. When the record got updated successfully, the method does not return the proper status showing update process.
  2. It looks like _DBContext.SaveChanges() updates VMS data. When a record lock happens, is this the method that might be locking the file?
  3. DBContext class extends Microsoft.EntityFrameworkCore.DbContext. How can I catch exceptions when it failed to update data due to record lock/time out?

SalesorderDetialsControoler.dbl

    {HttpPatch("SalesorderDetails(Salesorderlineid={aSalesorderlineid})")}
    {Consumes(MediaTypeNames.Application.Json)}
    {Produces("application/json")}
    {ProducesResponseType(StatusCodes.Status204NoContent)}
    {ProducesResponseType(StatusCodes.Status400BadRequest)}
    {ProducesResponseType(StatusCodes.Status401Unauthorized)}
    {ProducesResponseType(StatusCodes.Status404NotFound)}
    ;;; <summary>
    ;;; Patch (partial update) an existing record
    ;;; </summary>
    ;;; <remarks>
    ;;;
    ;;; </remarks>
    ;;; <param name="aSalesorderlineid" example="ABC">Key segment overlay</param>
    ;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
    ;;; <response code="204"><HTTP_204_MESSAGE></response>
    ;;; <response code="400"><HTTP_400_MESSAGE></response>
    ;;; <response code="401"><HTTP_401_MESSAGE></response>
    ;;; <response code="404"><HTTP_404_MESSAGE></response>
    ;;; <response code="500"><HTTP_500_MESSAGE></response>
    public method PatchSalesorderDetail, @IActionResult
        required in aSalesorderlineid, String
        {FromBody}
        required in aSalesorderDetail, @JsonPatchDocument<SalesorderDetail>
    proc
        ;; Validate inbound data
        if (!ModelState.IsValid)
            mreturn ValidationHelper.ReturnValidationError(ModelState)

        ;;Patch the existing salesorderDetail
        try
        begin
            ;;Get the salesorderDetail to be updated
            data salesorderDetailToUpdate = _DbContext.SalesorderDetails.Find(aSalesorderlineid.PadRight(11))
            data patchError, @JsonPatchError, ^null
            ;;Did we find it?
            if(salesorderDetailToUpdate == ^null)
                mreturn NotFound()

            ;;Apply the changes to the salesorderDetail we read
            aSalesorderDetail.ApplyTo(salesorderDetailToUpdate, lambda(error) { patchError = error })
            ;;if the patchdoc was bad return the error info
            if(patchError != ^null)
                mreturn BadRequest(string.Format("Error applying patch document: error message {0}, caused by {1}", patchError.ErrorMessage, JsonConvert.SerializeObject(patchError.Operation)))

            ;;Update and commit
            _DbContext.SalesorderDetails.Update(salesorderDetailToUpdate)
            _DbContext.SaveChanges()
        end
        catch (e, @InvalidOperationException)
        begin
            mreturn BadRequest(e)
        end
        catch (e, @ValidationException)
        begin
            ModelState.AddModelError("RelationValidation",e.Message)
            mreturn ValidationHelper.ReturnValidationError(ModelState)
        end
        endtry

        mreturn NoContent()

    endmethod
hippiehunter commented 5 months ago

Do you have a crash dump of the program when it's hung? It would be pretty easy to figure out whats going on if we could look around at the state of things when it happens.