0

I am trying to run a cypher query in py2neo and overcome some restrictions. I actually want to add some weight to the edges of my graph for a specific execution but after the execution of the program I don't want the changes to remain on my neo4j DB (Rollback). I want this in order to run the program/query with the edge weights as parameters every time Thanks in advance!

panomi
  • 21
  • 3

1 Answers1

0

It depends on timing. I believe py2neo uses the transactional cypher http endpoint. Rollback is possible before a transaction has been committed or finished, not after.

So let's say you're running your cypher query and doing other things at the same time.

tx = graph.cypher.begin()
statement = "some nifty mutating cypher in here"
tx.append(statement)
tx.commit()

By the time you hit commit you're done. The database doesn't necessarily work like git where you can undo any previous past change, or revert to a previous state of the database at a certain time. Generally you're creating transactions, then you're committing them, or rolling them back.

So, you can roll-back a transaction if you have not yet finished/committed it. This is useful because your transaction might include 8-9 different queries, all making changes. What if the first 4 succeed, then #5 fails? That's really what transaction rollback is meant to address, to make a big multi-query change into something atomic, that either all works, or all doesn't.

If this isn't what you want, then you should probably formulate a second cypher query which undoes whatever changes you're making to your weights. An example might be a pair of queries like this:

MATCH (a:Node) 
SET a.old_weight=a.weight 
WITH a 
SET a.weight={myNewValue} 
RETURN a;

Then undo it with:

MATCH (a:Node) 
SET a.weight=a.old_weight 
WITH a 
DELETE a.old_weight
RETURN a;

Here's further documentation on transactions from the java API that describes a bit more how they work.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • I found your answer really helpful. Actually my purpose is to run dijkstraa algorithm to compute some score (the smallest possible) for every node in comparison to the starting one. So undoing the changes on the weights before I commit may work for me! Thanks in advance – panomi Oct 18 '15 at 17:42