<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Brandon Black wrote:
<blockquote
cite="mid84621a60608220801n6ec106e4s573277669ab73c3b@mail.gmail.com"
type="cite"><br>
<br>
<div><span class="gmail_quote">On 8/22/06, <b
class="gmail_sendername">Andreas Dembach</b> <<a
href="mailto:ad@dg-i.net">ad@dg-i.net</a>> wrote:</span>
<blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Hello list,<br>
<br>
we have the following situation:<br>
<br>
- we are migrating mod_perl application to Catalyst under mod_perl2
and<br>
use DBIC as persistence layer.<br>
- the apache server hosts two (in fact even more) virtual hosts with
the <br>
same application but different configurations (e.g. one for test and
one<br>
for production).<br>
- beneath other configuration items, like template-root we want the<br>
different virtual hosts to use different database connections. <br>
<br>
Problem:<br>
<br>
As far as I can tell from the source code, DBIx::Class::Storage::DBI<br>
caches the database handle in '_dbh' which results in always using the<br>
first database handle that inititated a connection. So we end up
having <br>
the configured $dbh of whatever virtual host is called first within a<br>
newly created Apache process.<br>
<br>
Question:<br>
<br>
Is this a design decision to support only one DB connection per
process<br>
or is this a little bug that could be fixed by caching the dbh with
some <br>
additional meta info (e.g. the connection parameters à la<br>
DBI->connect_cached)?</blockquote>
<br>
Skipping the Catalyst part of things and just focusing on
DBIx::Class: This should be a non-issue as long as you're using
schema objects rather than schema classes, which is the preferred and
recommended way to go about things. <br>
</div>
</blockquote>
yes, you're probably right at this point. actually our application is
not completely a catayst application, but consists of two parts: a
web-frontend (catalyst-based) and a separate business logic layer,
which the catalyst app uses via a defined interface. We are using
DBIx::Class in both parts. In the non-Catalyst part I could verify,
that we do not have the problem here.<br>
<br>
Remains the one in the Catalyst::DBIC::Schema :-( <br>
<br>
package web::Model::Data;<br>
<br>
use strict;<br>
use base 'Catalyst::Model::DBIC::Schema';<br>
<br>
__PACKAGE__->config(<br>
schema_class => 'web::Schema::IbfData',<br>
connect_info => [<br>
sub { some-module->get_dbh; }<br>
],<br>
);<br>
<br>
I have always the same storage class (with the same dbh) within the
schema, so I guess the sub {} that I'm passing as connect_info is
only
called once.<br>
<br>
Matt suggested in another post to use ACCEPT_CONTEXT, which seems a
good idea. I tried that and it seems to work but anyway there are two
issues left:<br>
<br>
1) I could not figure out how to get rid of the
initial connect_info set by the ->config() call that
Cat::DBIC::Schema
seems to need during startup, which annoys a little since I do not have
any configuration information during Apache startup.<br>
2) I have to cache the schema for each database source, otherwise the
schema/storage/whatever seems to just vanish after returning the newly
cloned schema which results in exceptions like "Can't call method
"source" on an undefined value" or "calling execute on disconnected
handle".<br>
<br>
My Cat Model now looks like this:<br>
<br>
=====================================<br>
use base 'Catalyst::Model::DBIC::Schema';<br>
<br>
use Helper::Configuration;<br>
<br>
__PACKAGE__->config(<br>
schema_class => 'web::Schema::IbfDaten',<br>
# this here actually does not result in a valid DB connection, because
configuration is not yet determined on server startup<br>
connect_info => [<br>
Helper::Configuration->property("db.source"),<br>
Helper::Configuration->property("db.user"),<br>
Helper::Configuration->property("db.password"),<br>
],<br>
);<br>
<br>
my %schema_buffer;<br>
<br>
sub ACCEPT_CONTEXT {<br>
my ( $self, $c, @extra_arguments ) = @_;<br>
<br>
# Now in the request, the DB properties are defined foreach virtual
host<br>
my $datasource = Helper::Configuration->property("db.source"),<br>
my $user = Helper::Configuration->property("db.user"),<br>
my $password = Helper::Configuration->property("db.password"),<br>
<br>
unless ( defined( $schema_buffer{$$}{$datasource} ) ) {<br>
my $schema = $self->connect( $datasource, $user, $password ) ;<br>
$schema_buffer{$$}{$datasource} = $schema;<br>
}<br>
return $schema_buffer{$$}{$datasource};<br>
}<br>
===================================<br>
<br>
Well, at least I can move the overriden Storage class to /dev/null now
:-)<br>
<br>
Thanks for your help,<br>
Andreas<br>
<blockquote
cite="mid84621a60608220801n6ec106e4s573277669ab73c3b@mail.gmail.com"
type="cite">
<div><br>
Are you directly accessing the schema class? Are you using
Catalyst::Model::DBIC::Schema? Can you paste/post your Schema and
Model files, so we can see exactly how you're connecting things? I
*think* C::M::DBIC::Schema handles this case correctly, but of course
there could be a bug or thinko of some kind. <br>
<br>
-- Brandon<br>
<br>
</div>
<pre wrap=""><hr size="4" width="90%">
_______________________________________________
List: <a class="moz-txt-link-freetext"
href="http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class">http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class</a>
Wiki: <a class="moz-txt-link-freetext"
href="http://dbix-class.shadowcatsystems.co.uk/">http://dbix-class.shadowcatsystems.co.uk/</a>
IRC: irc.perl.org#dbix-class
SVN: <a class="moz-txt-link-freetext"
href="http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/">http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/</a>
Searchable Archive: <a class="moz-txt-link-freetext"
href="http://www.mail-archive.com/dbix-class@lists.rawmode.org/">http://www.mail-archive.com/dbix-class@lists.rawmode.org/</a></pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
-----------------------------------------------------------------
Dembach Goo Informatik GmbH & Co. KG
Andreas Dembach fon +49-221-801 483 0
Rathenauplatz 9 fax +49-221-801 483 20
D-50674 Cologne emergency +49-180-50 50 70 919
Germany smtp ad +at+ dg-i +dot+ net
pgp fingerprint 25C2 8C94 015A E92C 3C67 E9B7 2E39 6FFC 277D 6700 </pre>
</body>
</html>