# Db class and connection problems

**URL:** <https://www.ruby-forum.com/t/db-class-and-connection-problems/158134>\
**Category:** Ruby\
**Created:** [January 19, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/db-class-and-connection-problems/158134 "2009-01-19T17:15:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmcdougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lmcdougal](https://www.ruby-forum.com/u/lmcdougal)\
**Post date:** [January 19, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/db-class-and-connection-problems/158134/1 "2009-01-19T17:15:13Z")

</div>

Hello,

I am new in ruby and have a little problem… I have the following code

class Grid  
attr\_accessor :host  
…  
attr\_accessor :connection

def initialize (host, user, passwd, dbname)  
@host = host  
@user = …  
end

def connect  
@connection = Mysql.real\_connect(@host, @user, @passwd, @dbname)  
if @connection then  
puts “connected”  
end  
end

def query  
res = @connection.query(“select \* from table”)  
return res  
end  
end

and now from a script

require “the clash file”

db = Grid.new(“localhost”,“user”,“password”,“db”)  
db.connect  
db.query

and I get an error cause the database seem to be closed… or which is  
the same @connection is null? I only can make it work if I open and  
close the database from inside the query method, but this query is  
called more than 4000 times in the script and I reach the limit of  
connections of my system’s TCP/IP

Anyone could help me? I don’t understand why I can share @host, @user  
and the other attributes among the methods but @connection seems to be  
lost.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![lmcdougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lmcdougal](https://www.ruby-forum.com/u/lmcdougal)\
**Post date:** [January 19, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/db-class-and-connection-problems/158134/2 "2009-01-19T21:56:41Z")

</div>

Luis Moran wrote:  
…

> and I get an error

Show the _exact_ error - I mean copy-paste it, including the backtrace.  
Cross-reference backtrace entries which refer to your own code to the  
actual lines of your source.

It would also be useful if you could give a complete small program which  
replicates the problem, because it could just be a typo. At first glance  
what you’ve written looks fine, but it’s not runnable - e.g. there’s no  
require ‘mysql-ruby’ or require ‘ruby-mysql’. Also, it’s worth  
mentioning which mysql library you’re using, and what version.

> cause the database seem to be closed… or which is  
> the same @connection is null?

Those are two very different things. One says that @connection points to  
some object, but the object says the database connection is closed (it  
may have been closed due to some sort of error, for example). The other  
says @connection points to nothing at all (well, technically an instance  
of the ‘nil’ object), which could only happen if your own code set  
@connection = nil.

> I only can make it work if I open and  
> close the database from inside the query method, but this query is  
> called more than 4000 times in the script and I reach the limit of  
> connections of my system’s TCP/IP

If that’s true, then clearly you’re not closing each connection after  
using it.

Regards,

Brian.
