Re: using callbacks with connect_cached

Vincent Veyron <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.general
Message-ID <[email protected]>
Le vendredi 23 août 2013 à 16:25 +0200, David E. Wheeler a écrit :

> Oh, you know what? I think this is why the example in the docs and my blog post puts the callback into a lexical variable, then just passes that lexical. This is because the params passed to connect_cached(), if they vary at all, create different connections. You call connect_cached() three times, and construct the Callbacks hash reference anew each time. So it is different each time.
> 
> Try this:
> 
> my $cb = {
>     'connect_cached.reused' => sub { print "reused!!!"; return; },
> };
> for my $test (1..3) {
>     my $cached_dbh = MyApp::SQLite->connect_cached(
>         "dbi:SQLite:dbname=$dbfile", '', '',
>         {
>             RaiseError  => 1,
>             PrintError  => 0,
>             Callbacks => $cb,
>         }
>     );
>     ...
> }
> 

That did the trick! Congratulations for finding it.

I still can't use connect_cached.new for my original purpose, which was
to send an SQL query setting the correct datestyle for a new connection;
the error returned is :

 Can't call method "do" on an undefined value
at /home/lib/Aspro/db_handle.pm

which seems logical, since DBI.pm's documentation says :

"connect_cached.*" callbacks are invoked before connect_cached() has
applied the connect attributes 

But I found a middle ground (setting the datestyle once at login time,
it is then kept by the cached connection)

Thank you for your help.

-- 
Salutations, Vincent Veyron 
http://marica.fr/ 
Gestion des contrats, des contentieux juridiques et des sinistres
d'assurance
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.