When I upgrade to V2, the db.Close method does not exist.

defer db.close()

So how to Close the connection of database in V2.

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.

Comment From: jinzhu

Close is one of the most misused methods, most application don't need it, be sure you really need it.

If yes, use it like:

sqlDB, err := DB.DB()
sqlDB.Close()

Comment From: mistricky

@jinzhu Thx for ur reply, but I get the following error without db.Close

sorry, too many clients already (SQLSTATE 53300))

How do I solve it?

Comment From: jinzhu

Don't Open db connection for each request, initialize a global DB and use it for all requests, also check out http://v2.gorm.io/docs/connecting_to_the_database.html#Connection-Pool

Comment From: mistricky

@jinzhu Okaaay, thx I guess I know what should I do :).

Comment From: rebootcode

Close is one of the most misused methods, most application don't need it, be sure you really need it.

Why is it the most misused method? If an application needs to forcibly close or if the application crashes or somehow stops for any reason - cleanup is the first thing that comes in mind and what are your reason to oppose it?

Comment From: jinzhu

Close is one of the most misused methods, most application don't need it, be sure you really need it.

Why is it the most misused method? If an application needs to forcibly close or if the application crashes or somehow stops for any reason - cleanup is the first thing that comes in mind and what are your reason to oppose it?

it is not necessary to close when crash, and you can still Close DB connection in V2

Comment From: tobyartisan

Close is one of the most misused methods, most application don't need it, be sure you really need it.

Why is it the most misused method? If an application needs to forcibly close or if the application crashes or somehow stops for any reason - cleanup is the first thing that comes in mind and what are your reason to oppose it?

it is not necessary to close when crash, and you can still Close DB connection in V2

@jinzhu or anyone, can you clarify when you should and when you should not close the database connection in GORM v2?

And why do you say that you do not need to close it? Will GORM v2 automatically close all open connections on both a clean application shutdown and when the application crashes?

These details could be a good addition to the documentation page Connecting to a Database.

Comment From: anuj-kumar

Close is one of the most misused methods, most application don't need it, be sure you really need it.

Why is it the most misused method? If an application needs to forcibly close or if the application crashes or somehow stops for any reason - cleanup is the first thing that comes in mind and what are your reason to oppose it?

it is not necessary to close when crash, and you can still Close DB connection in V2

@jinzhu or anyone, can you clarify when you should and when you should not close the database connection in GORM v2?

And why do you say that you do not need to close it? Will GORM v2 automatically close all open connections on both a clean application shutdown and when the application crashes?

These details could be a good addition to the documentation page Connecting to a Database.

@jinzhu To put in other words, on application crash, if close() isn't called explicitly in v2, will gorm be able to handle the graceful shutdown, i.e wait for all ongoing transactions to finish?

Comment From: gosthell

Close is one of the most misused methods, most application don't need it, be sure you really need it.

Why is it the most misused method? If an application needs to forcibly close or if the application crashes or somehow stops for any reason - cleanup is the first thing that comes in mind and what are your reason to oppose it?

it is not necessary to close when crash, and you can still Close DB connection in V2

@jinzhu or anyone, can you clarify when you should and when you should not close the database connection in GORM v2? And why do you say that you do not need to close it? Will GORM v2 automatically close all open connections on both a clean application shutdown and when the application crashes? These details could be a good addition to the documentation page Connecting to a Database.

@jinzhu To put in other words, on application crash, if close() isn't called explicitly in v2, will gorm be able to handle the graceful shutdown, i.e wait for all ongoing transactions to finish?

I don’t think it does, I’ve seen db.Transaction that got started but didn’t get finalized due to a shutdown.