Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_DbContext.SaveChanges() not throwing proper exceptions #367

Open
sshih-rts opened this issue May 25, 2024 · 1 comment
Open

_DbContext.SaveChanges() not throwing proper exceptions #367

sshih-rts opened this issue May 25, 2024 · 1 comment
Labels
question Further information is requested

Comments

@sshih-rts
Copy link

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:

  • after -DBContext.SaveChanges(), it doesn't matter if the data got updated successfully or not, it always goes to the @ValidationException where e = null. But it skips the first line and goes straight to the 2nd line in that catch section mreturn.ValidationHelper.ReturnValidationError(ModelState)

         catch (e, @ValidationException)
         begin
             ModelState.AddModelError("RelationValidation",e.Message)
             mreturn ValidationHelper.ReturnValidationError(ModelState)
         end
         endtry
                                                                     
         mreturn NoContent()
    

But it does not return the validation error but goes to the statement outside the try-catch section and mreturn NoContent()

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
@sshih-rts sshih-rts added the question Further information is requested label May 25, 2024
@hippiehunter
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants