AMBARI-2526. NPE in the ambari log4j when the threads are shutting down. (smohanty)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/branch-1.2.4@1498599 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java b/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java
index 7c5582c..55934d4 100644
--- a/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java
+++ b/contrib/ambari-log4j/src/main/java/org/apache/ambari/log4j/common/store/DatabaseStore.java
@@ -96,10 +96,12 @@
   @Override
   public void close() throws IOException {
     try {
-      connection.close();
+      if (this.initialized && this.connection != null) {
+        connection.close();
+      }
     } catch (SQLException sqle) {
       throw new IOException(
-          "Failed to close connectionto database " + this.database, sqle);
+          "Failed to close connection to database " + this.database, sqle);
     }
   }
 }
diff --git a/contrib/ambari-log4j/src/test/java/org/apache/ambari/log4j/common/store/TestDatabaseStore.java b/contrib/ambari-log4j/src/test/java/org/apache/ambari/log4j/common/store/TestDatabaseStore.java
new file mode 100644
index 0000000..bafe16c
--- /dev/null
+++ b/contrib/ambari-log4j/src/test/java/org/apache/ambari/log4j/common/store/TestDatabaseStore.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.log4j.common.store;
+
+import junit.framework.TestCase;
+import org.apache.ambari.log4j.common.LogStoreUpdateProvider;
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.io.IOException;
+import java.sql.Connection;
+
+public class TestDatabaseStore extends TestCase {
+
+  class SampleLogStoreUpdateProvider implements LogStoreUpdateProvider {
+    public void init(Connection connection) throws IOException {
+    }
+
+    public void update(LoggingEvent originalEvent, Object parsedEvent)
+        throws IOException {
+    }
+  }
+
+  public void testDatabaseStore() throws IOException {
+    DatabaseStore store = new DatabaseStore(TestDatabaseStore.class.getName(), "", "", "",
+        new SampleLogStoreUpdateProvider());
+    store.close();
+  }
+}