简体   繁体   中英

Exception after connection to DB with mysql-native driver

I want to create to function. The first one is connect to DB, the second one is complete reconnection if first is failed.

In my experiment I turn off DB at start to get connect block failed and call reconnect block. After it I am turning on DB, and expecting that connection block will success, but I am getting exception.

Here is my code:

bool connect()
{
    if(connection is null)
    {
        scope(failure) reconnect(); // call reconnect if fail
        this.connection = mydb.lockConnection();
        writeln("connection done");
        return true;
    }
    else
        return false; 

}


void reconnect()
{
    writeln("reconnection block");
    if(connection is null)
    {
        while(!connect) // continue till connection will not be established
        {
            Thread.sleep(3.seconds);
            connectionsAttempts++;
            logError("Connection to DB is not active...");
            logError("Reconnection to DB attempt: %s", connectionsAttempts);
            connect();
        }
    if(connection !is null)
    {
        logWarn("Reconnection to DB server done");
    }

    }

}

The log (turning on DB after few seconds):

reconnection block
reconnection block
connection done
Reconnection to DB server done

object.Exception@C:\Users\Dima\AppData\Roaming\dub\packages\vibe-d-0.7.30\vibe-d\source\vibe\core\drivers\libevent2.d(326): Failed to connect to host 194.87.235.42:3306: Connection timed out [WSAETIMEDOUT ]

I can't understand why I am getting exception after: Reconnection to DB server done

There's two main problems here.

First of all, there shouldn't be any need for automatic retry attempts at all. If it didn't work the first time, and you don't change anything, there's no reason doing the same exact thing should suddenly work the second time. If your network is that unreliable, then you have much bigger problems.

Secondly, if you are going to automatically retry anyway, that's code's not going to work:

For one thing, reconnect is calling connect TWICE on every failure: Once at the end of the loop body and then immediately again in the loop condition regardless of whether the connection succeeded. That's probably not what you intended.

But more importantly, you have a potentially-infinite recursion going on there: connect calls reconnect if it fails. Then reconnect calls connect up to six times, each of those times connect calls reconnect AGAIN on failure, looping forever until the connection configuration that didn't work somehow magically starts working (or perhaps more likely, until you blow the stack and crash).

Honestly, I'd recommend simply throwing that all away: Just call lockConnection (if you're using vibe.d) or new Connection(...) (if you're not using vibe.d) and be done with it. If your connection settings are wrong, then trying the same connection settings again isn't going to fix them.

lockConnection -- Is there supposed to be a matching "unlock"? – Rick James

No, the connection pool in question comes from vibe.d . When the fiber which locked the connection exits (usually meaning "when your server is done processing a request"), any connections the fiber locked automatically get returned to the pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM