orientechnologies / orientdb

OrientDB is the most versatile DBMS supporting Graph, Document, Reactive, Full-Text and Geospatial models in one Multi-Model product. OrientDB can run distributed (Multi-Master), supports SQL, ACID Transactions, Full-Text indexing and Reactive Queries.
https://orientdb.dev
Apache License 2.0
4.73k stars 869 forks source link

Script Engine is not thread-safe #1252

Closed henryzhao81 closed 11 years ago

henryzhao81 commented 11 years ago

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

lvca commented 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.

mattaylor commented 11 years ago

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

lvca commented 11 years ago

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.