Class: DB::Client
- Inherits:
-
Object
- Object
- DB::Client
- Defined in:
- lib/db/client.rb
Overview
Binds a connection pool to the specified adapter.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
The adapter used for making connections.
Instance Method Summary collapse
-
#close ⇒ Object
Close all open connections in the connection pool.
-
#initialize(adapter, **options) ⇒ Client
constructor
Initialize the client and internal connection pool using the specified adapter.
-
#session(**options) ⇒ Object
(also: #context)
Acquires a connection and sends the specified statement if given.
-
#transaction(**options) ⇒ Object
Acquires a connection and starts a transaction.
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, **) @adapter = adapter @pool = connect(**) end |
Instance Attribute Details
#adapter ⇒ Object
The adapter used for making connections.
24 25 26 |
# File 'lib/db/client.rb', line 24 def adapter @adapter end |
Instance Method Details
#close ⇒ Object
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(**) session = Context::Session.new(@pool, **) 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(**) transaction = Context::Transaction.new(@pool, **) transaction.begin return transaction unless block_given? begin yield transaction rescue transaction.abort raise ensure transaction.commit? end end |