Class: DataObjects::NestedTransaction

Inherits:
Transaction
  • Object
show all
Defined in:
lib/dm-nested-transactions.rb

Constant Summary collapse

HOST =

The host name. Note, this relies on the host name being configured and resolvable using DNS

"#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
@@counter =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, connection, adapter) ⇒ NestedTransaction

Creates a NestedTransaction bound to an existing connection



64
65
66
67
68
# File 'lib/dm-nested-transactions.rb', line 64

def initialize(uri, connection, adapter)
  @adapter    = adapter
  @connection = connection
  @id = Digest::SHA256.hexdigest("#{HOST}:#{$$}:#{Time.now.to_f}:nested:#{@@counter += 1}")[0..10]
end

Instance Attribute Details

#connectionObject (readonly)

The connection object for this transaction - must have already had a transaction begun on it



52
53
54
# File 'lib/dm-nested-transactions.rb', line 52

def connection
  @connection
end

#idObject (readonly)

A unique ID for this transaction



54
55
56
# File 'lib/dm-nested-transactions.rb', line 54

def id
  @id
end

Class Method Details

.create_for_uri(uri, connection, adapter) ⇒ Object



56
57
58
59
# File 'lib/dm-nested-transactions.rb', line 56

def self.create_for_uri(uri, connection, adapter)
  uri = uri.is_a?(String) ? URI::parse(uri) : uri
  DataObjects::NestedTransaction.new(uri, connection, adapter)
end

Instance Method Details

#beginObject



90
91
92
# File 'lib/dm-nested-transactions.rb', line 90

def begin
  connection.create_command(begin_statement.call(@id)).execute_non_query
end

#begin_statementObject



70
71
72
# File 'lib/dm-nested-transactions.rb', line 70

def begin_statement
  lambda{ |id| "SAVEPOINT \"#{id}\"" }
end

#closeObject



87
88
# File 'lib/dm-nested-transactions.rb', line 87

def close
end

#commitObject



94
95
96
97
98
99
# File 'lib/dm-nested-transactions.rb', line 94

def commit
  statement = commit_statement
  if statement
    connection.create_command(commit_statement.call(@id)).execute_non_query
  end
end

#commit_statementObject



74
75
76
77
78
79
80
81
# File 'lib/dm-nested-transactions.rb', line 74

def commit_statement
  case @adapter.class.name
  when /oracle/i
    nil
  else
    lambda{ |id| "RELEASE SAVEPOINT \"#{id}\"" }
  end
end

#rollbackObject



101
102
103
# File 'lib/dm-nested-transactions.rb', line 101

def rollback
  connection.create_command(rollback_statement.call(@id)).execute_non_query
end

#rollback_statementObject



83
84
85
# File 'lib/dm-nested-transactions.rb', line 83

def rollback_statement
  lambda{ |id| "ROLLBACK TO SAVEPOINT \"#{id}\"" }
end