DRYな備忘録

Don't Repeat Yourself.

データベースのコネクションプーリング(とGo言語におけるそれ)

コネクションプーリングとは

「コネクションを張る/切る」というコストを削減することが主な目的。1つのDBに対するコネクション総数を制御する、というのはまた別概念(だけど同時に考慮、実装されることがおおい)。

Go言語におけるdatabase/sql

ドキュメントを見ると、コネクションプールとかよしなにやってくれるらしい。 最初は以下のようにやってたけど、不要のようだ。

(いつみてもtenntennさんの記事は勉強になるますmm)

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.

The sql package creates and frees connections automatically; it also maintains a free pool of idle connections. If the database has a concept of per-connection state, such state can only be reliably observed within a transaction. Once DB.Begin is called, the returned Tx is bound to a single connection. Once Commit or Rollback is called on the transaction, that transaction's connection is returned to DB's idle connection pool. The pool size can be controlled with SetMaxIdleConns.

DRYな備忘録