Answer the question
In order to leave comments, you need to log in
Goroutine-safe communication with MySql?
Hello. Please give an example or good information available on how I can make a goroutine-safe call to mysql. There will be a mysql database. The database will contain users with their data. While the program is running, data can be changed, read, written. And this can happen simultaneously with different goroutines. How do I build a call to mysql.
Answer the question
In order to leave comments, you need to log in
*sql.DB (and derived types that use it under the hood, such as *gorm.DB) are already thread-safe. The main thing is to open the connection 1 time during the initialization of the application and it can be reused in all goroutines.
The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question