]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added some synchronization #844
authorjplaine <jplaine@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 11 Oct 2010 07:27:26 +0000 (07:27 +0000)
committerjplaine <jplaine@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 11 Oct 2010 07:27:26 +0000 (07:27 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18360 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java

index efb80e38e8ae16ad242304d681dda46fc123403b..4fb6cccb8c1a0ba79d35df51fee5f4424fbf5289 100644 (file)
@@ -21,11 +21,12 @@ import org.simantics.db.exception.DatabaseException;
 \r
 /**\r
  * Manages system dynamic models.\r
- * @author Hannu Niemistö\r
+ * @author Hannu Niemist\r
  */\r
 public class SysdynModelManager {\r
     WeakHashMap<Resource, SysdynModel> models =\r
-        new WeakHashMap<Resource, SysdynModel>();\r
+        new WeakHashMap<Resource, SysdynModel>(); // FIXME: Resources are weak, there is no guarantee that model exists in this map as long as needed.\r
+                                                                                         //        HashTable with a disposal procedure could be better solution..\r
     Session session;\r
 \r
     public SysdynModelManager(Session session) {\r
@@ -36,40 +37,44 @@ public class SysdynModelManager {
      * Use only if not inside a transaction\r
      */\r
     public SysdynModel getModel(final Resource resource) {\r
-        SysdynModel model = models.get(resource);\r
-        if(model == null) {\r
-            try {\r
-                session.syncRequest(new ReadRequest() {\r
-                        \r
-                        @Override\r
-                        public void run(ReadGraph graph) throws DatabaseException {\r
-                            models.put(resource, new SysdynModel(graph, resource));\r
-                            \r
-                        }\r
-                    });\r
-            } catch (DatabaseException e) {\r
-                e.printStackTrace();\r
-            }\r
-\r
-            \r
-            model = models.get(resource);\r
-        }\r
-        return model;\r
+       synchronized(models) {\r
+               SysdynModel model = models.get(resource);\r
+               if(model == null) {\r
+                   try {\r
+                       session.syncRequest(new ReadRequest() {\r
+                               \r
+                               @Override\r
+                               public void run(ReadGraph graph) throws DatabaseException {\r
+                                   models.put(resource, new SysdynModel(graph, resource));\r
+                                   \r
+                               }\r
+                           });\r
+                   } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+                   }\r
+       \r
+                   \r
+                   model = models.get(resource);\r
+               }\r
+               return model;\r
+       }\r
     }\r
     \r
     /**\r
      * Should be used if called inside a transaction\r
      */\r
     public SysdynModel getModel(ReadGraph g, Resource resource) {\r
-        SysdynModel model = models.get(resource);\r
-        if(model == null) {\r
-            model = new SysdynModel(g, resource);\r
-            models.put(resource, model);\r
-        }\r
-        return model;\r
+       synchronized(models) {\r
+               SysdynModel model = models.get(resource);\r
+               if(model == null) {\r
+                   model = new SysdynModel(g, resource);\r
+                   models.put(resource, model);\r
+               }\r
+               return model;\r
+       }\r
     }\r
 \r
-    public static SysdynModelManager getInstance(Session session) {\r
+    public synchronized static SysdynModelManager getInstance(Session session) {\r
         SysdynModelManager manager =\r
             session.peekService(SysdynModelManager.class);\r
         if(manager == null) {\r