Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to close DB connection before closing application in Go?

Tags:

sql

database

go

In Go, when using a SQL database, does one need to close the DB (db.Close) before closing an application? Will the DB automatically detect that the connection has died?

like image 444
Robert Zaremba Avatar asked Dec 09 '15 09:12

Robert Zaremba


People also ask

Should I close database connection?

If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.

What happens if you don't close database connection?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.

Should a database connection stay open?

While the specific details matter in general there is nothing wrong with keeping connections open for long durations.

Should I close DB connection after query?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Why should you always close database connections explicitly?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Should I close all unnecessary database connections inside of PHP scripts?

I wonder if I should close any unnecessary database connection inside of my PHP scripts. I am aware of the fact that database connections are closed implicitly when the block stops executing and 'manually' closing the connections could kinda bloat the codebase with unnecessary code.

How do I Close a connection in a pool?

We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#, or a Using statement in Visual Basic.

Should I leave the database open in a busy app?

It's not uncommon to have thousands or hundreds of thousands of calls to a database in a busy app, and sooner or later the DB performance will kill the app's performance. It's really just common sense. If you have a valid reason to keep it open, then do so. If not, close it as soon as you're done with it.


1 Answers

DB will do its best to detect but with no luck, it may not be able to detect. Better to release what is acquired as soon as possible.

send() system call will wait for TCP connection to send data but client won't receive anything.

  1. Power failure, network issue or bare exit happened without properly releasing resources. TCP keepalive mechanism will kick in and try to detect that connection is dead.

  2. Client is paused and doesn't receive any data, in this case send() will block.

As a result, it may prevent

  1. Graceful shutdown of cluster.
  2. Advancing event horizon if it was holding exclusive locks as a part of transaction such as auto vacuum in postgresql.

Server keepalive config could be shortened to detect it earlier. (For example, ~2h 12m default in postgresql will be very long according to workload).

There may be a hard limit on max open connections, until detection, some connections will be zombie (there, unusable but decreases limit).

like image 105
ferhat Avatar answered Nov 06 '22 19:11

ferhat