Re: using callbacks with connect_cached

"David E. Wheeler" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.general
Message-ID <[email protected]>
On Aug 24, 2013, at 9:40 PM, Vincent Veyron <[email protected]> wrote:

> Not quite : I thought connected_cache.new would enable me to set the
> correct datestyle once for a cached connection, and then be cached with
> it, but we've seen that won't work.
> 
> Using the connected callback would do a 'set datestyle to xyz' for each
> connection, even if cached. I realize it's no big deal (.5ms on my lowly
> Atom processor), but every little bit helps, as I'm sure you know. 
> 
> So I now set the datestyle when the user logs in (hence once, with
> $dbh->do('SET datestyle TO xyz'), and the database handle is cached and
> reused as is.

You can also use a private attribute in connect_cached.new to tell the connected callback to do its thing:


my $cb = {
    'connect_cached.new' => sub {
        $_[4]->{private_is_new} = 1;
        return;
    },
    connected => sub {
        my $dbh = shift;
        warn "connected\n" if delete $dbh->{private_is_new};
        return;
    }
};

for (1..3) {
    DBI->connect_cached('dbi:SQLite:', '', '', {
        PrintError => 0,
        RaiseError => 1,
        AutoCommit => 1,
        Callbacks  => $cb,
    });
}

This emits "connected" only once.

Best,

David
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.