Server-side function: unable to read new/updated data from within same transaction

80 views
Skip to first unread message

Jean-Sebastien Lemay

unread,
May 23, 2016, 5:30:46 AM5/23/16
to OrientDB
I'm trying to figure out if it will be possible for me to split my logic between multiple server-side functions. For that to work, I need to make sure that data created/edited in one function call can be read by other subsequent functions within the same transaction. So far, that's not working for me, not even within the same function.

Here is my example Javascript function:

var db = orient.getDatabase();
db.save({
  "@class": "UserProfile",
  name : "test10-5"});

var userProfile = db.query('SELECT FROM UserProfile WHERE name = "test10-5"');
return userProfile;

With this logic, I expect one (1) record to be returned. My Java code indicates that an OIdentifiable[] array is returned, but it is empty. How come I can't find the data that I just created?

user.w...@gmail.com

unread,
May 23, 2016, 9:41:32 AM5/23/16
to OrientDB

Hi Jean-Sebastien,

I tried your script with version 2.2 and setting the language as javascrpit it works fine for me:




Hope it helps.


Regards,

Michela


Jean-Sebastien Lemay

unread,
May 23, 2016, 9:57:20 AM5/23/16
to OrientDB
Hello,

Yes, it works within the UI, but if you create a transaction with the Java API and execute the function it is unable to retrieve the record.

See my Java code:
OrientGraph db = test.graphFactory.getTx();
OIdentifiable[] retValue1 = (OIdentifiable[])db.getRawGraph()
.getMetadata()
.getFunctionLibrary()
.getFunction("testCreate")
.execute();

for (OIdentifiable oId : retValue1) {
System.out.println(String.format(
"#%s:%s",
oId.getIdentity().getClusterId(),
oId.getIdentity().getClusterPosition()));
}   

db.commit();

In the above example, my loop doesn't print anything, implying that my return array is empty. How come?

Luca Garulli

unread,
May 23, 2016, 11:33:45 AM5/23/16
to OrientDB
Are you connected with remote ? In this case the server-side function is out of the transaction that is on the client only until the commit(). Could it be the reason?

Best Regards,

Luca Garulli
Founder & CEO


--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean-Sebastien Lemay

unread,
May 23, 2016, 11:55:35 AM5/23/16
to OrientDB
Yes, I am using remote, and I need to figure out a way to be able to read my own modifications within the same transaction.

How would I update my function to be able to access the data, including modifications and new entities, within the current transaction?


Cheers,
Jean

Jean-Sebastien Lemay

unread,
May 23, 2016, 12:11:03 PM5/23/16
to OrientDB
I now realize that what I'm looking for is serializable isolation, which OrientDB does not offer. I was not aware of that. OK, that means a lot of problems for me. I thought I could achieve serializable level of isolation if I put all my logic within server-side functions.

Cheers,
Jean

Emrul Islam

unread,
May 24, 2016, 2:56:14 PM5/24/16
to OrientDB
Actually, unless I am mistaken you are saving then querying within the same server-side function.  In this instance, it *should* work I think.

Will let the OrientDB guys comment.

Luca Garulli

unread,
May 24, 2016, 4:20:35 PM5/24/16
to OrientDB
You're right: if you deploy your logic in the server, your function will be in the TX scope. I've just created your function on an empty database and works:

[
    {
        "@type": "d",
        "@rid": "#19:0",
        "@version": 1,
        "@class": "UserProfile",
        "name": "test10-5"
    }
]

But in that code there is no begin/commit, so everything is not tx. When you see "in transaction", what do you mean?

So I've changed the function to be like:

var db = orient.getDatabase();
db.begin();
db.save({
  "@class": "UserProfile",
  name : "test10-5"});

var userProfile = db.query('SELECT FROM UserProfile WHERE name = "test10-5"');
db.commit();
return userProfile;

And still works:

[
    {
        "@type": "d",
        "@rid": "#19:0",
        "@version": 1,
        "@class": "UserProfile",
        "name": "test10-5"
    },
    {
        "@type": "d",
        "@rid": "#19:1",
        "@version": 1,
        "@class": "UserProfile",
        "name": "test10-5"
    }
]


Best Regards,

Luca Garulli
Founder & CEO


Jean-Sebastien Lemay

unread,
May 25, 2016, 1:54:30 PM5/25/16
to OrientDB
Hi Luca,

At first it didn't work on my end because I was starting and committing the transaction from the Java code, and executing the function in between. Now I'm using OrientGraphNoTx and simply invoking my function, which creates and commit the transaction within the Javascript instead like your example, and that seems to work. 

Still, there's one remaining problem. Here's a new example function:

var db = orient.getDatabase();
db.begin();
db.save({
  "@class": "UserProfile",
  name : "test99-2"});

var userProfile = db.query('SELECT FROM UserProfile WHERE name = "test99-2"');
var allProfiles = db.query('SELECT FROM UserProfile');
var allMessages = db.query('SELECT FROM Message');
db.commit();
return {
  'userProfile': userProfile,
  'allProfiles': allProfiles,
  'allMessages': allMessages
};

When trying to parse the result, I'm back to the problem I originally had and published on StackOverflow last week, still unanswered: http://stackoverflow.com/questions/37330370/orientdb-how-to-return-multiple-datasets-from-a-javascript-command-executed-fro

Essentially it seems that my function above is not returning vertices or edges, as expected, but OIdentifiable[] instead. This means that my work is not done and I must fetch every OIdentifiable separately, after my transaction has committed, which is risky. Plus, imagine it returns 1000 records, and I have network latency of 10ms, doing 1000 separate db.getVertex() or db.getEdge() with each OIdentifiable will cost me 10 seconds. How can I solve this problem?

Best regards,
Jean
Reply all
Reply to author
Forward
0 new messages