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

Fix(connection/raw query) handle promise when db connection lost #258

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

stevealexandre
Copy link

@stevealexandre stevealexandre commented Dec 24, 2024

I'm currently developping a gta fivem resource where it could use oxmysql optionnally so I need to check if the database is available or not even after the oxmysql resource is started.
Currently oxmysql do not check if the database is still available after the start while for any reason, the connection can be lost.
Following my tests, I got a promise error not managed which cannot let us handle this case :
image

After my analysis of your code, I found a quick fix for that which result of this with the test code after:
image

Here my sample code for testing db connection :

local mysqlHost = GetConvar("mysql_connection_string", "")
local mysqlReady = false
if mysqlHost ~= "" then
    Citizen.CreateThread(function()
        ---Verify if MySQL is ready to send to database

        while GetResourceState("oxmysql") ~= "started" or not exports.oxmysql.isReady() do
            Citizen.Wait(30000)
        end
        -- test callback mode
       exports.oxmysql.query(nil, 'SELECT VERSION() as version', {}, function(result, error)
            if error then
                print("MySQL is not ready !")
                mysqlReady = false
            else
                print("MySQL is ready !")
                mysqlReady = true
            end
        end)

        -- test async mode
        local result = exports.oxmysql.query_async(nil, 'SELECT VERSION() as version', {});
        if result then
            print("MySQL async is ready !")
            mysqlReady = true
        else
            print("MySQL async is not ready !")
            mysqlReady = false
        end
    end)
end

I don't have a full vision of impact which could happens for other resources so feel free to suggest/review my fix.
Maybe a method to test the real current status of database connection could be great and not only trusting local state.

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

Successfully merging this pull request may close these issues.

1 participant