facat' blog

  • 编程Programming
  • 编译
  • 编译Compile
  • 随意
  • Tech

Fix "Too Many Connections" in Golang

2014-08-17
00:50:35
作者: facat

分类: 编程Programming.

Tags: gogolangMysql

Sometimes people may come into Too Many Connections error when using Mysql in Golang. A few things should be noted to avoid this error.

  1. Remember to use DB.COMMIT() if transaction is used. Connections to Mysql are released only if the transaction is commited. As far as I concern, DB.Exec doesn't have this problem.

  2. Use Row.Close() after you do a query.

For example:

row, err := db.Query("select a from b limit 1")
if row != nil {
    defer row.Close()
}

row.Close() is very important, otherwise Golang will keep that connection.

There are comments.

页 1 / 1

Proudly powered by Pelican, which takes great advantage of Python.

The theme is by Smashing Magazine, thanks!