UPDATE: I tried changing the storage of the PostgreSQL connection object from a constant POSTGRES to a class variable ##pg. That didn't fix the issue. Then, I tried changing it to a global variable $pg. That seems to have worked! I still would like to eventually implement database connection pooling, however, so that I can have a pool of up to 5 (or so) database connections handling requests instead of just one global connection for all requests. END UPDATE
I'm hosting the Acani Chats REST Server on Heroku.
The first request works OK, but subsequent requests fail to connect to the PostgreSQL database.
I get the following errors:
Rack app error: #<PG::UnableToSend: SSL error: decryption failed or bad record mac>
Rack app error: #<PG::UnableToSend: no connection to the server>
What's going on?
In /config/application.rb, I define the constant POSTGRES to be the PostgreSQL connection object.
Should I be using a global or class variable instead of a constant to hold onto the connection instance?
In /config/routes.rb, I define the Rack call method.
I want to learn how to implement database connection pooling in Ruby for Rack with Puma and PostgreSQL.
Puma is threaded so you need a thread safe pool of connections to PostgreSQL, otherwise concurrent requests will all use the same connection concurrently, which is unexpected.
Please have a look to the connection_pool gem. It should help.
Related
I have a rack puma server hosted, and there are multiple HTTP request-response cycles. I'm querying the data from MongoDB. Quite often the server hangs, and if I press Ctrl+C it resumes , if i don't then after a couple of minutes I get the following error:
C:/Rubies/Ruby193/lib/ruby/gems/1.9.1/gems/puma-1.6.3/lib/puma/client.rb:127:in 'readpartial': An existing connection was forcibly closed by the remote host. (E rrno::ECONNRESET)
I have pretty good exception handling and loggers, but this is not caught anywhere. I have to restart the server again to proceed.
Please suggest.
Using activerecord outside rails, may I be confident all the connection drudgework is performed behind the curtains the same as inside rails?
In rails, activerecord does a great job establishing a connection pool and activating or closing connection as needed.
If I have a ruby daemon which calls a class file with:
ActiveRecord::Base.establish_connection(:production)
# more active_record tasks
can I assume on the following calls a connection from a pool is used?
Yup, calling ActiveRecord::Base.establish_connection will create a connection pool according to: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb#L842
If I were using ActiveRecord, my puma config would establish the database connection as described in Heroku's guide Deploying Rails Applications with the Puma Web Server.
on_worker_boot do
ActiveRecord::Base.establish_connection
end
However, I am using ROM (Ruby Object Mapper).
I tried omitting the on_worker_boot block, but (predictably) the database connection is either not established, or is not established correctly, and the following error is raised.
PG::ConnectionBad: PQconsumeInput() SSL error: decryption failed or bad record mac
I've read the ROM Setup Guide but did not see anything relevant.
How do I establish the ROM connection in puma?
The solution seems to be to disconnect the gateway connection.
on_worker_boot do
ROM.env.gateways[:default].connection.disconnect
end
Read the docs, searched the stack, but I can't find a very simple
function: How do I check if the connection has been established or not?
establishing DB connection with
db = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
From time to time I forget to launch the mongodb database and then the driver tries to connect and well... does not say's a lot about the connection itself.
Is there some kind of .isConnected? method or anything to find
the connection status of current Mongo::Client instance?
Thanks in advance
Version 1.x of the Mongo ruby driver did expose a connected? method on Mongo::Connection, but it's been moved in v2 to live on Mongo::Server, however its meaning has changed somewhat.
Here's an example of using this method:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
#=> #<Mongo::Client:0x70279909414900 cluster=localhost:27017>
mongo.cluster.servers.first.connected?
#=> true
In v2 of the Mongo ruby driver, it will raise an exception when trying to use an invalid connection, for example, Mongo::Error::NoServerAvailable, based on the connection parameters about timeouts. For your purposes of simply checking whether the connection has been established, maybe something like:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
mongo.list_databases => # raise error or return an array
I have multiple database connection in my Sinatra Application. It should switch the database intelligently according to the params.
For example:
get '/:project/details' do
...
end
It should connect to the database 'project1' if the url is 0.0.0.0:3000/project1/details, 'project2' if the url is 0.0.0.0:3000/project2/details, and so on.
I am using active_record for database connection.
I want to keep the connection in a pool, so that I can use the same connection if more than one hit to the same project. And shift the connection in the pool according to the project with out establishing a new connection for each each and every hit.
How to implement this?