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

Ping() not working #154

Closed
luca-moser opened this issue May 18, 2016 · 2 comments
Closed

Ping() not working #154

luca-moser opened this issue May 18, 2016 · 2 comments

Comments

@luca-moser
Copy link

luca-moser commented May 18, 2016

Hi, I am currently facing a problem with the Ping() method in the SQL package, when using this driver. It seems that Ping() suceeds no matter what.

I have some little test code:

package main

import (
    "database/sql"
    "fmt"
    "time"
    _ "github.com/denisenkom/go-mssqldb"
)

func main() {

    connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;log=1",
        "host", "username", "password", 1433, "databasename")
    conn, err := sql.Open("mssql", connString)
    if err != nil {
        panic(err)
    }

    err = conn.Ping()
    if err != nil {
        fmt.Println("ping and reconnect failed:", err.Error())
        return
    }
    fmt.Println("connection to mssql database server ok")

    <-time.After(time.Duration(10) * time.Second)

    fmt.Println("pinging again database server...")
    err = conn.Ping()
    if err != nil {
        fmt.Println("ping and reconnect failed:", err.Error())
        return
    }
    fmt.Println("connection to mssql database server ok")
}

When I run this code and remove the network cable on my laptop when the waiting is going on <-time.After(time.Duration(10) * time.Second), the second pinging still succeeds with: "connection to mssql database server ok", although the database server is physically not reachable (my wireless adapter is deactivated btw).

I am using Ping() to check wether the database server is still online and to automatically re-connect to it. From the godoc of database/sql it states: "Ping verifies a connection to the database is still alive, establishing a connection if necessary.". My apps crash/throw errors on the first workday each week, because we usually restart our database server on the weekends.

Note: If I run this program with no network connection from the beginning it fails up on the first Ping(). It looks like subsquential Ping() don't report correctly.

Note 2: If using conn.SetConnMaxLifetime(time.Duration(3) * time.Second) in this code, the second Ping() will fail as it should.

@dimdin
Copy link
Collaborator

dimdin commented May 18, 2016

The driver has no control. The golang Ping implementation reuses the last connection from the pool. see: https://golang.org/src/database/sql/sql.go?s=14098:14124#L489

@luca-moser
Copy link
Author

I see, thanks for the heads up.

gabrielcorado pushed a commit to gravitational/go-mssqldb that referenced this issue Oct 4, 2024
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

No branches or pull requests

2 participants