# ActiveRecord without Rails: necessary to close connection?

**URL:** <https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555>\
**Category:** Ruby\
**Created:** [April 15, 2009, 7:45am UTC](https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555 "2009-04-15T07:45:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [April 15, 2009, 7:45am UTC](https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555/1 "2009-04-15T07:45:39Z")

</div>

Hello everyone,

This is the first time I want to use ActiveRecord outside Rails. This is  
my (simplified) code:

ActiveRecord::Base.establish\_connection(  
:adapter =\> ‘mysql’,  
:host =\> ‘localhost’,  
:username =\> ‘username’,  
:password =\> ‘password’,  
:database =\> ‘database’)

class Rate \< ActiveRecord::Base  
end

r = Rate.find(:first)  
r.value = new\_rate  
r.save!

This works fine, but I was wondering if I have to close any database  
connections at the end of my script (by using remove\_connection??)? Or  
is does AR take care of this automatically?

Thanks in advance.

Mischa.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [April 15, 2009, 11:24am UTC](https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555/2 "2009-04-15T11:24:44Z")

</div>

Mischa B. wrote:

> This works fine, but I was wondering if I have to close any database  
> connections at the end of my script (by using remove\_connection??)? Or  
> is does AR take care of this automatically?

Don’t bother. The operating system will close all open filehandles when  
the process terminates.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [April 15, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555/3 "2009-04-15T12:07:37Z")

</div>

Brian C. wrote:

> Don’t bother. The operating system will close all open filehandles when  
> the process terminates.

Thanks. With the same script I have another issue. When I execute the  
script from the command line it runs flawlessly, but when it’s executed  
from cron I get the following error in my script: no such file to load  
– mysql

The line where the error occurs is:  
r = Rate.find(:first)

My crontab looks like this:

- 
  - 
    - 
      - 
        - /usr/local/bin/ruby  
/home/mischa/railsapps/aexscript/periodic/get\_rate.rb production \>\>  
/home/mischa/railsapps/aexscript/periodic/get\_rate.log

So I use full path to everything.

Any ideas??

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [April 15, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/activerecord-without-rails-necessary-to-close-connection/165555/4 "2009-04-15T14:22:28Z")

</div>

I got it working. It turned out the mysql gem wasn’t in the expected gem  
folder. Why everything works smoothly from the command line is beyond  
me, but it works 🙂
