Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are database/sql transaction objects safe for concurrent access?

Tags:

go

I need to execute several SQL queries (select, update, delete) concurrently and roll back if any goroutine errors out. Thus the question: are DB transactions safe for concurrent access?

like image 578
hey Avatar asked Aug 15 '14 10:08

hey


1 Answers

DB is safe to be accessed from multiple goroutines:

DB is a database handle representing a pool of zero or more underlying connections.

It's safe for concurrent use by multiple goroutines.

Also Stmt is safe to be used from multiple goroutines:

Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.

You should use only one sql.Tx per goroutine:

Once DB.Begin is called, the returned Tx is bound to a single connection

like image 189
OneOfOne Avatar answered Oct 03 '22 23:10

OneOfOne