1 package SL::DBConnect::Cache;
4 use List::MoreUtils qw(apply);
9 my ($package, @args) = @_;
11 my $dbh = $cache{ _args2str(@args) };
13 if (!$dbh->{Active}) {
14 delete $cache{ _args2str(@args) };
22 my ($package, $dbh, @args) = @_;
24 $cache{ _args2str(@args) } = $dbh;
28 my ($package, @args) = @_;
30 my $dbh = $cache{ _args2str(@args) };
39 $_->{AutoCommit} || $_->rollback for values %cache;
49 my ($dbconnect, $dbuser, $dbpasswd, $options, $initial_sql) = @_;
55 join ';', apply { s/([;\\])/\\$1/g } # no collisions if anything contains ;
56 map { $_ => $options->{$_} }
57 sort keys %$options; # deterministic order
59 join ';', apply { $_ //= ''; s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str, $initial_sql;
70 SL::DBConnect::Cache - cached database handle pool
74 use SL::DBConnect::Cache;
76 my $dbh = SL::DBConnect::Cache->get(@args);
77 SL::DBConnect::Cache->store($dbh, @args);
79 # reset a cached handle
80 SL::DBConnect::Cache->reset($dbh);
82 # close a cached handle and forget it
83 SL::DBConnect::Cache->close($dbh);
85 SL::DBConnect::Cache->clear($dbh);
90 Implements a managed cache for DB connection handles.
92 The same would be possible with C<< DBI->connect_cached >>, but in that case,
93 we would have no control over the cache.
101 Retrieve a connection specified by C<ARGS>.
103 =item * C<store DBH ARGS>
105 Store a connection specified by C<ARGS>.
107 =item * C<reset ARGS>
109 Rollback the connection specified by C<ARGS>.
113 Empties the cache. If handles are not referenced otherwise, they will get
124 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>