Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to close connection in V2 #3145

Closed
mistricky opened this issue Jul 14, 2020 · 9 comments
Closed

How to close connection in V2 #3145

mistricky opened this issue Jul 14, 2020 · 9 comments
Assignees
Labels

Comments

@mistricky
Copy link

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.

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jul 14, 2020
@github-actions
Copy link

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.

@jinzhu
Copy link
Member

jinzhu commented Jul 14, 2020

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()

@jinzhu jinzhu closed this as completed Jul 14, 2020
@mistricky
Copy link
Author

@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?

@jinzhu
Copy link
Member

jinzhu commented Jul 15, 2020

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

@mistricky
Copy link
Author

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

@rebootcode
Copy link

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?

@jinzhu
Copy link
Member

jinzhu commented Aug 28, 2020

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

@tobyartisan
Copy link

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.

MichaelYcCho added a commit to MichaelYcCho/go-practice that referenced this issue Oct 26, 2022
- it is not necessary to close when crash, and you can still Close DB connection in V2
- go-gorm/gorm#3145
@anuj-kumar
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants