Skip to content
Brian Underwood edited this page May 8, 2015 · 12 revisions

Transactions

API

Examples

By default each Neo4j operation is wrapped in an transaction. If you want to execute several operation in one operation you can use the Neo4j::Transaction class, example:

Neo4j::Transaction.run do
  n = Neo4j::Node.create(name: 'kalle')
  n[:age] = 42
end

Rollback occurs if an exception is thrown, or the failure method is called on the transaction.

E.g.

Neo4j::Transaction.run do |tx|
  n = Neo4j::Node.create(name: 'kalle')
  tx.failure # all operations inside this tx will be rollbacked
  n[:age] = 42
end

Alternatively, you can start a transaction by calling Neo4j::Transaction.new. Be aware that if you do this, it will not close automatically and will not catch errors, so you will need to handle this manually. This is useful if you want to rescue a specific error and have additional logic to be performed in the event of a failure.

begin
  tx = Neo4j::Transaction.new
  # your code
rescue MyErrorClass
  tx.failure
ensure
  tx.close
end

Limitations

  • The query api (example Neo4j::Session.query, Person.where, or a_person.friends) can only return properties because limitations in the Neo4j Server Rest API. However, this works in the embedded db, see issue 440 Corrected with release 3.0.1. Neo4j-core has full support for the transactional endpoint.
  • Transactions are thread confined and can be nested as "flat nested transactions"

WARNING: Much of the information in this wiki is out of day. We are in the process of moving things to readthedocs

Api

Contribute

Clone this wiki locally