Transactional REST API

85 views
Skip to first unread message

Gorka Lertxundi

unread,
Jan 25, 2014, 5:42:56 AM1/25/14
to ne...@googlegroups.com
I started developing a neo4j client and when I reach to the transactional endpoint there's something i'm not sure about:

Which is the best option regarding to multiple 'MATCH' statements? (no updates, no creates):
  - Multiple queries directly to /transaction/commit (creating a new transaction each one?)
  - Create a transaction and then do MATCHes in it /transaction/5 (do commit :\ after all?)

rgds,

Michael Hunger

unread,
Jan 26, 2014, 12:40:18 AM1/26/14
to ne...@googlegroups.com
Thanks a lot for your engagement! What language / environment are you developing the driver for?

This is most efficient:
- Multiple queries directly to /transaction/commit (creating a new transaction each one?)

It depends on the usage though, if decisions for the following queries are made based on the results of previous ones then you'll probably want to support multiple requests within a single tx.

Cheers

Michael



--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nigel Small

unread,
Jan 26, 2014, 4:50:55 AM1/26/14
to Neo4J
Hi Gorka

If it helps, have a look at the way I've presented the transactional endpoint in py2neo:


At the top level is a Session object that maintains the root URI for the database then, from that, transactions can be created. For each transaction, it's possible to add any number of statements and send these to the server for execution or commit/rollback as required. The precise REST endpoint used varies depending on the order in which executes and commits are received.

For example:
from py2neo import cypher

session = cypher.Session("http://localhost:7474")
tx = session.create_transaction()

# send three statements to for execution but leave the transaction open
tx.append("MERGE (a:Person {name:'Alice'})")
tx.append("MERGE (b:Person {name:'Bob'})")
tx.append("CREATE UNIQUE (a)-[:KNOWS]->(b)")
tx.execute()

# send another three statements and commit the transaction
tx.append("MERGE (c:Person {name:'Carol'})")
tx.append("MERGE (d:Person {name:'Dave'})")
tx.append("CREATE UNIQUE (c)-[:KNOWS]->(d)")
tx.commit()
Each execute() or commit() method call will use one of four possible URIs:
  • BEGIN -> /db/data/transaction
  • BEGIN_COMMIT -> /db/data/transaction/commit
  • EXECUTE -> /db/data/transaction/{id}
  • COMMIT -> /db/data/transaction/{id}/commit
If the transaction ID is unknown (because no calls have previously been made) then the execute() method will use the BEGIN endpoint, otherwise it will use EXECUTE. Similarly, the commit() method will use either BEGIN_COMMIT or COMMIT. The actual code behind these methods is here:


By the way, I can't claim credit for this approach. It was inspired by a conversation with Tatham Oddie, author of the .NET driver, at GraphConnect SF last year :-)

Cheers
Nige

Michael Hunger

unread,
Jan 26, 2014, 5:31:57 AM1/26/14
to ne...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages