mithrandyr / SimplySql

PowerShell module for querying various SQL databases
MIT License
197 stars 31 forks source link

ParentContainsErrorRecordException when accessing mariadb #108

Closed azra1l closed 1 year ago

azra1l commented 1 year ago

Sometimes, when i query a mariadb database, my powershell script throws this error:

ParentContainsErrorRecordException: C:\Program Files\WindowsPowerShell\Modules\SimplySql\1.8.0\Providers\MySql\provider.ps1:42
Line |
  42 |              $da.Fill($ds)
     |              ~~~~~~~~~~~~~
     | Exception calling "Fill" with "1" argument(s): "Fatal error encountered during command execution."

I think it is somehow linked to the size of data returned from the query, but i can't reliably reproduce the error. When i immediatly run the exact same command again, it works without issue, or at least i never had this problem two times in a row. I think it progressively/gradually appears more often.

I tried to build my script around this error with a try/catch block, to just re-run the query if this specific error is thrown, but it somehow doesn't work and my script throws anyways.

Here is my try/catch block:

        Try {
            Invoke-SqlQuery -Query $Query -ConnectionName $Session
        }
        Catch [System.Management.Automation.ParentContainsErrorRecordException] {
            Write-Warning -Message 'Exception ParentContainsErrorRecordException, trying again'
            Invoke-SqlQuery -Query $Query -ConnectionName $Session
        }
        Catch {
            Throw $_.Exception
        }

It doesn't show the warning and will throw right away. Am i using the right error class? Or is it because the module already throws?

azra1l commented 1 year ago

By the way, the problem also appears on a PC where i have the current version 1.9.0 of simplysql installed.

azra1l commented 1 year ago

I stand corrected. For the first time, the error just appeared two times in a row for the same command. Not a third time though. The error was also thrown instantly, it usually takes some time, therefore i linked it to size of the resultset.

mithrandyr commented 1 year ago

@azra1l The error you are receiving is coming from the underlying MySql provider. You will need to investigate what is happening either on the MySQL server or in the MySQL provider that is the problem. Take a look at the entire stacktrack for the error.

azra1l commented 1 year ago

@mithrandyr ok, yes, thats the plan :) but why is it that i can't catch the exception thrown? it shouldn't matter where it comes from? when the exception details are shown in console output, it should be possible to catch it in the script i execute...

mithrandyr commented 1 year ago

@azra1l -- I think you need to use the -ErrorAction parameter and specify "Stop" for the try/catch to work (I believe the module is only throwing non-terminating errors). You may also want to use a more generic catch.