[ANN] Apache::DBI 0.0.20041025b

M

MoonWolf

Apache::DBI - DBI persistent connection library
===============================================
It works together with mod_ruby.
It holds DBI connection for every process of Apache.
From application, it can use transparent.

Requires
--------

* mod_ruby
* ruby-dbi

Install
-------

# ruby setup.rb

Configuration
-------------

httpd.conf:
<IfModule mod_ruby.c>
RubyRequire apache/dbi
</IfModule>

foo.rbx:
require 'dbi'
dbh = DBI.connect('dbi:pg:foo',...)
# returns pooled DatabaseHandle

Download
 
B

Brian Candler

I'm interested in this, but can you explain what extra functionality it
gives over

[foo.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:foo',...)

Thanks,

Brian.
 
M

MoonWolf

Brian said:
I'm interested in this, but can you explain what extra functionality it
gives over

[foo.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:foo',...)

[foo.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:foo',...)

[bar.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:bar',...)

`$dbh` conflicts. This is not cool way.
Apache::DBI's advantage is transparent & simple.
 
B

Brian Candler

[foo.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:foo',...)

[bar.rbx]
require 'dbi'
$dbh ||= DBI.connect('dbi:pg:bar',...)

$dbh conflicts. This is not cool way.

Well OK, but if you can't choose a unique global variable name, you could
instead write

module ::MyPrivateStuff
def self.dbh
@dbh ||= DBI.connect('dbi:pg:foo',...)
end
end
dbh = ::MyPrivateStuff.dbh
Apache::DBI's advantage is transparent & simple.

I'm guessing now. Does your module keep a separate database instance for
each unique value of the DBI string? Or each unique combination of all the
arguments inside DBI.connect(...) ?

In other words, does it do something like

$dbh ||= {}
$dbh['dbi:pg:bar'] ||= DBI.connect('dbi:pg:bar')

Help me out here :) I'm just trying to understand what it's doing, so I can
see whether I can make use of it.

If my guess is correct, then perhaps the pattern can be abstracted: e.g.

dbh = Persistent[DBI, :connect, 'dbi:pg:bar', ...]

and the implementation would be:

module Persistent
@things = {}
def self.[](*args)
@things[args] ||= args[0].send(*args[1..-1])
end
end

Regards,

Brian.
 
M

MoonWolf

Brian said:
I'm guessing now. Does your module keep a separate database instance for
each unique value of the DBI string? Or each unique combination of all the
arguments inside DBI.connect(...) ?

Apache::DBI has each unique combination of all the arguments.
In other words, does it do something like

$dbh ||= {}
$dbh['dbi:pg:bar'] ||= DBI.connect('dbi:pg:bar')

Help me out here :) I'm just trying to understand what it's doing, so I can
see whether I can make use of it.

If my guess is correct, then perhaps the pattern can be abstracted: e.g.

dbh = Persistent[DBI, :connect, 'dbi:pg:bar', ...]

and the implementation would be:

module Persistent
@things = {}
def self.[](*args)
@things[args] ||= args[0].send(*args[1..-1])
end
end

I think that it is necessary to take into consideration that it is also
peculiar to DBI, such as disconnet.

in future release, disconnected DBI instance(at DBI exception) is
automatically delete from Apache::DBI cache.
 
B

Brian Candler

I think that it is necessary to take into consideration that it is also
peculiar to DBI, such as disconnet.

in future release, disconnected DBI instance(at DBI exception) is
automatically delete from Apache::DBI cache.

OK, that's a useful feature to have. Perhaps also when you do DBI.connect
you could 'ping' an existing connection to ensure it is still alive, and if
not, reconnect.

I don't think that all DBI exceptions should cause a disconnection - but an
explicit disconnect, or an exception which indicates that the database was
disconnected (DBI::OperationalError?)

Regards,

Brian.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,188
Messages
2,571,004
Members
47,597
Latest member
TuyetKinne

Latest Threads

Top