Class: Neo4j::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/transaction.rb

Overview

All modifying operations that work with the node space must be wrapped in a transaction. Transactions are thread confined. Neo4j does not implement true nested transaction, instead it uses flat nested transactions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(instance = Neo4j.started_db) ⇒ Java::OrgNeo4jGraphdb::Transaction

Starts a new Neo4j Transaction

Examples:

tx = Neo4j::Transaction.new
# modify something
tx.success
tx.finish

Returns:

  • (Java::OrgNeo4jGraphdb::Transaction)

    a Java Neo4j Transaction object

See Also:



37
38
39
# File 'lib/neo4j/transaction.rb', line 37

def self.new(instance = Neo4j.started_db)
  instance.begin_tx
end

.run {|| ... } ⇒ Object

Runs a block in a Neo4j transaction

Many operations on neo requires an transaction. You will get much better performance if one transaction is wrapped around several neo operation instead of running one transaction per neo operation. If one transaction is already running then a ‘placebo’ transaction will be created. Performing a finish on a placebo transaction will not finish the ‘real’ transaction.

If an exception occurs inside the block the transaction will rollback automatically.

Examples:


Neo4j::Transaction.run { node = PersonNode.new }

access to the transaction and rollback


Neo4j::Transaction.run do |t|
  # something failed
  t.failure # will cause a rollback
end

Yields:

  • the block which should be run under one transaction

Yield Parameters:

Returns:

  • The value of the evaluated provided block

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/neo4j/transaction.rb', line 66

def self.run
  raise ArgumentError.new("Expected a block to run in Transaction.run") unless block_given?

  begin
    tx = Neo4j::Transaction.new
    ret = yield tx
    tx.success
  rescue Exception => e
    if Neo4j::Config[:debug_java] && e.respond_to?(:cause)
      puts "Java Exception in a transaction, cause: #{e.cause}"
      e.cause.print_stack_trace
    end
    tx.failure unless tx.nil?
    raise
  ensure
    tx.finish unless tx.nil?
  end
  ret
end

Instance Method Details

#acquire_read_lock(java_entity) ⇒ Java::OrgNeo4jGraphdb::Lock

Acquires a read lock for entity for this transaction. The lock (returned from this method) can be released manually, but if not it’s released automatically when the transaction finishes. There is no implementation for this here because it’s an java method

Parameters:

  • java_entity (Neo4j::Relationship, Neo4j::Node)

    the entity to acquire a lock for. If another transaction currently hold a write lock to that entity this call will wait until it’s released.

Returns:

  • (Java::OrgNeo4jGraphdb::Lock)

    a Lock which optionally can be used to release this lock earlier than when the transaction finishes. If not released (with Lock.release() it’s going to be released with the transaction finishes.

See Also:



25
26
# File 'lib/neo4j/transaction.rb', line 25

def acquire_read_lock(java_entity)
end

#acquire_write_lock(java_entity) ⇒ Java::OrgNeo4jGraphdb::Lock

Acquires a write lock for entity for this transaction. The lock (returned from this method) can be released manually, but if not it’s released automatically when the transaction finishes. There is no implementation for this here because it’s an java method

Parameters:

  • java_entity (Neo4j::Relationship, Neo4j::Node)

    the entity to acquire a lock for. If another transaction currently holds a write lock to that entity this call will wait until it’s released.

Returns:

  • (Java::OrgNeo4jGraphdb::Lock)

    a Lock which optionally can be used to release this lock earlier than when the transaction finishes. If not released (with Lock.release() it’s going to be released with the transaction finishes.

See Also:



16
17
# File 'lib/neo4j/transaction.rb', line 16

def acquire_write_lock(java_entity)
end