How to configure every (postgresql) db connection?

I would like to set some db session variables specific to my
application on the database connection that rails uses. They can be
set by issuing an SQL command like “SET statement_timeout = 1000”
once - it will be in effect for every subsequent SQL statement that
uses this connection.

So I tried to put the following into an initializer:

ActiveRecord::Base.connection.execute(“SET statement_timeout = 1000”)

but this does not have the desired effect - it is not set for all
connections. I assume this has to do with the connection pool rails
uses - the code above just sets it for a single one.

The PostgreSQLAdapter issues some SET statements itself for each new
connection - is there a good way to add your own configuration
statements, either specific to the PostgreSQLAdapter or in a general
way?

Til

In SqlServer, in the context of a Model, you can do this:

self.connection.instance_variable_get

(:@connection).handle.instance_variable_get(:@handle).setproperty
(‘CommandTimeout’, 300)

Perhaps there is something similar for Postgres.

At Thu, 15 Oct 2009 13:57:34 -0700 (PDT),
jemminger [email protected] wrote:

In SqlServer, in the context of a Model, you can do this:

self.connection.instance_variable_get

(:@connection).handle.instance_variable_get(:@handle).setproperty
(‘CommandTimeout’, 300)

Perhaps there is something similar for Postgres.

I used to do this in the context of a model which I assume has a
similar effect:

self.connection.execute(“SET statement_timeout 300”)

but I doubt that this is reliably configuring all connections of the
AR connection pool, and I’d rather do it in an app-wide configuration
file and not in a model.

Til

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs