gorm randomly returned nil for a count query

59 Views Asked by At

For paging purposes, we have two queries running concurrently, the main query fetches the items (limit to the page size) and the other query runs in a goroutine which queries the total count of items satisfying the conditions (which could be more than one page). The problem is that we randomly got nil returns from the count query. SQL count querys should never return nil, right? In addition, when the problem happened, if I reran the same count query from another client manually, I could get the count correctly. So something was not right when a nil was returned. The count query looks like below, and the 'row' variable occasionally returned nil. We could check for nil to avoid crashing, but I wonder when it would ever return nil for count. Btw, the main and count queries are both quite complex, including building tree structures with recursive clauses (not sure if that has anything to do with the nil returns).

go func() {
    var rowCount int
    row := db.Raw(countSQL, args...).Row()
    err = row.Scan(&rowCount)

Gorm is supposed to be thread safe with goroutines. I wonder what else could have caused this. It does look like a racing condition from the surface. Any help will be appreciated. Thanks!

2

There are 2 best solutions below

0
gongqin On

The problem was due to concurrent queries with the same gorm session. After using a new session in the goroutine, the problem went away.

row := db.Session(&gorm.Session{}).Raw(countSQL, args...).Row()
0
Ashaffah On

Try

db.Raw(countSQL, args...).Count(&rowCount)