Class: DB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/db/client.rb

Overview

Binds a connection pool to the specified adapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, **options) ⇒ Client

Initialize the client and internal connection pool using the specified adapter.



16
17
18
19
20
# File 'lib/db/client.rb', line 16

def initialize(adapter, **options)
	@adapter = adapter
	
	@pool = connect(**options)
end

Instance Attribute Details

#adapterObject

The adapter used for making connections.



24
25
26
# File 'lib/db/client.rb', line 24

def adapter
  @adapter
end

Instance Method Details

#closeObject

Close all open connections in the connection pool.



27
28
29
30
31
32
33
# File 'lib/db/client.rb', line 27

def close
	@pool.wait_until_free do
		Console.warn(self) {"Waiting for #{@adapter} pool to drain: #{@pool}"}
	end
	
	@pool.close
end

#session(**options) ⇒ Object Also known as: context

Acquires a connection and sends the specified statement if given.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/db/client.rb', line 40

def session(**options)
	session = Context::Session.new(@pool, **options)
	
	return session unless block_given?
	
	begin
		session.connect!
		
		yield session
	ensure
		session.close
	end
end

#transaction(**options) ⇒ Object

Acquires a connection and starts a transaction.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/db/client.rb', line 61

def transaction(**options)
	transaction = Context::Transaction.new(@pool, **options)
	
	transaction.begin
	
	return transaction unless block_given?
	
	begin
		yield transaction
		
	rescue
		transaction.abort
		raise
	ensure
		transaction.commit?
	end
end