第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Golang 中對(duì)長(zhǎng)時(shí)間運(yùn)行的 MSSQL 事務(wù)的連接響應(yīng)錯(cuò)誤

Golang 中對(duì)長(zhǎng)時(shí)間運(yùn)行的 MSSQL 事務(wù)的連接響應(yīng)錯(cuò)誤

Go
呼喚遠(yuǎn)方 2022-08-24 17:20:11
我有一個(gè)請(qǐng)求者,負(fù)責(zé)管理針對(duì) Azure SQL 數(shù)據(jù)庫(kù)的 SQL 查詢。負(fù)責(zé)事務(wù)查詢的函數(shù)如下: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 failuresfunc (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    }總的來說,這很有效。我注意到的一個(gè)問題是,當(dāng)各個(gè)請(qǐng)求之間經(jīng)過很長(zhǎng)時(shí)間時(shí),我會(huì)在第一次重試時(shí)遇到錯(cuò)誤,然后在后續(xù)重試時(shí)出錯(cuò),最終導(dǎo)致失敗。我的想法是,問題與此錯(cuò)誤有關(guān)。從本質(zhì)上講,微軟似乎在30分鐘后使空閑請(qǐng)求無效。但是,由于我將最大空閑時(shí)間設(shè)置為10分鐘,因此這應(yīng)該不是問題所在。i/o timeoutbad connection這是怎么回事,我該如何解決這個(gè)問題?
查看完整描述

1 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

經(jīng)過一些調(diào)查,我發(fā)現(xiàn)數(shù)據(jù)庫(kù)連接在 30 分鐘窗口后變得陳舊,修改連接池的生存期或空閑時(shí)間并不能真正解決此問題。因此,為了緩解這個(gè)問題,我所做的是事先修改我的函數(shù)以ping服務(wù)器,這樣我就可以確保連接是“新鮮的”,因?yàn)槿狈Ω玫男g(shù)語。getConn


func (requester *Requester) getConn(ctx context.Context) (*sql.Conn, error) {


    // First, attempt to ping the server to ensure that the connection is good

    // If this fails, then return an error

    if err := requester.conn.PingContext(ctx); err != nil {

        return nil, err

    }


    // Create an object that will dictate how and when the retries are done

    // We currently want an exponential backoff that retries a maximum of 5 times

    repeater := backoff.WithContext(backoff.WithMaxRetries(

        backoff.NewExponentialBackOff(), 5), ctx)


    // Do a retry operation with a 500ms wait time and a maximum of 5 retries

    // and return the result of the operation therein

    var conn *sql.Conn

    if err := backoff.Retry(func() error {


        // Attempt to get the connection to the database

        var err error

        if conn, err = requester.conn.Conn(ctx); err != nil {


            // We failed to get the connection; if we have a login error, an EOF or handshake

            // failure then we'll attempt the connection again later so just return it and let

            // the backoff code handle it

            log.Printf("Conn failed, error: %v", err)

            if isLoginError(err, requester.serverName, requester.databaseName) {

                return err

            } else if strings.Contains(err.Error(), "EOF") {

                return err

            } else if strings.Contains(err.Error(), "TLS Handshake failed") {

                return err

            }


            // Otherwise, we can't recover from the error so return it

            return backoff.Permanent(err)

        }


        return nil

    }, repeater); err != nil {

        return nil, err

    }


    return conn, nil

}


查看完整回答
反對(duì) 回復(fù) 2022-08-24
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)