markus jais wrote:
> hello,
> 
> I am writing a little vocabulary trainer with ruby,  and postgresql
> and the cool ruby-dbi.
> 
> There are some classes, each with several methods, many of them
> making connections to the database.
> 
> Is it a good idea to create a database (e.g. called "dbh")  handle in each 
> method and call at the end 
> dbh.disconncect
> that is, the dbh is local to each method

It's bad this way. Each method has it's own connection.
Could be very slow.

> this would be a lot of connects and disconnects, but maybe the
> risk of data los is smaller.

Disconnecting from a database is not neccessary to prevent data loss.
Important is, that you commit changes, i.e. transactions are important for
preventing data loss.
Thus after you have changed a table and these changes should be persistent forever,
even after a power-failure, call "dbh.commit"

> on the other hand: are the performace disadvantages with this
> 
> another idea would be to make dbh global because several classes
> need a database handle.

It would be bad to use global variables. Your classes would depend on this.

> a compromise would be to make "dbh" a class variable for each
> class that needs it.
> 
> any thoughts??

Pass a dbh handle to "initialize".
For example write a superclass for all classes that access the database:

class DatabaseAccess

  attr_accessor :dbh  

  def initialize(dbh)
    @dbh = dbh
  end

end

class Vokabel < DatabaseAccess

  def initialize(dbh, ....)
    super(dbh)
  end

  # ...

end


or assign @dbh yourself in initialize without the need of DatabaseAccess.


Regards,

  Michael

-- 
Michael Neumann
merlin.zwo InfoDesign GmbH
http://www.merlin-zwo.de