Search code examples
gogo-gorm

Should I use some blocking mechanisms with async access to DB


I'm writing an async chatbot in Go with gorm and Postgres. Should I some blocking mechanism when using the gorm db client or I may safely avoid mutexes or etc. For example, I'm using gorm db client in different goroutines. Something like:

func () {

db.Update(...)

}

Solution

  • You don't need to do that. From the official database/sql docs (which gorm wraps), we can see (emphasis added):

    The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the OpenDB function should be called just once. It is rarely necessary to close a DB.