You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a database name is provided that does not exist, we get back an error however even though db.Close() is executed the network connection still remains open.
To Reproduce
package main
import (
"context""database/sql""flag""fmt""os""os/signal"
_ "github.com/denisenkom/go-mssqldb"
)
funcmain() {
var (
username="sa"password="Password123"hostname="127.0.0.1"port=1433database=flag.String("db", "master", "database name")
count=flag.Int("count", 1, "number of calls to make to the database")
)
flag.Parse()
dsn:=fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", username, password, hostname, port, *database)
ctx, stop:=signal.NotifyContext(context.Background(), os.Interrupt)
deferstop()
fori:=0; i<*count; i++ {
iferr:=doQuery(ctx, dsn); err!=nil {
fmt.Println("error running query", i+1, err.Error())
} else {
fmt.Printf("run %d done\n", i+1)
}
}
fmt.Println("waiting for ctrl+c to exit")
<-ctx.Done()
}
funcdoQuery(ctx context.Context, dsnstring) error {
db, err:=sql.Open("sqlserver", dsn)
iferr!=nil {
returnfmt.Errorf("error open db %w", err)
}
deferdb.Close()
row:=db.QueryRowContext(ctx, "SELECT 1")
varresultintiferr:=row.Scan(&result); err!=nil {
returnfmt.Errorf("error scanning row %w", err)
}
returnnil
}
If you provide a database name that exists then should see the following, before sending the exit signal if you look for connections to the database it will be in TIME_WAIT
❯ go run main.go -db exists
run 1 done
waiting for ctrl+c to exit# new terminal
❯ netstat -an | grep '1.1433'
tcp4 0 0 127.0.0.1.60859 127.0.0.1.1433 TIME_WAIT
However if you provide a database name that does not exist then the connection remains ESTABLISHED even though it should have been closed.
❯ go run main.go -db unknown
error running query 1 error scanning row mssql: login error: Cannot open database "unknown" that was requested by the login. Using the user default database "master" instead.
waiting for ctrl+c to exit# new terminal
❯ netstat -an | grep '1.1433'
tcp4 0 0 127.0.0.1.1433 127.0.0.1.60894 ESTABLISHED
tcp4 0 0 127.0.0.1.60894 127.0.0.1.1433 ESTABLISHED
Expected behavior
The network connection should be closed
Further technical details
SQL Server version: docker image mcr.microsoft.com/mssql/server:2022-latest
Operating system: macOS 13.6.3
The text was updated successfully, but these errors were encountered:
Describe the bug
If a database name is provided that does not exist, we get back an error however even though
db.Close()
is executed the network connection still remains open.To Reproduce
If you provide a database name that exists then should see the following, before sending the exit signal if you look for connections to the database it will be in
TIME_WAIT
However if you provide a database name that does not exist then the connection remains
ESTABLISHED
even though it should have been closed.Expected behavior
The network connection should be closed
Further technical details
SQL Server version: docker image mcr.microsoft.com/mssql/server:2022-latest
Operating system: macOS 13.6.3
The text was updated successfully, but these errors were encountered: