OpenInteract::DBI - Centralized connection location to DBI databases


OpenInteract documentation  | view source Contained in the OpenInteract distribution.

Index


NAME

Top

OpenInteract::DBI - Centralized connection location to DBI databases

SYNOPSIS

Top

 # Get a database handle based on the 'main' info in your config
 # (conf/server.perl)

 my $db = OpenInteract::DBI->connect({ $CONFIG->{db_info}{main} });

DESCRIPTION

Top

No, we do not subclass DBI with this. No, we do not override any of the DBI methods. Instead, we provide the means to connect to the database from one location along with the ability to manipulate the default connection information before we connect.

For instance, you can setup OpenInteract in two separate databases. When users of a certain name login (say, 'devel'), you can change the 'db_name' key of the database connection info hashref from 'webdb' to 'webdb-devel'.

Note that this should work flawlessly with Apache::DBI, and if you are using this on a different persistent Perl platform (say, PerlEx) then this module gives you a single location from which to retrieve database handles -- this makes using the BEGIN/END tricks ActiveState recommends in their FAQ pretty trivial.

METHODS

Top

connect( \%connnect_info, \%params )

Usage:

 my $connect_name = 'main';
 my $db = eval { OpenInteract::DBI->connect({
                         $CONFIG->{db_info}{ $connect_name } }) };

 die "Cannot connect to database! Error found: $@" if ( $@ );
 my ( $sth );
 eval {
     $sth = $db->prepare( 'SELECT blah FROM bleh' );
     $sth->execute;
 };
 ...

Returns: A DBI database handle with the following parameters set:

 RaiseError:  1
 PrintError:  0
 ChopBlanks:  1
 AutoCommit:  1 (for now...)
 LongReadLen: 32768 (or as set in config)
 LongTruncOk: 0 (or as set in config)

The first parameter is a hashref of connection information. This should include:

The second parameter is also a hashref and can contain the following keys:

Any errors encountered will result in an exception thrown via die. Generally, there will be two errors:

Other errors may occur in your callbacks, but reporting them is entirely up to you. This class does not wrap the call in an eval {} block so you can capture the error and inspect it yourself. After all, they are your callbacks.

STRATEGIES

Top

Under mod_perl, you can use the simple connection-pooling module Apache::DBI. This module is quite simple -- it overrides the connect call for DBI. For each connect call made, it looks at the parameters (dsn, username, password and the DBI parameters) to determine whether it has connected to this database previously. If so, it returns the cached connection. If not, it creates the connection and caches it. This happens on a per-httpd-child basis, so if you have 10 httpd children you will have 10 concurrent connections to the database. Easy, right?

What happens if you are running multiple websites using one httpd child? Say you have five websites running on mod_perl process group which has the aforementioned 10 children. Since each website likely has its own database, you will eventually have 10 x 5 = 50 connections to your database. This can be a bad thing.

To get around this, and assuming that all of these websites connect to the database as the same user (which certainly is not a given), OpenInteract allows you to specify a single database for connection. Once the connection is handed out, OpenInteract will perform the SQL 'use' command to switch to the correct database.

The trigger for this is the 'db_name' key of the first parameter passed into the connect method of this class. If this field is specified, the method will perform the following statement:

 $dbh->do( "use $db_info->{db_name}" );

So that you only have to keep one connection for this database open all the time.

TO DO

Top

Test with PerlEx

Try to use the BEGIN/END tricks ActiveState recommends -- do they work with just scripts, or also with modules?

BUGS

Top

None known.

SEE ALSO

Top

Apache::DBI

DBI - http://www.symbolstone.org/technology/perl/DBI

PerlEx - http://www.activestate.com/Products/PerlEx/

COPYRIGHT

Top

AUTHORS

Top

Chris Winters <chris@cwinters.com>

Marcus Baker <mbaker@intes.net>


OpenInteract documentation  | view source Contained in the OpenInteract distribution.