我有一个请求者,负责管理针对 Azure SQL 数据库的 SQL 查询。负责事务查询的函数如下:
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"time"
"github.com/cenkalti/backoff"
_ "github.com/denisenkom/go-mssqldb" // Need to import the SQL driver so we can tell Golang how to interpret our requests
)
// Helper function that does a single Exec with a transaction with a context on a query and variables.
// This function will return an error if there are any failures
func (requester *Requester) doTransaction(ctx context.Context,
isolation sql.IsolationLevel, txFunc func(*sql.Tx) error) error {
// First, get the database connection; if this fails then return an error
conn, err := requester.getConn(ctx)
if err != nil {
return err
}
// Before we continue on, ensure that the connection is clsoed and returned to the connection pool
defer func() {
if err := conn.Close(); err != nil {
log.Printf("Close failed, error: %v", err)
}
}()
// Next, start the transaction with the given context and the default isolation
tx, err := requester.getTx(ctx, conn, isolation)
if err != nil {
return err
}
总的来说,这很有效。我注意到的一个问题是,当各个请求之间经过很长时间时,我会在第一次重试时遇到错误,然后在后续重试时出错,最终导致失败。我的想法是,问题与此错误有关。从本质上讲,微软似乎在30分钟后使空闲请求无效。但是,由于我将最大空闲时间设置为10分钟,因此这应该不是问题所在。i/o timeoutbad connection
这是怎么回事,我该如何解决这个问题?
子衿沉夜
相关分类