[Java] Document's permanent ID after commit?

78 views
Skip to first unread message

Mateusz Dymczyk

unread,
Mar 26, 2014, 11:16:43 AM3/26/14
to orient-...@googlegroups.com
As mentioned in the wiki (https://github.com/orientechnologies/orientdb/wiki/Transactions) while storing documents in the DB in a transaction they are assigned a temporary ID. Is there any way to get the permanent ID of that record after a commit? I read in one of the discussions that the commit() should return a mapping of changed IDs but I couldn't find is it how it actually works/how to use it.

Let's say we have a sample code like in the wiki example:

try{
  db.begin(TXTYPE.OPTIMISTIC);
  ODocument saved = db.save(document);
  db.commit();
  return saved;
} catch( Exception e ){
  db.rollback();
} finally{
  db.close();
}
Is there a way to store in "saved" the permanent ID instead of the temporary one before the return statement? A query for that permanent ID would be one way but I would really prefer to avoid having to do that kind of "tricks".

Regards,
Mateusz

Odysseas

unread,
Mar 27, 2014, 10:27:02 AM3/27/14
to orient-...@googlegroups.com
Mateusz,

After the commit the object will have a permanent ID assigned to it so if you retrieve the identity of the saved object, you should get a permanent record ID.

Odysseas

Mateusz Dymczyk

unread,
Mar 27, 2014, 8:13:30 PM3/27/14
to orient-...@googlegroups.com
Odysseas,

Yes I'm aware of that. The thing is it requires an additional request to the DB (yes not much overhead but I don't like it), me writing a method looking for an identical record (except the ID) which will have to be very generic AND potential problems if someone by mistake adds a DB class which doesn't have to be unique, in such case I won't have any way of telling which record's ID should I retrieve as there could be multiple identical ones. In such case I would have to add some kind of a timestamp or my my own ID.

I was wondering if there's a way to retrieve the result of the commit (what the commit actually did, some kind of temp -> permanent ID mapping) or to reload the document we got back from save() so it updates it's ID automatically.

Mateusz

Odysseas Pentakalos

unread,
Mar 28, 2014, 9:49:09 AM3/28/14
to orient-...@googlegroups.com
Mateusz,

I haven't tested this on a remote connection but on a local connection the ID of your object will be updated by reference once the commit is done so you don't need to perform another query. So, in the example you have shown above, if you do make a call to saved.getIdentity() before the commit you will get a temporary ID but if you call saved.getIdentity() after the commit, you should get a permanent one. Perhaps I am misunderstanding your question?

Odysseas




--

---
You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/slAw072CzvE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mateusz Dymczyk

unread,
Apr 1, 2014, 9:16:07 AM4/1/14
to orient-...@googlegroups.com
Hey,

Unless I'm doing something horribly wrong the ID in that object isn't
being updated. That's what I thought at the beginning but if I call
getIdentity() after commit() I still am getting the temporary ID
instead of the proper one. I'll go through the code, both my code and
Orient's, to see where's the problem and if it indeed should work as
you say.

Mateusz

Mateusz Dymczyk

unread,
Apr 1, 2014, 11:00:16 PM4/1/14
to orient-...@googlegroups.com
Oh and this only happens with distributed transactions (even if there's only 1 node in the cluster). Otherwise the ID is indeed properly updated. My guess is sending/receiving messages overrides something or that there are some shared collections (in OTransactionOptimistic or somewhere) and in distributed environment a different object is being updated. Not sure.

Mateusz 
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "OrientDB" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/orient-database/slAw072CzvE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages