Closed henryzhao81 closed 11 years ago
You're right, seems that it isn't multi-thread. I was thinking about not wasting resources creating a OScriptManager all the times but just removing the property that caches the engines:
protected Map<String, ScriptEngine> engines;
And creating a new ScriptEngine every-time.
As some implementations of the script engine may be threadsafe you can check at run time by
if (ScriptEngineFactory.getParameter("THREADING") == null)
FYI Oracle's new Script Engine, Nashorn is now available in OpenJDK 1.8 - ( https://blogs.oracle.com/nashorn/) Although still not threadsafe Nashorn is suppsedly 5 times faster, 5 times smaller and
Hi Matt, thank you for the tips, implemented! Now we've 2 different collections: shareable and not-shareable engines.
About Nashorn I met one of the main developers in Riga, Latvia few weeks ago and they are doing an incredible job: making the most dynamic language as static identifying patterns where this is possible.
Check on JDK6 API Document, looks like ScriptEngine is not thread safe http://docs.oracle.com/javase/6/docs/api/javax/script/ScriptEngineFactory.html "The engine implementation is not thread safe, and cannot be used to execute scripts concurrently on multiple threads."
In source code, the script engine looks like create by singleton (OCommandExecutorFunction.java) line 69 final OScriptManager scriptManager = Orient.instance().getScriptManager();
I did some concurrent testing, some data lost, some data is duplicated. Then i change the code by new ScriptManager every time, final OScriptManager scriptManager = new OScriptManager(); and looks good, no data lost, no data duplicate.
Please help check.
BTW, i have posted my test code on Google code before
https://groups.google.com/forum/?fromgroups=#!topic/orient-database/Dpkb7Nwuh0U