Module: RDF::Transactable

Included in:
Graph, Repository
Defined in:
lib/rdf/mixin/transactable.rb

Overview

A transaction application mixin.

Classes that include this module must provide a ‘#begin_transaction` method returning an Transaction.

Examples:

running a read/write transaction with block syntax

repository = RDF::Repository.new # or other transactable

repository.transaction(mutable: true) do |tx|
  tx.insert [:node, RDF.type, RDF::OWL.Thing]
  # ...
end

See Also:

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#transaction(mutable: false) ⇒ RDF::Transaction #transaction(mutable: false) {|tx| ... } ⇒ self Also known as: transact

Executes the given block in a transaction.

Raising an error within the transaction block causes automatic rollback.

Examples:

running a transaction

repository.transaction(mutable: true) do |tx|
  tx.insert [RDF::URI("https://rubygems.org/gems/rdf"), RDF::RDFS.label, "RDF.rb"]
end

manipulating a live transaction

tx = repository.transaction(mutable: true)
tx.insert [RDF::URI("https://rubygems.org/gems/rdf"), RDF::RDFS.label, "RDF.rb"]
tx.execute

Overloads:

  • #transaction(mutable: false) ⇒ RDF::Transaction

    Returns an open transaction; the client is responsible for closing the transaction via #execute or #rollback.

    Parameters:

    • mutable (Boolean) (defaults to: false)

    Returns:

    • (RDF::Transaction)

      an open transaction; the client is responsible for closing the transaction via #execute or #rollback

  • #transaction(mutable: false) {|tx| ... } ⇒ self

    Parameters:

    • mutable (Boolean) (defaults to: false)

      allows changes to the transaction, otherwise it is a read-only snapshot of the underlying repository.

    Yields:

    • (tx)

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

    Returns:

    • (self)

See Also:

Since:

  • 0.3.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rdf/mixin/transactable.rb', line 50

def transaction(mutable: false, &block)
  tx = begin_transaction(mutable: mutable)
  return tx unless block_given?

  begin
    case block.arity
      when 1 then block.call(tx)
      else tx.instance_eval(&block)
    end
  rescue => error
    rollback_transaction(tx)
    raise error
  end
  commit_transaction(tx)
  self
end