jessepinho
jessepinho

Reputation: 5772

Do Sequel gem connections need to be manually closed when using Net::SSH::Gateway?

I am using the Sequel gem to connect to a database. The DB server is remote, though, so I have to log in over SSH first.

My Ruby script is set up to, every five minutes, SSH in, ping the database, then close the SSH connection. (SSH is handled by Net::SSH::Gateway.)

But I recently got a "too many connections" error on MySQL. When checking the MySQL process list, I found a bunch of sleeping connections from the Ruby script. So I added a db.disconnect line to my script to disconnect from the database before closing the SSH connection, and that seemed to fix it.

My question is, aren't database connections closed automatically? Why were there a bunch of sleeping SQL connections?

Upvotes: 0

Views: 667

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

It's hard to say exactly what is going on since you didn't provide a link to the script you are using. Based on the limited information provided, you are probably creating new Sequel::Database objects every five minutes. A Sequel::Database object is designed to provide persistent connections to the database, and is usually created during application start and stored in a constant.

In general, you should create a single Sequel::Database object and just send a simple query every 5 minutes. Alternatively, you should provide a block to the method that creates your Sequel::Database object so that it is automatically closed when the block returns.

Upvotes: 2

Related Questions