Cache dbi for web app

okay, so i didn’t completely trash my web app, i’m going to create both
types of interfaces instead, web and traditional.

but my web app is dog slow. one of the reasons is that i connect to the
database on every request. is there a way to persist a database
connection between requests?

~transami

__("< berk! berk!
__/
^^

Tom Sawyer transami@transami.net writes:

but my web app is dog slow. one of the reasons is that i connect to
the database on every request. is there a way to persist a database
connection between requests?

Perhaps putting the functionality into a daemon, and having the web
front-end communicate via a pipe or shared memory would do what you
want? (can you do shm in ruby?)

Or, perhaps use an rpc mechanism, like xmlrpc?

···


Josh Huber

I’ve never used it but I’ve had http://www.firstworks.com/sqlrelay.html in
my bookmarks for a while – just in case I thought I needed a speedup
based on connection pooling daemons.

···

On Sat, Jul 13, 2002 at 05:51:28AM +0900, Tom Sawyer wrote:

okay, so i didn’t completely trash my web app, i’m going to create both
types of interfaces instead, web and traditional.

but my web app is dog slow. one of the reasons is that i connect to the
database on every request. is there a way to persist a database
connection between requests?

~transami

__("< berk! berk!
__/
^^


Alan Chen
Digikata LLC

Tom Sawyer transami@transami.net writes:

but my web app is dog slow. one of the reasons is that i connect to the
database on every request. is there a way to persist a database
connection between requests?

Switching to mod_ruby would help a lot, both removing the interpreter
start-up time and giving you a persistent database connection.

For the connection, something as simple as

db ||= DBI.connect(…)

at the start of the script will work fine until you want to get
sophisticated and do things like reconnect after database shutdowns
and the like.

Cheers

Dave

Tom Sawyer transami@transami.net writes:

but my web app is dog slow. one of the reasons is that i connect to the
database on every request. is there a way to persist a database
connection between requests?

if you’re using mod_ruby or any other methods that use the same process
for serving different requests, then perhaps this connection pool
class is helpful.

YS.

dbasePool.rb (4.53 KB)

hey dave,

i am using mod_ruby. just to be clear, using the cgi module dosen’t
inhibit mod_ruby in any way right? also, what about extensions. my main
script is .rbx, per mod_ruby, but it requires other scripts that are
just .rb. how does that effect it? and finally…

db ||= DBI.connect(…)

how does this work?

thanks,
tom

···

On Fri, 2002-07-12 at 15:51, Dave Thomas wrote:

Tom Sawyer transami@transami.net writes:

but my web app is dog slow. one of the reasons is that i connect to the
database on every request. is there a way to persist a database
connection between requests?

Switching to mod_ruby would help a lot, both removing the interpreter
start-up time and giving you a persistent database connection.

For the connection, something as simple as

db ||= DBI.connect(…)

at the start of the script will work fine until you want to get
sophisticated and do things like reconnect after database shutdowns
and the like.

Cheers

Dave


~transami

__("< berk! berk!
__/
^^

Tom Sawyer transami@transami.net writes:

i am using mod_ruby. just to be clear, using the cgi module dosen’t
inhibit mod_ruby in any way right? also, what about extensions. my main
script is .rbx, per mod_ruby, but it requires other scripts that are
just .rb. how does that effect it? and finally…

it all gets loaded once and then stays there, getting invoked each
time it handles a request.

db ||= DBI.connect(…)

how does this work?

It probably should have been $db ||= …

The first time the program is run, $db will be nil, so the above
statement, which is equivalent to

$db = $db || DBI.connect…

will assign the value of DBI.connect to $db.

The second and subsequent times the script runs, $db will already be
set (to the database connection) and so you won’t reconnect.

Dave