class

DB::Database

Inherits DB::ConnectionContext / DB::SessionMethods / DB::QueryMethods / Reference / Object

Acts as an entry point for database access. Connections are managed by a pool. Use DB#open to create a Database instance.

Refer to QueryMethods and SessionMethods for documentation about querying the database.

Database URI

Connection parameters are usually in a URI. The format is specified by the individual database drivers, yet there are some common properties names usually shared. See the reference book for examples.

The connection pool can be configured from URI parameters:

  • initial_pool_size (default 1)
  • max_pool_size (default 0 = unlimited)
  • max_idle_pool_size (default 1)
  • checkout_timeout (default 5.0)
  • retry_attempts (default 1)
  • retry_delay (in seconds, default 1.0)

When querying a database, prepared statements are used by default. This can be changed from the prepared_statements URI parameter:

  • prepared_statements (true, or false, default true)

Constructors

new(connection_options : Connection::Options, pool_options : Pool::Options, &factory : -> Connection)

Initialize a database with the specified options and connection factory. This covers more advanced use cases that might not be supported by an URI connection string such as tunneling connection.

Source

Instance methods

checkout

returns a connection from the pool the returned connection must be returned to the pool by explictly calling Connection#release

Source
close

Closes all connection to the database.

Source
prepared_statements?

Returns whether by default the statements should be prepared or not.

Source
prepared_statements_cache?
Source
setup_connection

Run the specified block every time a new connection is established, yielding the new connection to the block.

db = DB.open(DB_URL)
db.setup_connection do |connection|
  connection.exec "SET TIME ZONE 'America/New_York'"
end
Source
transaction

yields a Transaction from a connection of the pool Refer to BeginTransaction#transaction for documentation.

Source
using_connection

yields a connection from the pool the connection is returned to the pool when the block ends

Source