Discussion:
Sharing a DBI connection with Class::DBI
Sam Tregar
2008-01-30 02:52:19 UTC
Permalink
Hey all. I'm getting started with Rose::DB::Object. My problem is I need
to inter-operate with a large pre-existing Class::DBI code-base. I'd like
to setup Rose to be able to share the same DBI connection that Class::DBI
uses - otherwise I'll instantly double the number of connections from our
web cluster when I roll out Rose::DB::Object.

I know of one problem - Class::DBI requires its DBI handles to inherit from
DBIx::ContextualFetch while Rose wants its handle to inherit from
Rose::DB::MySQL. Anybody know how to solve this? Alternately, do you know
of other reasons why it won't work?

Thanks!

-sam
Peter Karman
2008-01-30 02:55:45 UTC
Permalink
Post by Sam Tregar
Hey all. I'm getting started with Rose::DB::Object. My problem is I need
to inter-operate with a large pre-existing Class::DBI code-base. I'd like
to setup Rose to be able to share the same DBI connection that Class::DBI
uses - otherwise I'll instantly double the number of connections from our
web cluster when I roll out Rose::DB::Object.
I know of one problem - Class::DBI requires its DBI handles to inherit from
DBIx::ContextualFetch while Rose wants its handle to inherit from
Rose::DB::MySQL. Anybody know how to solve this? Alternately, do you know
of other reasons why it won't work?
just a naive guess here, but if you were using DBI->connect_cached as the
underlying connect method, wouldn't DBI handle the sharing for you?
--
Peter Karman . http://peknet.com/ . peter-Cz8MpMUdMMvQT0dZR+***@public.gmane.org

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Sam Tregar
2008-01-30 02:57:15 UTC
Permalink
Post by Peter Karman
just a naive guess here, but if you were using DBI->connect_cached as the
underlying connect method, wouldn't DBI handle the sharing for you?
No, I'm pretty sure DBI uses the class as part of the connect_cached key.

-sam
John Siracusa
2008-01-30 03:08:27 UTC
Permalink
Post by Sam Tregar
Post by Peter Karman
just a naive guess here, but if you were using DBI->connect_cached as the
underlying connect method, wouldn't DBI handle the sharing for you?
No, I'm pretty sure DBI uses the class as part of the connect_cached key.
And as for DBI's, connect_cached(), you can use that (instead of the
default connect()) in Rose::DB by overriding dbi_connect() in your
Rose::DB subclass:

http://search.cpan.org/dist/Rose-DB/lib/Rose/DB.pm#dbi_connect

-John

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
John Siracusa
2008-01-30 03:06:39 UTC
Permalink
Post by Sam Tregar
Hey all. I'm getting started with Rose::DB::Object. My problem is I need
to inter-operate with a large pre-existing Class::DBI code-base. I'd like
to setup Rose to be able to share the same DBI connection that Class::DBI
uses - otherwise I'll instantly double the number of connections from our
web cluster when I roll out Rose::DB::Object.
I know of one problem - Class::DBI requires its DBI handles to inherit from
DBIx::ContextualFetch while Rose wants its handle to inherit from
Rose::DB::MySQL. Anybody know how to solve this? Alternately, do you know
of other reasons why it won't work?
The "db" atribute of an RDBO-derived object "isa" Rose::DB, but each
Rose::DB-derived object "has a" DBI $dbh. IOW, plain old DBI database
handles are used via delegation in RDBO. There's no subclassing of
DBI classes at all.

As for sharing, if you already have a DBI $dbh from elsewhere, you can
simply stuff it into an appropriate Rose::DB object and use that as
the "db" value in RDBO objects and calls. Just make sure that the
Rose::DB object has the same driver as the DBI $dbh.

Example:

$dbh = ...; # get from elsewhere
$db = Rose::DB->new(driver => 'mysql', dbh => $dbh);

Overriding init_db() in a common base class is the usual way to do
this "everywhere." There are many possible policies for init_db().
You can find a few old threads on the topic in the list archives:

http://www.mail-archive.com/rose-db-object-5NWGOfrQmneRv+***@public.gmane.org/maillist.html

but keep in mind the new new_or_cached() Rose::DB method that makes a
lot of the caching discussion in some older threads less relevant
these days.

http://search.cpan.org/dist/Rose-DB/lib/Rose/DB.pm#new_or_cached

Also keep in mind the funky (but useful! :) mod_perl-aware default
behavior of new_or_cached()'s default cache backend:

http://search.cpan.org/dist/Rose-DB/lib/Rose/DB/Cache.pm#prepare_db

-John

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Sam Tregar
2008-01-30 03:24:17 UTC
Permalink
Post by John Siracusa
The "db" atribute of an RDBO-derived object "isa" Rose::DB, but each
Rose::DB-derived object "has a" DBI $dbh. IOW, plain old DBI database
handles are used via delegation in RDBO. There's no subclassing of
DBI classes at all.
Good to hear. Do you happen to know if RDBO will tolerate a
DBIx::ContextualFetch based DBI handle?

but keep in mind the new new_or_cached() Rose::DB method that makes a
Post by John Siracusa
lot of the caching discussion in some older threads less relevant
these days.
http://search.cpan.org/dist/Rose-DB/lib/Rose/DB.pm#new_or_cached
Yes, I found that one - seems very useful. Maybe something to add to the
tutorial?

Thanks for the help!

-sam
John Siracusa
2008-01-30 03:32:49 UTC
Permalink
Post by Sam Tregar
Do you happen to know if RDBO will tolerate a
DBIx::ContextualFetch based DBI handle?
It should, and if it doesn't, I can probably make it do so with some
minor edits. Give of a try and let me know.

-John

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Sam Tregar
2008-01-30 18:26:56 UTC
Permalink
Post by John Siracusa
Post by Sam Tregar
Do you happen to know if RDBO will tolerate a
DBIx::ContextualFetch based DBI handle?
It should, and if it doesn't, I can probably make it do so with some
minor edits. Give of a try and let me know.
Looks pretty good. I setup a hacked Rose::DB with RootClass set to
DBIx::ContextualFetch by default and ran the Rose::DB::Object test suite
against it with MySQL. Everything passed. I'll let you know if I see
anything odd when I try to actually use it myself.

-sam

Loading...