Class: Rooq::ConnectionProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/rooq/connection.rb

Overview

Abstract interface for connection lifecycle management. Implementations control how connections are acquired and released.

This is inspired by jOOQ's ConnectionProvider interface.

Instance Method Summary collapse

Instance Method Details

#acquireObject

Acquire a connection for query execution.

Returns:

  • (Object)

    a database connection

Raises:

  • (NotImplementedError)

    if not implemented by subclass



13
14
15
# File 'lib/rooq/connection.rb', line 13

def acquire
  raise NotImplementedError, "#{self.class} must implement #acquire"
end

#release(connection) ⇒ Object

Release a previously acquired connection.

Parameters:

  • connection (Object)

    the connection to release

Raises:

  • (NotImplementedError)

    if not implemented by subclass



20
21
22
# File 'lib/rooq/connection.rb', line 20

def release(connection)
  raise NotImplementedError, "#{self.class} must implement #release"
end

#with_connection {|connection| ... } ⇒ Object

Execute a block with an acquired connection, ensuring release.

Yields:

  • (connection)

    the acquired connection

Returns:

  • (Object)

    the result of the block



27
28
29
30
31
32
33
34
# File 'lib/rooq/connection.rb', line 27

def with_connection
  connection = acquire
  begin
    yield connection
  ensure
    release(connection)
  end
end