I am using Sequel on an application that is composed of about half a dozen 
sinatra services. These services use Sequel::Model.db to connect to 
postgres via some shared logic that is installed as a gem 
'sequel_connection'. That connection logic is pretty simple. Here it is.

module Persistence
  module Sequel
    require 'sequel'

    module Connection
      class << self
        def _database
          @_database ||= ::Sequel.connect(connection_string)
        end

        def database
          retries = 0
          begin
            retries += 1
            _database
          rescue ::Sequel::DatabaseDisconnectError
            retry if retries > 10
          end
        end

        def connection_string
          ["postgres://"].tap do |s|
            s << config.username        if config.username
            s << ":#{config.password}" if config.password
            s << "@#{config.host}"      if config.host
            s << ":#{config.port}"         if config.port
            s << "/#{config.database}" if config.database
          end.join("")
        end

        def config
          Config.for(ENV['RACK_ENV'])
        end
      end

      require 'ostruct'
      require 'yaml'

      class Config < OpenStruct
        class << self
          attr_accessor :config_file

          def for(env)
            new(config[env])
          end

          def config
            YAML.load_file("#{config_file}")
          end
        end
      end
    end
  end
end

The services set the connections in an init script that get required in the 
sinatra class as such:

require 'sequel_connection'

Persistence::Sequel::Connection::Config.config_file = 
File.join('config/database.yml')
Sequel::Model.db = Persistence::Sequel::Connection.database

I am experiencing trouble with the connections dropping and not being 
re-establised. The air brake error is this: 

Sequel::DatabaseDisconnectError: PG::Error: SSL error: sslv3 alert bad 
record mac

I assume there is some important thread safety stuff here that I am 
glossing over due to my inexperience with this kind of setup. What's the 
best way to deal with this? Is sharing the connection the wrong approach? 
Should I nix the gem and have this logic in each service? Would wrapping it 
in a Thread.new block work? Or is there something else I'm missing like 
disconnecting/reconnecting properly?

Thanks for the help,
-Dave

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/395OghXYCRMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to