Re: already existing database handles

[prev] [thread] [next] [Date index for 2005/06/17]

From: Edward J. Sabol
Subject: Re: already existing database handles
Date: 18:39 on 17 Jun 2005
> I have been searching the mailing list and the web for an answer to this
> but nothing has presented itself.
>
> I want to use Class::DBI in a framework which already has database
> connections open. It seems from the docs that Class::DBI wants to establish
> the connection in new(). Is there a way to tell it to use an already
> existing database handle?
>
> I thought about creating the class::dbi object with fake data and then
> setting $obj->{db_Main} manually but seems like a nasty hack and doesn't
> seem to work anyway.
>
> Ideally, I want to be able to tell class::dbi in new to use a passed in
> dbhandle. soemthing like
>
> Class::DBI->new(db_Main => $dbh, namespace => . . . etc

This is actually covered in the Class::DBI documentation in the section
"Dynamic Database Connections / db_Main" and is certainly possible, with one
major caveat: The DBI handle you want to re-use must conform to what
Class::DBI expects. There are a few required attributes, as defined in
Class::DBI::_default_attributes and Ima::DBI::_default_attributes. The most
notable (some might say annoying) of these is RootClass =>
DBIx::ContextualFetch. If the framework whose DBI handles you wish to re-use
is incompatible with DBIx::ContextualFetch, then you cannot re-use database
handles.

Anyway, as the Class::DBI documentation says, the correct way of doing this
is to override the db_Main subroutine in your base class. Your db_Main should
return a DBI handle with the correct attributes and it should do so quickly,
as db_Main is called quite frequently. Refer to the following Wiki nodes for
examples of how to override db_Main:

http://wiki.class-dbi.com/index.cgi?UsingWithModPerl
http://wiki.class-dbi.com/index.cgi?UsingMultipleDatabases

What you want to do is similar to -- though not exactly the same as -- the
methods outlined in those nodes.

Hope this helps,
Ed

already existing database handles
moreejt 17:01 on 17 Jun 2005

Re: already existing database handles
Edward J. Sabol 18:39 on 17 Jun 2005

Re: already existing database handles
JT Moree 02:02 on 19 Jun 2005

Re: already existing database handles
Sean Davis 03:15 on 19 Jun 2005

Re: already existing database handles
JT Moree 21:02 on 19 Jun 2005

Generated at 16:37 on 28 Jul 2005 by mariachi v0.52