Class: DB::Context::Transaction

Inherits:
Session
  • Object
show all
Defined in:
lib/db/context/transaction.rb

Overview

A database transaction context that extends Session with transaction management capabilities.

Instance Attribute Summary

Attributes inherited from Session

#connection, #pool

Instance Method Summary collapse

Methods inherited from Session

#call, #clause, #close, #closed?, #connect!, #initialize, #query, #with_connection

Constructor Details

This class inherits a constructor from DB::Context::Session

Instance Method Details

#abortObject

Abort the transaction and return the connection to the connection pool.



33
34
35
36
# File 'lib/db/context/transaction.rb', line 33

def abort
	self.call("ROLLBACK")
	self.close
end

#beginObject

Begin a transaction.



13
14
15
16
# File 'lib/db/context/transaction.rb', line 13

def begin
	self.connect!
	self.call("BEGIN")
end

#commitObject

Commit the transaction and return the connection to the connection pool.



19
20
21
22
# File 'lib/db/context/transaction.rb', line 19

def commit
	self.call("COMMIT")
	self.close
end

#commit?Boolean

Commit the transaction if it’s still open, otherwise do nothing. This is a safe version of commit that checks if the transaction is still active.

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/db/context/transaction.rb', line 26

def commit?
	unless self.closed?
		self.commit
	end
end

#rollback(name) ⇒ Object

Return back to a previously registered savepoint.



44
45
46
# File 'lib/db/context/transaction.rb', line 44

def rollback(name)
	self.call("ROLLBACK #{name}")
end

#savepoint(name) ⇒ Object

Mark a savepoint in the transaction.



39
40
41
# File 'lib/db/context/transaction.rb', line 39

def savepoint(name)
	self.call("SAVEPOINT #{name}")
end