登录
首页 >  Golang >  Go问答

解决 Azure SQL 错误

来源:stackoverflow

时间:2024-02-24 17:45:26 454浏览 收藏

你在学习Golang相关的知识吗?本文《解决 Azure SQL 错误》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我有一个旨在处理 tsql 查询的函数。本质上,它尝试创建与服务器的连接,运行查询,提交,然后关闭连接。目前,我们依赖 mssqldb 驱动程序,因为后端是 azure sql。

func (requester *requester) dotransaction(ctx context.context, isolation sql.isolationlevel, 
    txfunc func(*sql.tx) error) error {

    // first, attempt to get a connection from the connection pool. if this fails return an error
    conn, err := requester.conn.conn(ctx)
    if err != nil {
        fmt.printf("conn failed, error type: %s\n", reflect.typeof(err))
        log.printf("conn failed, error: %v", err)
        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 := conn.begintx(ctx, &sql.txoptions{isolation: isolation, readonly: false})
    if err != nil {
        fmt.printf("begintx failed, error type: %s\n", reflect.typeof(err))
        log.printf("begintx failed, error: %v", err)
        return err
    }

    // now, ensure that the transaction is either rolled back or committed before
    // the function ends
    defer func() {
        if p := recover(); p != nil {
            tx.rollback()
            panic(p)
        } else if err != nil {
            log.printf("an error occurred: %v", err)
            if err := tx.rollback(); err != nil {
                log.printf("rollback failed, error: %v", err)
            }
        } else {
            if err := tx.commit(); err != nil {
                log.printf("commit failed, error: %v", err)
            }
        }
    }()

    // finally, run the function and return the result
    err = txfunc(tx)
    return err
}

这在大多数情况下都很有效。然而,我注意到由于超时、无服务器暂停、超出 io 限制等原因而发生的许多错误;如:

Login error: mssql: Database 'my-db' on server 'myserver.database.windows.net' is not currently available. Please retry the connection later. If the problem persists, contact customer support, and provide them the session tracing ID of '{SOME_GUID}'.

我想通过某种退避来处理这些问题,而不是仅仅失败。然而,要做到这一点,我需要能够以某种方式解释错误。但是,返回的错误的类型均为 *errors.errorstring。我尝试使用 as(error, interface{}) 来检查错误是否是 mssql.error ,但事实并非如此,所以我不太确定如何处理它。如何确定这些错误的原因是什么?


解决方案


您可以尝试使用类型断言将遇到的错误转换为此处定义的错误 as defined here

err:=//your operation that returns an error
//check and convert the error to a mssql error with more context
if mssqlerr,ok:=err.(mssql.error);ok{
   if mssqlerr.sqlerrornumber() == someretryableerrorcode{
      //custom retry logic...
   }
}

或者,您可以使用错误。如下

var msSQLErr mssql.Error
if errors.As(err,&msSQLErr){
   if msSQLErr.SQLErrorNumber() == someRetryableErrorCode{
      //custom retry logic...
   }
}

更新:不幸的是,该库似乎没有包装底层错误,因此您不能使用错误。is或错误。至于某些错误,例如网络相关错误,因此您必须使用 strings.contains 之类的内容自己匹配错误字符串

以上就是《解决 Azure SQL 错误》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>