Class: Rod::Berkeley::Transaction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Transaction

Initializes the transaction within the given environment.



8
9
10
11
12
# File 'lib/rod/berkeley/transaction.rb', line 8

def initialize(environment)
  @environment = environment
  @started = false
  @finished = false
end

Instance Attribute Details

#environmentObject (readonly)

The environment of the transaction.



5
6
7
# File 'lib/rod/berkeley/transaction.rb', line 5

def environment
  @environment
end

Instance Method Details

#abortObject

Abort the transaction.

Raises:



85
86
87
88
89
# File 'lib/rod/berkeley/transaction.rb', line 85

def abort
  raise DatabaseError.new("The transaction has not been started!") unless started?
  _abort
  @finished = true
end

#begin(options = {}) ⇒ Object

Begins the transaction.

The following options are supported (see the Berkeley DB documentation for full description of the flags):

  • +:read_committed: - degree 2 isolation of the transaction, BDB: DB_READ_COMMITTED

  • +:read_uncommitted: - degree 1 isolation of the transaction, BDB: DB_READ_UNCOMMITTED

  • +:bulk: - enable transactional bulk insert, NOT SUPPORTED! BDB: DB_TXN_BULK

  • +:no_sync: - do not flush synchronously to the log (ACI without D), BDB: DB_TXN_NOSYNC

  • +:no_wait: - raises exception if can’t obtain a lock immediately, BDB: DB_TXN_NOWAIT

  • +:snapshot: - snapshot isolation of the transaction, BDB: DB_TXN_SNAPSHOT

  • +:sync: - (default) flush synchronously to the log (full ACID), BDB: DB_TXN_SYNC

  • +:wait: - (default) wait for locks if they are not available, BDB: DB_TXN_WAIT

  • +:write_no_sync: - write to the log but don’t flush it, similar to :no_sync, BDB: DB_TXN_WRITE_NOSYNC

Raises:



36
37
38
39
40
# File 'lib/rod/berkeley/transaction.rb', line 36

def begin(options={})
  raise DatabaseError.new("The transaction has already started.") if started?
  _begin(options)
  @started = true
end

#commit(options = {}) ⇒ Object

Commit the transaction.

The following options are supported (see the Berkeley DB documentation for full description of the flags):

  • +:no_sync: - do not flush synchronously to the log (ACI without D), BDB: DB_TXN_NOSYNC

  • +:sync: - (default) flush synchronously to the log (full ACID), BDB: DB_TXN_SYNC

  • +:write_no_sync: - write to the log but don’t flush it, similar to :no_sync, BDB: DB_TXN_WRITE_NOSYNC

Raises:



78
79
80
81
82
# File 'lib/rod/berkeley/transaction.rb', line 78

def commit(options={})
  raise DatabaseError.new("The transaction has not been started!") unless started?
  _commit(options)
  @finished = true
end

#finishObject

Aborts the transaction if it was started and not finished otherwise leaves it as it is.



62
63
64
65
66
# File 'lib/rod/berkeley/transaction.rb', line 62

def finish
  return unless started?
  return if finished?
  self.abort
end

#finished?Boolean

Returns true if the transaction was finished.

Returns:

  • (Boolean)


48
49
50
# File 'lib/rod/berkeley/transaction.rb', line 48

def finished?
  @finished
end

#resetObject

Restarts the transaction if it was finished. This allows for preservation of resources.

Raises:



54
55
56
57
58
# File 'lib/rod/berkeley/transaction.rb', line 54

def reset
  raise DatabaseError.new("Transaction not finished!") if started? && !finished?
  @started = false
  @finished = false
end

#started?Boolean

Returns true if the transaction was started.

Returns:

  • (Boolean)


43
44
45
# File 'lib/rod/berkeley/transaction.rb', line 43

def started?
  @started
end