Camping and DBI

Lennon Day-Reynolds rcoder at gmail.com
Fri Jan 26 12:27:30 EST 2007


On 1/26/07, James <jamesd.earl at gmail.com> wrote:
> Hi,
>
> I've been playing around with using DBI instead of ActiveRecord 'cause
> I just love to beat myself up!  ;)  Is this an okay way to do this, or
> is there any other more creative ways?

The code looks like it'll *work*, but you're not going to get much of
a performance boost over regular ActiveRecord -- esp. since you
reconnect to the database for each query!

Try this:

---
class DB
def self.dbh
  @dbh ||= DBI.connect('DBI:Mysql:dbname', 'username', 'password')
end
end
---

That way, your connection will only be established once per model
class, and will stay open between requests. Alternately, you could set
@@dbh instead of @dbh, which would open just one connection for all
your model classes, but you might have problems handling multiple
simultaneous requests through a single active database handle.

-Lennon


More information about the Camping-list mailing list