From: jplaine Date: Mon, 11 Oct 2010 07:27:26 +0000 (+0000) Subject: Added some synchronization #844 X-Git-Tag: simantics-1.2.1~33 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=64da41c4b30a69421cc1c9d284deef2f52cde7dd;p=simantics%2Fsysdyn.git Added some synchronization #844 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18360 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java index efb80e38..4fb6cccb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java @@ -21,11 +21,12 @@ import org.simantics.db.exception.DatabaseException; /** * Manages system dynamic models. - * @author Hannu Niemistö + * @author Hannu Niemist� */ public class SysdynModelManager { WeakHashMap models = - new WeakHashMap(); + new WeakHashMap(); // FIXME: Resources are weak, there is no guarantee that model exists in this map as long as needed. + // HashTable with a disposal procedure could be better solution.. Session session; public SysdynModelManager(Session session) { @@ -36,40 +37,44 @@ public class SysdynModelManager { * Use only if not inside a transaction */ public SysdynModel getModel(final Resource resource) { - SysdynModel model = models.get(resource); - if(model == null) { - try { - session.syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - models.put(resource, new SysdynModel(graph, resource)); - - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - - - model = models.get(resource); - } - return model; + synchronized(models) { + SysdynModel model = models.get(resource); + if(model == null) { + try { + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + models.put(resource, new SysdynModel(graph, resource)); + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + + model = models.get(resource); + } + return model; + } } /** * Should be used if called inside a transaction */ public SysdynModel getModel(ReadGraph g, Resource resource) { - SysdynModel model = models.get(resource); - if(model == null) { - model = new SysdynModel(g, resource); - models.put(resource, model); - } - return model; + synchronized(models) { + SysdynModel model = models.get(resource); + if(model == null) { + model = new SysdynModel(g, resource); + models.put(resource, model); + } + return model; + } } - public static SysdynModelManager getInstance(Session session) { + public synchronized static SysdynModelManager getInstance(Session session) { SysdynModelManager manager = session.peekService(SysdynModelManager.class); if(manager == null) {