From: Jared Johnson Date: Fri, 20 Mar 2009 21:41:49 +0000 (+0000) Subject: Add cookbook entry for dealing with runaway prepared statement cache X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f4db5947795f080d87afa7c598b54b238143bb3f;p=dbsrgits%2FDBIx-Class-Historic.git Add cookbook entry for dealing with runaway prepared statement cache --- diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index bb73581..5d6e34f 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -323,6 +323,8 @@ zamolxes: Bogdan Lucaciu norbi: Norbert Buchmuller +solomon: Jared Johnson + =head1 LICENSE You may distribute this code under the same terms as Perl itself. diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 27119b6..bd3c007 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -1737,4 +1737,33 @@ If you are instead using the L. +=head1 MEMORY USAGE + +=head2 Cached statements + +L normally caches all statements with L<< prepare_cached()|DBI/prepare_cached >>. +This is normally a good idea, but if too many statements are cached, the database may use too much +memory and may eventually run out and fail entirely. If you suspect this may be the case, you may want +to examine DBI's L<< CachedKids|DBI/CachedKidsCachedKids_(hash_ref) >> hash: + + # print all currently cached prepared statements + print for keys %{$schema->storage->dbh->{CachedKids}}; + # get a count of currently cached prepared statements + my $count = scalar keys %{$schema->storage->dbh->{CachedKids}}; + +If it's appropriate, you can simply clear these statements, automatically deallocating them in the +database: + + my $kids = $schema->storage->dbh->{CachedKids}; + delete @{$kids}{keys %$kids} if scalar keys %$kids > 100; + +But what you probably want is to expire unused statements and not those that are used frequently. +You can accomplish this with L or L: + + use Tie::Cache; + use DB::Main; + my $schema = DB::Main->connect($dbi_dsn, $user, $pass, { + on_connect_do => sub { tie %{shift->_dbh->{CachedKids}}, 'Tie::Cache', 100 }, + }); + =cut