denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 499 forks source link

[BUG] Returning only partial results for db queries #743

Closed AmaliMatharaarachchi closed 2 years ago

AmaliMatharaarachchi commented 2 years ago

Describe the bug Let's say query1 should return 40 result rows. But it returns only a partial set of the results (e.g. sometimes 8 results only) when timeout context is given.

To Reproduce I

func withTimeout() {
    var d1 int
    cont, cancel := context.WithTimeout(context.Background(), time.Duration(300)*time.Minute)
    defer cancel()
    row, err := DB.QueryContext(cont, query1)
    if err == nil {
        for {
            if !row.Next() {
                logger.log("No more data")
                break
            } else {
                row.Scan(&d1)
                logger.log(d1)
            }
        }
    } 
}
func withoutTimeout() {
    var d1 int
    row, err := DB.QueryContext(context.Background(), query1)
    if err == nil {
        for {
            if !row.Next() {
                logger.log("No more data")
                break
            } else {
                row.Scan(&d1)
                logger.log(d1)
            }
        }
    } 
}

withTimeout() prints only some records. withoutTimeout() prints all 40 records.

Note that even we give a large value for the timeout (e.g. 300 min), it still has not work.

Further technical details

SQL Server version: run in docker from mcr.microsoft.com/mssql/server:2019-latest Operating system: Docker container version: github.com/denisenkom/go-mssqldb v0.12.0

AmaliMatharaarachchi commented 2 years ago

This issue has happened because I was doing the cancel() before finishing rows scanning. Hence this is not a bug.