Describe the feature

GORM should try to reconnect to a database when connection is lost (e.g. Network error, DB restart)

After the DB restarts, the gorm application usually isnt able to recover and get back to normal operations. That should be solved.

To reproduce the behaviour that I want to avoid - working with Postgres, I get:

  • start your DB
  • run you gorm app
  • use your gorm app
  • shutdown DB
  • try using gorm app
  • see it failing with db connection error: "FATAL: terminating connection due to administrator command (SQLSTATE 57P01)"
  • restart DB
  • Try using App
  • See it still getting the same error

Expecation would be that the application reconnects to the DB and continue to work without a restart.

Motivation

GORM applications without DB connection do not work very well.

Related Issues

Multiple issues have been created with that, but all have been closed, but without mentioning a reason or a workaround:

https://github.com/go-gorm/gorm/issues?q=reconnect

There are also some chat messages about it: https://gitter.im/jinzhu/gorm?at=5a59df94ba39a53f1a0e36f2 - but it's not clear to me why gorm doesn't come out of the box with that reconnect behaviour - are there important downsides to configure it by default to reconnect automatically?

I'd be happy to try find a solution for that thing or add some documentation with a workaround if I get some guidance for more experienced gorm developers about how to do it best... maybe there is a reason why it has not been implemented before, or maybe I am overseeing something that makes it not a useful feature.

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days

Comment From: sincerefly

I need this feature

Comment From: borzovplus

any updates?

Comment From: flaxinger

I just started googling this topic and landed on this stale issue. Any hints to prebuilt solutions would be of much help (if there are any)

Comment From: nk915

I need this feature too.

Comment From: 2015WUJI01

Is there any solution now?

Comment From: Stogas

I am unable to replicate this in a Gin API application, i.e. it recovers automatically with a subsequent request.

has this feature been implemented?

Comment From: lyn1505

gorm automatically reconnects for me. I'm using gorm version v1.25.12. It would be helpful if @jinzhu can confirm this

Comment From: dsychin

I am running into the same issue on v1.25.12. Code is set up to connect using an existing sql connection. Previously I had gorm v1.24.2 with gorm.io/driver/mysql 1.4.4 and it would reconnect automatically but now once it fails it will never reconnect.

{"time":"2024-11-05T10:39:56.01449173+09:00","level":"ERROR","prefix":"-","file":"gormlogger.go","line":"44","message":"failed to initialize database, got error %v[dial tcp [::1]:3307: connect: connection refused]"}
{"time":"2024-11-05T10:40:04.352576548+09:00","level":"ERROR","prefix":"-","file":"gormlogger.go","line":"44","message":"failed to initialize database, got error %v[sql: database is closed]"}
{"time":"2024-11-05T10:40:12.343878406+09:00","level":"ERROR","prefix":"-","file":"gormlogger.go","line":"44","message":"failed to initialize database, got error %v[sql: database is closed]"}

With the older version, it never shows "database is closed" and would recover when the database is back.

{"time":"2024-11-05T10:54:27.88827598+09:00","level":"ERROR","prefix":"-","file":"gormlogger.go","line":"44","message":"failed to initialize database, got error %v[dial tcp
[::1]:3307: connect: connection refused]"}
{"time":"2024-11-05T10:54:38.721245664+09:00","level":"ERROR","prefix":"-","file":"gormlogger.go","line":"44","message":"failed to initialize database, got error %v[dial tcp
 [::1]:3307: connect: connection refused]"}
{"time":"2024-11-05T10:54:48.710993513+09:00","id":"","remote_ip":"127.0.0.1","host":"localhost:8080","method":"GET","uri":"/api/auth","user_agent":"
Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0","status":307,"error":"","stackTrace":"","latency":14063159,"latency_human":"14.063159ms","bytes_in":0
,"bytes_out":0}

It seem to have stopped working from version v1.25. I have tested it with v1.24.6 and it works.

Comment From: jinzhu

gorm automatically reconnects for me. I'm using gorm version v1.25.12. It would be helpful if @jinzhu can confirm this

Reference in

Yes, Go manages the connection pool and will handle reconnections after a failure.

Comment From: nk915

@jinzhu Thank you.

Comment From: dsychin

@jinzhu I believe the reconnection feature is broken when using an existing database connection.

The issue seems to be caused by this change released with v1.25.

When using gorm.Open with a dsn, this is not an issue because a new connection would be created, however when using an existing database connection, the closed connection is reused which would fail.

I don't think this is the intended behaviour?

Comment From: jinzhu

@jinzhu I believe the reconnection feature is broken when using an existing database connection.

The issue seems to be caused by this change released with v1.25.

When using gorm.Open with a dsn, this is not an issue because a new connection would be created, however when using an existing database connection, the closed connection is reused which would fail.

I don't think this is the intended behaviour?

This doesn’t make any difference, GORM handles it the same way internally.