Robustus halfway creating an instance variable will create a connection for each instance of your class.
You want to create a thread-safe api data warehouse that will use the mutex to handle access to your connection objects (s). This is a control mechanism that uses ActiveRecord in rails.
I created a ruby ββstone called "q", which provides a simple mechanism for this. The Pearl is here: https://github.com/jacobsimeon/q
install like this git clone https://github.com/jacobsimeon/q
then add this to any file that initializes your application: require 'q/resourcepool'
then create a data warehouse during initialization:
class DataConnection < ResourcePool def create_resource PGConn.connect(@config) end end @datasource = ResourcePool.new(your_connection_info_here)
then you can use @datasource to execute commands against your database
post '/send' do @datasource.exec(standard_postgres_params) end
Contact me on github or twitter (@jacobsimeon on both) if you are interested in following this route.
source share