How can I detect a connection failure in gorm?

huangapple go评论91阅读模式
英文:

How can I detect a connection failure in gorm?

问题

我正在使用gorm ORM在Go中编写一个小而简单的Web应用程序。

由于数据库可能与Web应用程序独立失败,我希望能够识别与此情况对应的错误,以便在不重新启动Web应用程序的情况下重新连接到数据库。

示例:

考虑以下代码:

var mrs MyRowStruct
db := myDB.Model(MyRowStruct{}).Where("column_name = ?", value).First(&mrs)
return &mrs, db.Error
  1. 如果db.Error != nil,如何以编程方式确定错误是否源于数据库连接问题?

  2. 根据我的阅读,我了解到gorm.DB不代表一个连接,所以如果数据库连接失败,我是否需要担心重新连接或重新发出对gorm.Open的调用?

  3. 在Go中处理数据库故障的常见模式有哪些?

英文:

I'm writing a small, simple web app in go using the gorm ORM.

Since the database can fail independently of the web application, I'd like to be able to identify errors that correspond to this case so that I can reconnect to my database without restarting the web application.

Motivating example:

Consider the following code:

var mrs MyRowStruct
db := myDB.Model(MyRowStruct{}).Where("column_name = ?", value).First(&mrs)
return &mrs, db.Error
  1. In the event that db.Error != nil, how can I programmatically determine if the error stems from a database connection problem?

  2. From my reading, I understand that gorm.DB does not represent a connection, so do I even have to worry about reconnecting or re-issuing a call to gorm.Open if a database connection fails?

  3. Are there any common patterns for handling database failures in Go?

答案1

得分: 7

Gorm似乎会吞噬数据库驱动程序的错误,只会发出自己分类的错误类型(参见gorm/errors.go)。目前似乎没有报告连接错误。

考虑提交一个问题或拉取请求,直接暴露数据库驱动程序的错误。

[原文]

尝试按照gorm自述文件中的“错误处理”部分的建议,检查db.Error的运行时类型。

假设它是由数据库驱动程序返回的错误类型,你可能可以获得一个指示连接错误的特定代码。例如,如果你正在使用通过pq库连接的PostgreSQL,你可以尝试像这样的代码:

import "github.com/lib/pq"

// ...

if db.Error != nil {
  pqerr, ok := err.(*pq.Error)
  if ok && pqerr.Code[0:2] == "08" {
    // PostgreSQL的“连接异常”是类别“08”
    // http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html#ERRCODES-TABLE
    // 处理连接错误...
  } else {
    // 处理非pg错误或非连接错误...
  }
}
英文:

Gorm appears to swallow database driver errors and emit only it's own classification of error types (see gorm/errors.go). Connection errors do not currently appear to be reported.

Consider submitting an issue or pull request to expose the database driver error directly.

[Original]

Try inspecting the runtime type of db.Error per the advice in the gorm readme "Error Handling" section.

Assuming it's an error type returned by your database driver you can likely get a specific code that indicates connection errors. For example, if you're using PostgreSQL via the pq library then you might try something like this:

import "github.com/lib/pq"

// ...

if db.Error != nil {
  pqerr, ok := err.(*pq.Error)
  if ok && pqerr.Code[0:2] == "08" {
    // PostgreSQL "Connection Exceptions" are class "08"
    // http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html#ERRCODES-TABLE
    // Do something for connection errors...
  } else {
    // Do something else with non-pg error or non-connection error...
  }
}

huangapple
  • 本文由 发表于 2015年3月26日 02:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/29263203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定