Class: FlexiRecord::ConnectionPool

Inherits:
ThreadResourcePool show all
Defined in:
lib/flexirecord.rb

Overview

A pool of database connections to be used exclusively by one thread at a time.

Instance Method Summary collapse

Methods inherited from ThreadResourcePool

#current_resource, #use_resource

Constructor Details

#initialize(connection_options) ⇒ ConnectionPool

Creates a new ConnectionPool which automatically generates and caches Connection objects with the given options.



1507
1508
1509
1510
1511
# File 'lib/flexirecord.rb', line 1507

def initialize(connection_options)
  @connection_options = connection_options.dup
  pool_size = @connection_options.delete(:pool_size)
  super(pool_size || 10)
end

Instance Method Details

#destroy_resource(connection) ⇒ Object

Implementation of ThreadResourcePool#destroy_resource.



1524
1525
1526
# File 'lib/flexirecord.rb', line 1524

def destroy_resource(connection)
  connection.close
end

#generate_resourceObject

Implementation of ThreadResourcePool#generate_resource.



1514
1515
1516
# File 'lib/flexirecord.rb', line 1514

def generate_resource
  FlexiRecord::Connection.new(@connection_options)
end

#reset_resource(connection) ⇒ Object

Implementation of ThreadResourcePool#reset_resource.



1519
1520
1521
# File 'lib/flexirecord.rb', line 1519

def reset_resource(connection)
  not connection.spoiled?
end

#transaction(*arguments) ⇒ Object

Wrapper for Connection#transaction for the Connection of the current thread.



1535
1536
1537
1538
1539
1540
1541
# File 'lib/flexirecord.rb', line 1535

def transaction(*arguments)
  use_connection do |connection|
    connection.transaction(*arguments) do
      return yield
    end
  end
end

#use_connection(*args, &block) ⇒ Object

Passes a Connection object to the given block, which then can be used by the current thread.



1530
1531
1532
# File 'lib/flexirecord.rb', line 1530

def use_connection(*args, &block)
  use_resource(*args, &block)
end