On Sun, Aug 25, 2013 at 01:38:57PM +0200, David E. Wheeler wrote:
> On Aug 24, 2013, at 9:40 PM, Vincent Veyron <vv.li...@wanadoo.fr> wrote:
> 
> 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.

If you're going to use a callback on connected and an attribute then
there's no need to also use connect_cached.new. Just this should do:

      connected => sub {
          my $dbh = shift;
          if (++$dbh->{private_connected_count} == 1) {
              ... # is new connection
          }
          return;
      }

Tim.

p.s. I'd be delighted to get a doc patch that notes the need to use a
lexical for the callbacks hashref on connect_cached.

p.p.s. I'd also consider a patch to add a 'connect_cached.connected'
callback for new connections.

Reply via email to