Fix the AAIGraph closing prematurely that was
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / dbmap / AAIGraph.java
index 72beb85..4921b91 100644 (file)
@@ -29,16 +29,16 @@ import java.util.Map;
 import java.util.Properties;
 
 import org.apache.commons.lang.exception.ExceptionUtils;
+import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
-
 import org.openecomp.aai.dbgen.SchemaGenerator;
 import org.openecomp.aai.exceptions.AAIException;
 import org.openecomp.aai.util.AAIConstants;
+
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.thinkaurelius.titan.core.TitanFactory;
 import com.thinkaurelius.titan.core.TitanGraph;
-import com.thinkaurelius.titan.core.TitanTransaction;
 import com.thinkaurelius.titan.core.schema.TitanManagement;
 
 /**
@@ -54,11 +54,12 @@ import com.thinkaurelius.titan.core.schema.TitanManagement;
  */
 public class AAIGraph {
 
-       protected Map<String, TitanGraph> graphs = new HashMap<>();
+       private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
        protected static final String COMPONENT = "aaidbmap";
+       protected Map<String, TitanGraph> graphs = new HashMap<>();
        private final String REALTIME_DB = "realtime";
        private final String CACHED_DB = "cached";
-       private final EELFLogger logger = EELFManager.getInstance().getLogger(this.getClass().getSimpleName());
+
 
 
        /**
@@ -95,47 +96,55 @@ public class AAIGraph {
        }
        
        private void loadGraph(String name, String configPath) throws AAIException {
-               try {
-                       final TitanGraph graph = TitanFactory.open(configPath);
-                       InputStream is = new FileInputStream(configPath);
-               Properties graphProps = new Properties();
-               graphProps.load(is);
-               
-                       if (graphProps.get("storage.backend").equals("inmemory")) { 
+           // Graph being opened by TitanFactory is being placed in hashmap to be used later
+               // These graphs shouldn't be closed until the application shutdown
+           TitanGraph graph = TitanFactory.open(configPath);
+               try (InputStream is = new FileInputStream(configPath)) {
+
+                       Properties graphProps = new Properties();
+                       graphProps.load(is);
+
+                       if ("inmemory".equals(graphProps.get("storage.backend"))) {
                                // Load the propertyKeys, indexes and edge-Labels into the DB
                                loadSchema(graph);
-                               if (graphProps.containsKey("load.snapshot.file")) {
-                                       String value = graphProps.getProperty("load.snapshot.file");
-                                       if ("true".equals(value)) {
-                                               try {
-                                                       String location = System.getProperty("snapshot.location");
-                                                       logAndPrint(logger, "Loading snapshot to inmemory graph.");
-                                                       TitanTransaction transaction = graph.newTransaction();
-                                                       transaction.io(IoCore.graphson()).readGraph(location);
-                                                       transaction.commit();
-                                                       logAndPrint(logger, "Snapshot loaded to inmemory graph.");
-                                               } catch (IOException e) {
-                                                       graph.close();
-                                                       logAndPrint(logger, "ERROR: Could not load datasnapshot to in memory graph. \n" + ExceptionUtils.getFullStackTrace(e));
-                                                       System.exit(0);
-                                               }
-                                       }
-                               }
+                               loadSnapShotToInMemoryGraph(graph, graphProps);
                        }
-                       
+
                        if (graph == null) {
                                throw new AAIException("AAI_5102");
                        }
-                       
+
                        graphs.put(name, graph);
                } catch (FileNotFoundException fnfe) {
                        throw new AAIException("AAI_4001");
            } catch (IOException e) {
                        throw new AAIException("AAI_4002");
-
            }
        }
-       
+
+       private void loadSnapShotToInMemoryGraph(TitanGraph graph, Properties graphProps) {
+               if (logger.isDebugEnabled()) {
+                       logger.debug("Load Snapshot to InMemory Graph");
+               }
+               if (graphProps.containsKey("load.snapshot.file")) {
+                       String value = graphProps.getProperty("load.snapshot.file");
+                       if ("true".equals(value)) {
+                               try (Graph transaction = graph.newTransaction()) {
+                                       String location = System.getProperty("snapshot.location");
+                                       logAndPrint(logger, "Loading snapshot to inmemory graph.");
+                                       transaction.io(IoCore.graphson()).readGraph(location);
+                                       transaction.tx().commit();
+                                       logAndPrint(logger, "Snapshot loaded to inmemory graph.");
+                               } catch (Exception e) {
+                                       logAndPrint(logger,
+                                               "ERROR: Could not load datasnapshot to in memory graph. \n"
+                                                       + ExceptionUtils.getFullStackTrace(e));
+                                       System.exit(0);
+                               }
+                       }
+               }
+       }
+
        private void loadSchema(TitanGraph graph) {
                // Load the propertyKeys, indexes and edge-Labels into the DB
                TitanManagement graphMgt = graph.openManagement();