This is a creation in Article, where the information may have evolved or changed.
Problem:
2015/12/04 17:03:36 sqldb.go:2166:sqldriverconnect: {IM005} [MICROSOFT][ODBC Driver Manager] driver Sqlallochandle on SQL_ HANDLE_DBC failure
Environment: Go language writing
SQL SERVER Database
Packages used by the "CODE.GOOGLE.COM/P/ODBC" database operation
The program just started, running all right, but after a period of time, will always appear the above error message. Carefully review the printed error message, initially considered to be a database exception information.
Keep track of the database running, other programs connected to the database can be connected, troubleshooting database problems. However, the SQLDriverConnect reminder is clearly a reminder that the connection database failed. Suspect that there is a problem with the database connection pool.
To see the database connection for this application, the number of connections to the SQL Server database, the surprising discovery that there are more than 1000 connections, to see the detailed execution of the statement situation, found that the more than 1000 connections, in addition to a few connections of the other 1000 more are executed two statements. The same sentence notation
stmt, errs := Db.Prepare("select * from tbl")if errs!=nil {}defer stmt.Close()rows, err := stmt.Query()if err!=nil {}for rows.Next(){ varint ifnil { return value }}
All the SQL is written in the same way, but why only these two statements will appear so many connections can not be released, to view the usage of data Prepare syntax, only unexpectedly found that the rows need to shut down, the other statements did not appear multi-connection situation is because, the other rows are finished loop, Rows are self-releasing if they are automatically cycled, or they need to be closed manually.
stmt, errs := Db.Prepare("select * from tbl")if errs!=nil {}defer stmt.Close()rows, err := stmt.Query()if err!=nil {}defer//必须手动关闭连接for rows.Next(){ varint ifnil { return value }}
After the change, the program runs normally.