# One connection per session

**URL:** <https://www.ruby-forum.com/t/one-connection-per-session/81483>\
**Category:** Rails\
**Created:** [January 9, 2007, 9:57am UTC](https://www.ruby-forum.com/t/one-connection-per-session/81483 "2007-01-09T09:57:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HSSSSkon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/43a26b/32.png) [@HSSSSkon](https://www.ruby-forum.com/u/HSSSSkon)\
**Post date:** [January 9, 2007, 9:57am UTC](https://www.ruby-forum.com/t/one-connection-per-session/81483/1 "2007-01-09T09:57:47Z")

</div>

Hi all,

I’m new to rails and started building an application on top of my  
legacy database. I trying to get a login/out system work and I’m using  
login generator as a basis. My problem is: The user that logs in to the  
app must also connect to the database (with his own username/password).  
This means that I need to have a connection per session. Does anyone  
have some hint on how to do this without needing to reconnect for each  
request?

Regards,

Håkon

---

<div class="post-metadata">

**Author:** ![HSSSSkon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/43a26b/32.png) [@HSSSSkon](https://www.ruby-forum.com/u/HSSSSkon)\
**Post date:** [January 9, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/one-connection-per-session/81483/2 "2007-01-09T13:19:48Z")

</div>

put this in controllers/application.rb

```
    before_filter :set_current_user

```

def set\_current\_user  
User.current\_user = session[‘user’]  
end

in the user model add this single line:

```
    cattr_accessor :current_user

```

…that should do the trick.

s

---

<div class="post-metadata">

**Author:** ![HSSSSkon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/43a26b/32.png) [@HSSSSkon](https://www.ruby-forum.com/u/HSSSSkon)\
**Post date:** [January 10, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/one-connection-per-session/81483/3 "2007-01-10T16:02:07Z")

</div>

I’m not sure I understand you correctly. Can I store the connection in  
the session? I thought I needed to do a  
ActiveRecord::Base.establish\_connection per session if each user should  
have their own connection to the database.

Håkon

---

<div class="post-metadata">

**Author:** ![HSSSSkon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/43a26b/32.png) [@HSSSSkon](https://www.ruby-forum.com/u/HSSSSkon)\
**Post date:** [January 10, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/one-connection-per-session/81483/4 "2007-01-10T19:44:10Z")

</div>

I think you will have to reconnect the user to the database for each  
controller action he invokes.  
First insert users database info into config/database.yml file. Then  
store the name of the database into session when a user logs in. Then  
in the application controller do:

```
before_filter :db_connect

```

private  
def db\_connect  
if session[‘database’]  
ActiveRecord::Base.establish\_connection(session[‘database’])  
end  
end

and in your login controller:

def your\_login\_method  
# figure out database name  
# …  
if authenticate(login, password)  
session[‘database’] = database\_name  
redirect\_to :somewhere  
end  
end
