[Catalyst-commits] r6183 - in trunk/Catalyst-Plugin-Session-Store-DBI: . lib/Catalyst/Plugin/Session/Store

andyg at dev.catalyst.perl.org andyg at dev.catalyst.perl.org
Fri Mar 23 00:33:25 GMT 2007


Author: andyg
Date: 2007-03-23 00:33:24 +0000 (Fri, 23 Mar 2007)
New Revision: 6183

Modified:
   trunk/Catalyst-Plugin-Session-Store-DBI/Changes
   trunk/Catalyst-Plugin-Session-Store-DBI/lib/Catalyst/Plugin/Session/Store/DBI.pm
Log:
Session::Store::DBI 0.11, patch from Peter Karman to reset cached sth's if the dbh goes away.  Optimized a bit by pre-generating all SQL statements at startup

Modified: trunk/Catalyst-Plugin-Session-Store-DBI/Changes
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBI/Changes	2007-03-22 12:13:43 UTC (rev 6182)
+++ trunk/Catalyst-Plugin-Session-Store-DBI/Changes	2007-03-23 00:33:24 UTC (rev 6183)
@@ -1,5 +1,10 @@
 Revision history for Perl extension Catalyst::Plugin::Session::Store::DBI
 
+0.11    2007-03-22 17:00:00
+        - Reset cached statement handles if the dbh goes away or changes.
+          (Peter Karman)
+        - Pre-generate all SQL statements at startup.
+
 0.10    2007-03-15 12:00:00
         - Fixed Rose::DB::Object dbh call.
         - Forgot to document Rose::DB::Object support.

Modified: trunk/Catalyst-Plugin-Session-Store-DBI/lib/Catalyst/Plugin/Session/Store/DBI.pm
===================================================================
--- trunk/Catalyst-Plugin-Session-Store-DBI/lib/Catalyst/Plugin/Session/Store/DBI.pm	2007-03-22 12:13:43 UTC (rev 6182)
+++ trunk/Catalyst-Plugin-Session-Store-DBI/lib/Catalyst/Plugin/Session/Store/DBI.pm	2007-03-23 00:33:24 UTC (rev 6183)
@@ -8,8 +8,9 @@
 use NEXT;
 use Storable qw/nfreeze thaw/;
 
-our $VERSION = '0.10';
+our $VERSION = '0.11';
 
+__PACKAGE__->mk_classdata('_session_sql');
 __PACKAGE__->mk_classdata('_session_dbh');
 __PACKAGE__->mk_classdata('_sth_get_session_data');
 __PACKAGE__->mk_classdata('_sth_get_expires');
@@ -127,6 +128,27 @@
                      . 'please configure dbi_dbh or dbi_dsn'
         );
     }
+    
+    # Pre-generate all SQL statements
+    my $table = $c->config->{session}->{dbi_table};
+    $c->_session_sql( {
+        get_session_data        =>
+            "SELECT session_data FROM $table WHERE id = ?",
+        get_expires             =>
+            "SELECT expires FROM $table WHERE id = ?",
+        check_existing          =>
+            "SELECT 1 FROM $table WHERE id = ?",
+        update_session          =>
+            "UPDATE $table SET session_data = ?, expires = ? WHERE id = ?",
+        insert_session          =>
+            "INSERT INTO $table (session_data, expires, id) VALUES (?, ?, ?)",
+        update_expires          =>
+            "UPDATE $table SET expires = ? WHERE id = ?",
+        delete_session          =>
+            "DELETE FROM $table WHERE id = ?",
+        delete_expired_sessions =>
+            "DELETE FROM $table WHERE expires IS NOT NULL AND expires < ?",
+    } );
 }
 
 sub _session_dbi_connect {
@@ -240,34 +262,27 @@
 sub _session_sth {
     my ( $c, $key ) = @_;
 
-    my $table = $c->config->{session}->{dbi_table};
-
-    my %sql = (
-        get_session_data        =>
-            "SELECT session_data FROM $table WHERE id = ?",
-        get_expires             =>
-            "SELECT expires FROM $table WHERE id = ?",
-        check_existing          =>
-            "SELECT 1 FROM $table WHERE id = ?",
-        update_session          =>
-            "UPDATE $table SET session_data = ?, expires = ? WHERE id = ?",
-        insert_session          =>
-            "INSERT INTO $table (session_data, expires, id) VALUES (?, ?, ?)",
-        update_expires          =>
-            "UPDATE $table SET expires = ? WHERE id = ?",
-        delete_session          =>
-            "DELETE FROM $table WHERE id = ?",
-        delete_expired_sessions =>
-            "DELETE FROM $table WHERE expires IS NOT NULL AND expires < ?",
-    );
-
-    if ( $sql{$key} ) {
+    if ( my $sql = $c->_session_sql->{$key} ) {
         my $accessor = "_sth_$key";
-        if ( !defined $c->$accessor ) {
-            $c->$accessor( $c->_session_dbh->prepare( $sql{$key} ) );
+        
+        if ( defined $c->$accessor ) {
+            
+            # Check for the 'morning bug', where the dbh may have gone away
+            # while we still have cached sth's using it.
+            if ( $c->$accessor->{Database} ne $c->_session_dbh ) {
+                # The sth has an old dbh, so we need to prepare it again
+                if ( $c->$accessor->{Active} ) {
+                    $c->$accessor->finish;
+                }
+            }
+            else {
+                return $c->$accessor;
+            }
         }
-        return $c->$accessor;
+        
+        return $c->$accessor( $c->_session_dbh->prepare( $sql ) );
     }
+    
     return;
 }
 
@@ -275,18 +290,8 @@
 sub DESTROY {
     my $c = shift;
     $c->NEXT::DESTROY(@_);
-
-    my @handles = qw/
-        get_session_data
-        get_expires
-        check_existing
-        update_session
-        insert_session
-        update_expires
-        delete_session
-        delete_expired_sessions/;
-
-    for my $key (@handles) {
+    
+    for my $key ( keys %{ $c->_session_sql } ) {
         my $accessor = "_sth_$key";
         if ( defined $c->$accessor && $c->$accessor->{Active} ) {
             $c->$accessor->finish;




More information about the Catalyst-commits mailing list