Can't create edge within a transaction that creates a document

45 views
Skip to first unread message

Borov

unread,
Nov 14, 2016, 4:25:05 PM11/14/16
to OrientDB
I'm trying run a transaction which will perform few operations using SQL. First it will create a new document, then it will update a status of another document and third, it will create an edge between an existing document and the newly created. I tried to use a transaction and even nested transactions, but to no avail. Mainly the problem is that when I create a document with an SQL, it returns a temporary RID. It seems to me that Orient doesn't like to use temp document RID to create an edge.

I'm using the latest 2.2.12 version. Here is a simple version of the code:

ODatabaseDocumentTx db = DbPool.getConnection();
String existingRid = "#111:1";

try {
    db.begin();

    // create transaction record
    ODocument doc = new ODocument("TransactionDirectPost");
    ODocument transaction = doc.save();
    String transactionRid = transaction.getIdentity().toString();

    // change data state to sold
    String sql = "update ? set metaInternal.dataState = ?";
    db.command(new OCommandSQL(sql)).execute(new ORecordId(existingRid), "SOLD");

    // create an edge
    sql = "create edge SentDirectPost from ? to ?";
    db.command(new OCommandSQL(sql)).execute(new ORecordId(existingRid), new ORecordId(transactionRid));

    db.commit();
} catch (Exception e) {
    db.rollback();
} finally {
    db.close();
}

Error I'm getting:

Cannot read record #-1:-2 since the position is invalid in database 'Customers'

The error is thrown at the edge creation time.

It seems logical to perform SQL operations in a single transaction. Is this a completely incorrect way of doing such operations? If so, what is the right or best way to do it? 

Thanks.

user.w...@gmail.com

unread,
Nov 14, 2016, 6:24:33 PM11/14/16
to OrientDB
Hi,

can you try to commit the update operation?
Have you already insert some data or did you do it in some steps above?

Thx

Regards,
Michela

Borov

unread,
Nov 14, 2016, 6:34:49 PM11/14/16
to OrientDB
If I commit the update operation it works, but it breaks the transaction and creates a risk of not creating an edge. There are 2 documents (2 separate classes), one already exists (variable existingRid) and one is going to be created within this transaction and I want to create an edge between them two. 

Borov

unread,
Nov 16, 2016, 9:20:37 PM11/16/16
to OrientDB
Any ideas anyone?

alessand...@gmail.com

unread,
Nov 17, 2016, 5:28:29 AM11/17/16
to OrientDB
Hi, 
have you seen the documentation ?

Best regards,
Alessandro

Borov

unread,
Nov 17, 2016, 11:30:41 AM11/17/16
to OrientDB
Hi Alessandro,

Yes, I've read the docs multiple times and typically we use transactions in multiple parts of the application just fine. What's unique about this case is that we adding an edge in the transaction for the newly created record (vertex). This is where Orient fails on me.

As you can see from the description, we need to execute 3 commands as part of one transaction: create new document, update property of another document and create an edge from an existing document to the newly created. The first 2 are fine, but the edge creation fails with the complaint about the RID. It seems that the edge can't be created in the transaction for the temporary RID of the just created document.

I need to understand if this is a limitation of Orient and is there a better way of doing this. Intuitively, I feel that if the edge creation is the last command that must be executed when transaction is committed on the server's end, it shall use temporary RID map it internally to the permanent (since at the time of the 3rd command the created document must have the permanent RID already) and be able to create an edge.

Or, am I getting this completely wrong? If so, how can I add the edge as part of the same transaction when creating a document to avoid a possibility of creating a new document without the edge?

alessand...@gmail.com

unread,
Nov 17, 2016, 11:41:39 AM11/17/16
to OrientDB
Hi Borov,
you are right. If a document have a temporary id you can't create an edge from or to that document.


Borov

unread,
Nov 17, 2016, 11:43:19 AM11/17/16
to OrientDB
Thanks for confirming. Wouldn't it be nice to add this functionality to the future releases? Or, is there a good reason I'm not aware of not to do so?

alessand...@gmail.com

unread,
Nov 17, 2016, 11:47:56 AM11/17/16
to OrientDB
You could open a request on github.

Thanks.
Reply all
Reply to author
Forward
0 new messages