Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No such function" error occurred while creating & executing Javascript Functions dynamically. #4785

Closed
randikaf opened this issue Aug 13, 2015 · 5 comments
Assignees
Labels

Comments

@randikaf
Copy link

I have encountered with below error while creating Javascript functions and executing it from a web application. The newly created function is not getting executed and below error has been occurred.

Exception in thread "main" com.orientechnologies.orient.core.command.script.OCommandScriptException: Error on parsing script at position #0: Error on execution of the script
Script: testFunction3
------^
    at com.orientechnologies.orient.core.command.script.OCommandExecutorFunction.executeInContext(OCommandExecutorFunction.java:106)
    at com.orientechnologies.orient.core.metadata.function.OFunction.executeInContext(OFunction.java:148)
    at com.orientechnologies.orient.core.metadata.function.OFunction.execute(OFunction.java:123)
    at Test.main(Test.java:45)
Caused by: java.lang.NoSuchMethodException: No such function testFunction3
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:190)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:505)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:227)
    at com.orientechnologies.orient.core.command.script.OCommandExecutorFunction.executeInContext(OCommandExecutorFunction.java:93)
    ... 3 more

This issue can recreate using below code snippet and using below mentioned steps. Here I used this code to simulate the function execution happen in my web application. Web application initialize the DB connection at startup.

    ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:localhost/SampleDb");
    db.open("admin", "admin");
    boolean enableNextFunc = false;
    final String functionName = "testFunction";
    String name = functionName +"1";
    int count = 1;
    while (true) {
        db.reload();

        if(enableNextFunc){
            name = functionName+count;
        }
        OFunction sum = db.getMetadata().getFunctionLibrary().getFunction(name);


        if(sum != null){
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("a", "160:9");
            //
            Object obj = (Object) sum.execute("160:9");
            System.out.println("Function: "+functionName +", Result: "+obj.toString());
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            count++;
            enableNextFunc= true;
        }
    }
  • Run the above code in a main method.
  • Create a Javascript function from the Orientdb Studio with the name of testFunction1 and with a simple return "F1"; script & save.
  • Then again create another function with the name of testFunction2 with the script of return "F2";

The function testFunction1 will execute but the second function testFunction2 will not execute and will throw the above error.

@randikaf
Copy link
Author

@lvca , Can you please comment on this or delegate to someone?

@lvca
Copy link
Member

lvca commented Sep 21, 2015

Ok, seems the function library caches it.

@lvca
Copy link
Member

lvca commented Sep 21, 2015

By reading your log, the error is on "testFunction3" that doesn't exist in the database. In your description you speak about testFunction1 and testFunction2, but not testFunction3 that is the function not found.

@lvca lvca closed this as completed Sep 21, 2015
@lvca lvca self-assigned this Sep 21, 2015
@randikaf
Copy link
Author

randikaf commented Nov 4, 2015

@lvca This is a valid issue and you might not followed the steps to reproduce the issue. Here is another code snippet to reproduce the same issue. Once you run the below code, it'll execute only first function, then it'll fail while executing the second function.
OrientDB: orientdb-community-2.1.0

    public static void main(String[] args) {
        ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:localhost/GratefulDeadConcerts");
        db.open("admin", "admin");
        int count = 1;
        while (true) {
            db.reload();

            String name = "testFunction" + count;
            OFunction sum = db.getMetadata().getFunctionLibrary().createFunction(name);
            sum.setLanguage("javascript");
            sum.setCode("return " + name + ";");
            sum.save();

            System.out.println(sum.getId());
            Object obj = (Object) sum.execute();
            System.out.println("Function: " + name + ", Result: " + obj.toString());
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            count++;
        }
    }

Therefore I can't create functions dynamically and execute those with this issue.

@cjayawickrema
Copy link

@lvca I have observed the same. Here's our practical use case. We have a web application running on tomcat which uses orientdb java API. And using this API we are invoking some server functions (javascript).

Now when we go to orientdb studio and update the function logic, the changes are not effective until I restart tomcat. However, this problem doesn't occur when we invoke it through your Rest API (not sure if that's the correct term) or from studio itself. So I assume this has something to do with the Java API where it might be caching these functions. Any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants