LoggerAppenderPDO: Fixed reconnect logic.
authorIvan Habunek <ihabunek@apache.org>
Sat, 18 Aug 2012 10:22:19 +0000 (10:22 +0000)
committerIvan Habunek <ihabunek@apache.org>
Sat, 18 Aug 2012 10:22:19 +0000 (10:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/logging/log4php/trunk@1374546 13f79535-47bb-0310-9956-ffa450edef68

src/main/php/appenders/LoggerAppenderPDO.php

index 9c9ab06..6443aad 100644 (file)
@@ -119,7 +119,7 @@ class LoggerAppenderPDO extends LoggerAppender {
                try {
                        $this->establishConnection();
                } catch (PDOException $e) {
-                       $this->warn("Failed connecting to database: " . $e->getMessage());
+                       $this->warn("Failed connecting to database. Closing appender. Error: " . $e->getMessage());
                        $this->close();
                        return;
                }
@@ -157,27 +157,24 @@ class LoggerAppenderPDO extends LoggerAppender {
         * the appender will close.
         */
        public function append(LoggerLoggingEvent $event) {
-               
-               for ($attempt = 1; $attempt <= $this->reconnectAttempts; $attempt++) {
+
+               for ($attempt = 1; $attempt <= $this->reconnectAttempts + 1; $attempt++) {
                        try {
-                               // If it's a retry, reestablish the connection
-                               if ($attempt > 1) {
-                                       $this->establishConnection();
-                               }
-                               
-                               // Write to database
-                               @$this->preparedInsert->execute($this->format($event));
-                               @$this->preparedInsert->closeCursor();
+                               // Attempt to write to database
+                               $this->preparedInsert->execute($this->format($event));
+                               $this->preparedInsert->closeCursor();
                                break;
                        } catch (PDOException $e) {
                                $this->warn("Failed writing to database: ". $e->getMessage());
                                
                                // Close the appender if it's the last attempt
-                               if ($attempt == $this->reconnectAttempts) {
-                                       $this->warn("Failed after {$this->reconnectAttempts} attempts. Closing appender.");
+                               if ($attempt > $this->reconnectAttempts) {
+                                       $this->warn("Failed writing to database after {$this->reconnectAttempts} reconnect attempts. Closing appender.");
                                        $this->close();
+                               // Otherwise reconnect and try to write again
                                } else {
                                        $this->warn("Attempting a reconnect (attempt $attempt of {$this->reconnectAttempts}).");
+                                       $this->establishConnection();
                                }
                        }
                }