0

I'm trying to introduce a transaction using py2neo. At the beginning of the code to be transactional I open new transaction using:

tx = graph.cypher.begin()

after that there are several actions taken:

  1. Read some data already stored in the database before transaction has started
  2. Create new node based on that
  3. Create another node
  4. Create the relationship between those two nodes
  5. Read that relationship using node.match_outgoing(relation_type)

While actions 1-4 are done correctly, the last one fails on "node not bound". This is no suprise as transaction haven't been commited yet and there is no remote counterpart for that node. Still, I need to do it like this. The other approach would require a huge refactoring and a spaghetti code.

For creating nodes in transaction I'm using CreateStatement from cypher.It's done like that:

graph = db_connection.get_graph()
statement = CreateStatement(graph)
statement.create(relation)
append_to_current_tx(statement)

So there is no way of getting the node that is in bound-like state (a transient one).

Is there a way to read relationships for the node that was just created in a transaction that is still uncommited (but all of it's relationships are created before in the same transaction). Are there nested transactions in py2neo?

kboom
  • 2,279
  • 3
  • 28
  • 43

1 Answers1

1

You can accomplish this with tx.process() which will send all pending statements to the server to be executed, but will leave the transaction open and uncommitted.

For example:

tx = graph.cypher.begin()
tx.append('CREATE (:Person{name: 'Bob'})-[r:EATS]->(:Food {name: 'Pizza'}) SET r.count=1')
tx.append('''
    MATCH (:Person{name: 'Bob'})-[r:EATS]->(:Food {name: 'Pizza'})
    SET r.count = r.count + 1
    RETURN r.count AS count
''')
result = tx.process()
for record in result:
    print(record.one) # count will be 2 even though the transaction has not been committed
tx.commit()
William Lyon
  • 8,371
  • 1
  • 17
  • 22
  • Yeah but using node = graph.create(node) i was able to get a bound node and using transactions I cannot do this. I'm left with a node not bound to any remote counterpart. Do I need a separate query to retrieve such entity again...? – kboom Feb 17 '16 at 12:39