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;
46 sub disconnect_all_and_clear {
47 $_->disconnect for values %cache;
54 my ($dbconnect, $dbuser, $dbpasswd, $options, $initial_sql) = @_;
60 join ';', apply { s/([;\\])/\\$1/g } # no collisions if anything contains ;
61 map { $_ => $options->{$_} }
62 sort keys %$options; # deterministic order
64 join ';', apply { $_ //= ''; s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str, $initial_sql;
75 SL::DBConnect::Cache - cached database handle pool
79 use SL::DBConnect::Cache;
81 my $dbh = SL::DBConnect::Cache->get(@args);
82 SL::DBConnect::Cache->store($dbh, @args);
84 # reset a cached handle
85 SL::DBConnect::Cache->reset($dbh);
87 # close a cached handle and forget it
88 SL::DBConnect::Cache->close($dbh);
90 SL::DBConnect::Cache->clear($dbh);
95 Implements a managed cache for DB connection handles.
97 The same would be possible with C<< DBI->connect_cached >>, but in that case,
98 we would have no control over the cache.
106 Retrieve a connection specified by C<ARGS>.
108 =item * C<store DBH ARGS>
110 Store a connection specified by C<ARGS>.
112 =item * C<reset ARGS>
114 Rollback the connection specified by C<ARGS>.
118 Empties the cache. If handles are not referenced otherwise, they will get
129 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>