Ruby Sequel + PG: Is (DB.disconnect + signal/trap/exit) necessary in Sequel-backed apps?

StackOverflow https://stackoverflow.com/questions/18348172

  •  25-06-2022
  •  | 
  •  

Question

Coming from other environments (eg nodejs), it was necessary to close the db connections after the server finishes and closes. I've searched Sequel's source + online Sequal examples. I've seen .disconnect mentioned mostly with just forks and threads.

Is is necessary to manually call DB.disconnect in a signal trap at app exit? Or are the connections closed automatically?

I'm only running a simple Rack app, w/o app preloading in Unicorn, only Postgresql connections.

Was it helpful?

Solution

Ruby will automatically close the database connection sockets on process shutdown, so you don't need to call DB.disconnect manually (though you can if you want to).

OTHER TIPS

When your PHP script is finished, PHP will automatically perform garbage collection on the objects and resources you used previously. As result of this, plus the fact that most scripts are over in less than a tenth of a second, it is generally not necessary to explicitly disconnect from your MySQL server or to hand-free the space allocated to your SQL results.

if you wish to know:

$db->close(); 

which closes the current connection to the database (if used on your database variable)

Only close the connection to the database entirely if you are done with your SQL Connection for that script

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top