Issue creating records in transaction
@Ostico, @mogui We are trying pyOrient at our company since Neo4j looks like is going to be too expensive. So our backend is going to be really dependent on the engine we choose. On my firsts tests I came across this issue:
I think there is a problem when creating records in a graph, here's what I found trying to make the transaction work with graphs:
client = pyorient.OrientDB("localhost", 2424)
client.connect("xxx", "xxx")
client.db_open("test", "admin", "admin", pyorient.DB_TYPE_GRAPH, "")
people_cluster = client.command("create class People extends V")
client.command("create vertex People content {'name': 'dummy', 'age': 21}")
client.command("create vertex People content {'name': 'foo'}")
attrs = {'@People': {'name': 'another_me', 'age': 31}}
res = client.record_create(people_cluster[0], attrs)
attrs2 = {'@People': {'name': 'me', 'age': 30}}
rec_position2 = ( client.get_message(pyorient.RECORD_CREATE) ).prepare((people_cluster[0], attrs2))
tx = client.tx_commit()
tx.begin()
tx.attach(rec_position2)
tx.commit()
# This returns 'dummy', 'foo' and 'another_me', but the people created in the tx is not present
res = client.command("select from People")
print(res[0]) => {'@People':{'age': 21, 'name': 'dummy', 'version':2,'rid':'#13:0'}
print(res[1]) => {'@People':{'name': 'foo'},'version':1,'rid':'#13:1'}
print(res[2]) => {'@People':{'age': 31, 'name': 'another_me'},'version':1,'rid':'#13:2'}
# The ones created in the transaction are found in the cluster, but with no class
print(client.command("select from #3:0")[0]) => {{'name': 'me', 'age': 30},'version':1,'rid':'#3:0'}
When I check on studio webapp the vertex created in the tx has class = 'Unknown'
I'm looking through the code but cant find where the problem might be.
Thanks!
Hi @sebastiandev ,
from the code you posted i can see some errors, ther transaction handler must be instantiated with client.tx_commit() method and the CRUD methods must be used between begin and commit methods:
tx = client.tx_commit()
tx.begin()
attrs = {'@People': {'name': 'another_me', 'age': 31}}
res = client.record_create(people_cluster[0], attrs)
tx.commit()
Please refer to https://github.com/mogui/pyorient#transactions for a better example.
ok the tx.tx_commit() is a typo. I meant client.tx_commit()
But in the factory test you create the msgs before creating the tx, then attach and run. Also note that the records get created, only that in a different cluster, with no class.
Also note that i think you are missing the tx.attach(cmd) as in the examples:
attrs = {'@People': {'name': 'another_me', 'age': 31}}
res = client.record_create(people_cluster[0], attrs)
tx.attach( res)
res = tx.commit()
I know using it like a factory is the same as initiating the tx and then issuing record_create, because there's an if checking for global transaction flag, but in the end is the same, command gets prepared only, and the transaction sends them on commit. Right?
Right, i missed the attach method. Yes the command are only prepared inside an ordered list of commands.
In the example i created some records before transaction to show the result ofthe update and delete methods called inside the transaction.
ok, i see that doing that actually works fine. But I'm wondering why doing
tx = client.tx_commit()
tx.begin()
attrs2 = {'@People': {'name': 'me', 'age': 30}}
rec_position2 = client.get_message(pyorient.RECORD_CREATE).prepare((people_cluster[0], attrs2))
tx.attach(rec_position2)
tx.commit()
doesnt work. Isn't the same as doing rec_position2 = record_create(people_cluster[0], attrs2) ??
or even creating the command before the transaction and then attach it, that should be the same, since we are only preparing the command to execute it later.
I also have an issue with transactions:
attr = { '@Topic': {'name': 'nameTopicHere', 'idSource': 15}} cluster_id = 11 #check TODO how to get it!? client.record_create(11, attr )
works ok, and I can see on the console it is inserted.
I try:
tx = client.tx_commit() tx.begin() q = client.record_create(cluster_id, attr) tx.attach(q) res = tx.commit()
I have no errors in python, but no records are created in console.
Can you shed light please?