db class and connection problems

Discussion in 'Ruby' started by Luis Moran, Jan 19, 2009.

  1. Luis Moran

    Luis Moran Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Luis Moran, Jan 19, 2009
    #1
    1. Advertising

  2. 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 19, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Johnson

    Using Connection Class VS Connection Module

    Dave Johnson, Dec 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    647
    Dave Johnson
    Dec 11, 2005
  2. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  3. R
    Replies:
    1
    Views:
    580
  4. Kathryn Bean
    Replies:
    1
    Views:
    694
    John Harrison
    Oct 28, 2004
  5. =?Utf-8?B?cGF0cmlja2RyZA==?=

    Internal connection fatal error and other random db connection err

    =?Utf-8?B?cGF0cmlja2RyZA==?=, Jun 14, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    579
    =?Utf-8?B?cGF0cmlja2RyZA==?=
    Jun 14, 2007
Loading...

Share This Page