Class: Syntropy::DB::ConnectionPool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, fn, max_conn) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



10
11
12
13
14
15
16
17
# File 'lib/syntropy/db/connection_pool.rb', line 10

def initialize(machine, fn, max_conn)
  @machine = machine
  @fn = fn
  @count = 0
  @max_conn = max_conn
  @queue = UM::Queue.new
  @key = :"connection_pool_#{object_id}"
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/syntropy/db/connection_pool.rb', line 8

def count
  @count
end

Instance Method Details

#closeObject



43
44
45
46
47
48
49
# File 'lib/syntropy/db/connection_pool.rb', line 43

def close
  while @queue.count > 0
    db = @machine.shift(@queue)
    db.close
    @count -= 1
  end
end

#execute(sql) ⇒ Object



39
40
41
# File 'lib/syntropy/db/connection_pool.rb', line 39

def execute(sql, *, **)
  with_db { it.execute(sql, *, **) }
end

#query(sql) ⇒ Object



35
36
37
# File 'lib/syntropy/db/connection_pool.rb', line 35

def query(sql, *, **, &)
  with_db { it.query(sql, *, **, &) }
end

#with_dbObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/syntropy/db/connection_pool.rb', line 19

def with_db
  if (db = Thread.current[@key])
    @machine.snooze
    return yield(db)
  end

  db = checkout
  begin
    Thread.current[@key] = db
    yield(db)
  ensure
    Thread.current[@key] = nil
    checkin(db)
  end
end