M
M
Muxauko2021-07-15 18:25:58
go
Muxauko, 2021-07-15 18:25:58

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

2 answer(s)
D
Dmitry Shitskov, 2021-07-15
@Zarom

*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.

P
Pavel Shvedov, 2021-07-15
@mmmaaak

gorm say thread safe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question