You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sm...@apache.org on 2013/06/19 02:54:20 UTC

svn commit: r1494406 - /incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java

Author: smohanty
Date: Wed Jun 19 00:54:20 2013
New Revision: 1494406

URL: http://svn.apache.org/r1494406
Log:
AMBARI-2427. Error during installing NameNode - name node takes a long time to start. (smohanty)

Modified:
    incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java

Modified: incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java?rev=1494406&r1=1494405&r2=1494406&view=diff
==============================================================================
--- incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java (original)
+++ incubator/ambari/branches/branch-1.2.4/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java Wed Jun 19 00:54:20 2013
@@ -24,6 +24,9 @@ import java.sql.SQLException;
 
 import org.apache.ambari.log4j.common.LogStore;
 import org.apache.ambari.log4j.common.LogStoreUpdateProvider;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.util.StringUtils;
 import org.apache.log4j.spi.LoggingEvent;
 
 public class DatabaseStore implements LogStore {
@@ -31,14 +34,19 @@ public class DatabaseStore implements Lo
   final private String database;
   final private String user;
   final private String password;
-  
-  final private Connection connection;
   final private LogStoreUpdateProvider updateProvider;
-  
-  public DatabaseStore(String driver, 
-      String database, String user, String password, 
-      LogStoreUpdateProvider updateProvider) 
+  final private String driver;
+  private Connection connection;
+  private boolean initialized;
+
+  private static final Log LOG = LogFactory.getLog(DatabaseStore.class);
+
+  public DatabaseStore(String driver,
+                       String database, String user, String password,
+                       LogStoreUpdateProvider updateProvider)
       throws IOException {
+    this.initialized = false;
+    this.driver = driver;
     try {
       Class.forName(driver);
     } catch (ClassNotFoundException e) {
@@ -48,20 +56,40 @@ public class DatabaseStore implements Lo
     this.database = database;
     this.user = (user == null) ? "" : user;
     this.password = (password == null) ? "" : password;
-    try {
-      this.connection = 
-          DriverManager.getConnection(this.database, this.user, this.password);
-    } catch (SQLException sqle) {
-      throw new IOException("Can't connect to database " + this.database, sqle);
-    }
     this.updateProvider = updateProvider;
-    
-    this.updateProvider.init(this.connection);
   }
-  
+
   @Override
   public void persist(LoggingEvent originalEvent, Object parsedEvent)
       throws IOException {
+    if (!this.initialized) {
+      synchronized (DatabaseStore.class) {
+        if (!this.initialized) {
+          try {
+            this.connection =
+                DriverManager.getConnection(this.database, this.user, this.password);
+          } catch (SQLException sqle) {
+            LOG.debug("Failed to connect to db " + this.database, sqle);
+            System.err.println("Failed to connect to db " + this.database +
+                " as user " + this.user + " password " + this.password +
+                " and driver " + this.driver + " with " +
+                StringUtils.stringifyException(sqle));
+            throw new IOException("Can't connect to database " + this.database, sqle);
+          } catch (Exception e) {
+            LOG.debug("Failed to connect to db " + this.database, e);
+            System.err.println("Failed to connect to db " + this.database +
+                " as user " + this.user + " password " + this.password +
+                " and driver " + this.driver + " with " +
+                StringUtils.stringifyException(e));
+            throw new RuntimeException(
+                "Failed to create database store for " + this.database, e);
+          }
+          this.updateProvider.init(this.connection);
+          this.initialized = true;
+        }
+      }
+    }
+
     updateProvider.update(originalEvent, parsedEvent);
   }