[NUUG fiksgatami] [patch] Avoid internal server errors and improve error message from fixmystreet

Petter Reinholdtsen pere at hungry.com
Mon Jan 26 18:22:40 CET 2009


[Matthew Somerville]
> Unless I applied the patch incorrectly (always possible :) ),
> there's a problem with your changes to DBHandle.pm - it's possible
> that a call to select_all() is the first call to the database, which
> means it is the call to dbh() that connects to and sets up the
> database. So $dbh doesn't exist before then in order to change its
> HandleError - dbh() throws a warning about dbh_process, and then
> creates a new_dbh() anyway, so the change to HandleError is lost.

I do not believe it is a problem, as the return value from consequtive
dbh() calls should be the same variable, and thus the same HandleError
will be operated on too.  Anyway, to make it more clear that the same
variable is operated on, this patch work too.

Index: perllib/mySociety/DBHandle.pm
===================================================================
RCS file: /repos/mysociety/perllib/mySociety/DBHandle.pm,v
retrieving revision 1.22
diff -u -3 -p -r1.22 DBHandle.pm
--- perllib/mySociety/DBHandle.pm       26 Jan 2009 15:06:14 -0000      1.22
+++ perllib/mySociety/DBHandle.pm       26 Jan 2009 17:13:43 -0000
@@ -188,17 +188,16 @@ END {

 sub select_all {
     my ($query, @bind_values) = @_;
-    # Commenting out for now
-    #our $dbh;
-    #local $dbh->{HandleError} =
-    #    sub ($$$) {
-    #        my ($err) = @_;
-    #        # Let's not make any unwise assumptions about reentrancy here.
-    #        local $dbh->{HandleError} = sub ($$$) { };
-    #        $dbh->rollback();
-    #        throw mySociety::DBHandle::Error($err . ": '" . $query . "' - args '" . join("','", @bind_values) . "'");
-    #    };
-    dbh()->selectall_arrayref($query, { Slice => {} }, @bind_values);
+    my $dbh = dbh();
+    local $dbh->{HandleError} =
+        sub ($$$) {
+            my ($err) = @_;
+            # Let's not make any unwise assumptions about reentrancy here.
+            local $dbh->{HandleError} = sub ($$$) { };
+            $dbh->rollback();
+            throw mySociety::DBHandle::Error($err . ": '" . $query . "' - args '" . join("','", @bind_values) . "'");
+        };
+    $dbh->selectall_arrayref($query, { Slice => {} }, @bind_values);
 }

 1;

The code will loose the reconnect support within select_all(), but I
hope it is OK.  It is tested and found to be working (at least it is
reporting the failing SQL calls. :)

Happy hacking,
-- 
Petter Reinholdtsen


More information about the fiksgatami mailing list