# Ruby on Rails with Oracle

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119>\
**Category:** Rails\
**Created:** [August 11, 2006, 1:10am UTC](https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119 "2006-08-11T01:10:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [August 11, 2006, 1:10am UTC](https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119/1 "2006-08-11T01:10:55Z")

</div>

Hello everyone,

I am trying to convince my colleagues to convert to Ruby on Rails,  
however I am up against the following problem:

Ruby on rails seems to be primarily designed a single database doing a  
small number of queries. While trying to process a webpage that does  
thousands of sql queries on Oracle, I get the message that I have used  
up too many cursors.

In Java, I can use stmt.close() to close all the cursors on a sql  
statement. In Ruby on Rails the sql “statement” and the sql “result set”  
are hidden inside ActiveRecord’s methods. Therefore I can’t close the  
statement.

As a work around I can close the database connection, however it takes  
too long to open another connection thousands of times.

How can I solve this problem?

Thanks

- Jon

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [August 11, 2006, 2:53am UTC](https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119/2 "2006-08-11T02:53:33Z")

</div>

take a look to this link  
[http://vsbabu.org/mt/archives/2005/07/19/oracle\_with\_active\_record.html](http://vsbabu.org/mt/archives/2005/07/19/oracle_with_active_record.html)

Jon Leung wrote:

> Hello everyone,
> 
> I am trying to convince my colleagues to convert to Ruby on Rails,  
> however I am up against the following problem:
> 
> Ruby on rails seems to be primarily designed a single database doing a  
> small number of queries. While trying to process a webpage that does  
> thousands of sql queries on Oracle, I get the message that I have used  
> up too many cursors.
> 
> In Java, I can use stmt.close() to close all the cursors on a sql  
> statement. In Ruby on Rails the sql “statement” and the sql “result set”  
> are hidden inside ActiveRecord’s methods. Therefore I can’t close the  
> statement.
> 
> As a work around I can close the database connection, however it takes  
> too long to open another connection thousands of times.
> 
> How can I solve this problem?
> 
> Thanks
> 
> - Jon

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119/3 "2006-08-15T14:16:41Z")

</div>

If I wer your Oracle DBA, I would be very concerned that you’re running  
an  
app that has a web page which runs thousands of SQL queries. I would  
expect  
you to run out of cursors. Are you able to take advantage of eager  
loading?  
I don’t know if this is so much a Rails issue as an issue with your app.  
(Sounds like the n+1 problem)

A quick glance at the source for the Oracle connection adapter for  
ActiveRecord shows that cursors are being created and closed. If you  
are  
sure that this isn’t happening on your database then you might want to  
consider filing a ticket.

C:\ruby\lib\ruby\gems\1.8\gems\activerecord-  
1.14.4\lib\active\_record\connection\_adapters\oracle\_adapter.rb line 472  
def select(sql, name = nil)  
cursor = execute(sql, name)  
cols = cursor.get\_col\_names.map { |x| oracle\_downcase(x) }  
rows = []

```
      while row = cursor.fetch
        hash = Hash.new

        cols.each_with_index do |col, i|
          hash[col] =
            case row[i]
            when OCI8::LOB
              name == 'Writable Large Object' ? row[i]: row[i].read
            when OraDate
              (row[i].hour == 0 and row[i].minute == 0 and 

```

row[i].second  
== 0) ?  
row[i].to\_date : row[i].to\_time  
else row[i]  
end unless col == ‘raw\_rnum\_’  
end

```
        rows << hash
      end

      rows
    ensure
      cursor.close if cursor
    end

```

You could increase the number of open cursors at the server level too.  
Check to make sure you are using eager-lading though… not doing that  
can  
kill your app.

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-with-oracle/102119/4 "2006-08-15T14:20:42Z")

</div>

Jon Leung wrote:

> In Java, I can use stmt.close() to close all the cursors on a sql  
> statement. In Ruby on Rails the sql “statement” and the sql “result set”  
> are hidden inside ActiveRecord’s methods. Therefore I can’t close the  
> statement.
> 
> As a work around I can close the database connection, however it takes  
> too long to open another connection thousands of times.
> 
> How can I solve this problem?

Hi Jon,

I don’t know where you’re seeing this.

We’re using AR against Oracle and don’t have this issue at all.

In the active\_record/connection\_adapters/oracle\_adapter.rb, there’s this  
code,  
which closes the cursor:

```
     def select(sql, name = nil)
       cursor = execute(sql, name)
       cols = cursor.get_col_names.map { |x| oracle_downcase(x) }
       rows = []

       while row = cursor.fetch

```

…  
…  
…  
rows \<\< hash  
end

```
       rows
     ensure
       cursor.close if cursor
     end

```

So it does close the cursor.

Regards,  
Blair
