On Friday, February 1, 2013 1:04:06 PM UTC-8, Jeremy Evans wrote:
> On Friday, February 1, 2013 11:59:09 AM UTC-8, Alex Vinnik wrote:
>>
>> Hi all.
>>
>> Looks like we run into an issue with postgres adapter
>> when connection_validator extension is used. The problem is simple. DB
>> connection is NOT lazy anymore when connection_validator is activated.
>> Sequel version is 3.43.0. Consider this snippet
>>
>> #!/usr/bin/env ruby
>>
>> require "sequel"
>> require 'logging'
>>
>> logger = Logging.logger(STDOUT)
>> logger.level = :debug
>>
>> logger.info 'Starting...'
>>
>> DB = Sequel.connect(:adapter => 'postgres',
>> :username => 'whatever', :password => 'whatever'',
>> :database => 'whatever'', :host => 'localhost')
>>
>> logger.info DB.inspect
>> DB.loggers << logger
>>
>> DB.extension(:connection_validator)
>>
>> DB.loggers << logger
>> puts `sudo netstat -ntp | egrep '5432.*ESTABLISHED.*#{Process.pid}'`
>>
>> You can run it and see that DB connection gets established even no SQL is
>> fired by the app. It is certainly not a lazy connection.
>>
>> I, [2013-02-01T13:45:49.668455 #19887] INFO : Starting...
>> I, [2013-02-01T13:45:49.729210 #19887] INFO :
>> #<Sequel::Postgres::Database: {:adapter=>"postgres",
>> :username=>"transporter", :password=>"2ownTransporter",
>> :database=>"transporter", :host=>"localhost"}>
>> I, [2013-02-01T13:45:49.757414 #19887] INFO : (0.000299s) SET
>> standard_conforming_strings = ON
>> I, [2013-02-01T13:45:49.757832 #19887] INFO : (0.000280s) SET
>> client_min_messages = 'WARNING'
>> I, [2013-02-01T13:45:49.758023 #19887] INFO : (0.000129s) SET DateStyle
>> = 'ISO'
>> tcp6 0 0 ::1:48713 ::1:5432
>> ESTABLISHED 19887/ruby
>> I, [2013-02-01T13:45:49.831646 #19887] INFO : Finished...
>>
>> After some digging in the code and seems like calling server_version in
>> select_clause_methods triggers DB connection.
>>
>> https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/shared/postgres.rb#L1303
>> here is server_version code...
>>
>> https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/shared/postgres.rb#L453
>>
>> Just to compare... connecting to MS SQL does have lazy connection. I
>> totally understand rationale behind this code and the need to have PG
>> version handy in order to prepare statements but it is sort inconsistent
>> with other adapters. May be to document this PG connection_validator
>> "feature" because I don't think it easily fixable at the moment. What do
>> you think?
>>
>
> Your analysis appears to be correct, but could you explain why this is an
> issue? The point at which Sequel makes a connection is an implementation
> detail, so I'm not sure why we should document it. There are other
> database extensions that cause a connection (the pg_hstore extension, for
> example), so the connection_validator extension is not alone in this.
>
> The fact that Sequel usually creates connections lazily is itself an
> implementation detail. It's definitely possible that a connection attempt
> will automatically be made on Sequel::Database initialization in the
> future, in order to fail fast.
>
> Thanks,
> Jeremy
>
When Sequel connects to the database is not an implementation detail
because it has side effects.
For example, we're using Sequel in a small Rails app. We're setting up the
connection_validator in an initializer, so that it's configured for the
life of the app. We also have Rake tasks that drop the database, create it,
and apply migrations to it. This task happens to need to load the Rails
environment before running, so the connection_validator is set up. But the
drop of the database fails, because of the database connection that is
outstanding:
dropdb: database removal failed: ERROR: database "bbox_test" is being accessed
by other users
DETAIL: There are 1 other session(s) using the database.
We can have code run differently (e.g., not depend on the Rails environment),
but because we have to change around our code to work properly, I think it's
incorrect to say that "The point at which Sequel makes a connection is an
implementation detail". For reasons like ours, when Sequel makes a connection
needs to be well-documented and predictable, *so that* clients can write their
code properly.
Sincerely,
Ed
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.