Class: NeverBlock::DB::PooledFiberedMysqlConnection
- Inherits:
-
Object
- Object
- NeverBlock::DB::PooledFiberedMysqlConnection
- Defined in:
- lib/never_block/db/pooled_fibered_mysql_connection.rb
Overview
A pooled postgres connection class. This class represents a proxy interface to a connection pool of fibered postgresql connections.
Instance Method Summary collapse
-
#begin_db_transaction ⇒ Object
This method must be called for transactions to work correctly.
-
#close ⇒ Object
close all connections and remove them from the event loop.
-
#commit_db_transaction ⇒ Object
see =begin_db_transaction.
-
#exec(query) ⇒ Object
A proxy for the connection’s exec method quries the pool to get a connection first.
-
#initialize(size = 4, &block) ⇒ PooledFiberedMysqlConnection
constructor
Requires a hash or an array with connection parameters and a pool size (defaults to 4).
-
#method_missing(method, *args) ⇒ Object
Pass unknown methods to the connection.
-
#respond_to?(method) ⇒ Boolean
Pass method queries to the connection.
-
#rollback_db_transaction ⇒ Object
see =begin_db_transaction.
Constructor Details
#initialize(size = 4, &block) ⇒ PooledFiberedMysqlConnection
Requires a hash or an array with connection parameters and a pool size (defaults to 4)
11 12 13 14 15 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 11 def initialize(size=4, &block) @pool = NB::Pool::FiberedConnectionPool.new(:size=>size, :eager=>true) do yield end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Pass unknown methods to the connection
60 61 62 63 64 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 60 def method_missing(method, *args) @pool.hold do |conn| conn.send(method, *args) end end |
Instance Method Details
#begin_db_transaction ⇒ Object
This method must be called for transactions to work correctly. One cannot just send “begin” as you never know which connection will be available next. This method ensures you get the same connection while in a transaction.
29 30 31 32 33 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 29 def begin_db_transaction @pool.hold(true) do |conn| conn.exec("begin") end end |
#close ⇒ Object
close all connections and remove them from the event loop
52 53 54 55 56 57 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 52 def close @pool.all_connections do |conn| conn.unregister_from_event_loop conn.close end end |
#commit_db_transaction ⇒ Object
see =begin_db_transaction
44 45 46 47 48 49 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 44 def commit_db_transaction @pool.hold do |conn| conn.exec("commit") @pool.release(Fiber.current,conn) end end |
#exec(query) ⇒ Object
A proxy for the connection’s exec method quries the pool to get a connection first
19 20 21 22 23 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 19 def exec(query) @pool.hold do |conn| conn.exec(query) end end |
#respond_to?(method) ⇒ Boolean
Pass method queries to the connection
67 68 69 70 71 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 67 def respond_to?(method) @pool.hold do |conn| conn.respond_to?(method) end end |
#rollback_db_transaction ⇒ Object
see =begin_db_transaction
36 37 38 39 40 41 |
# File 'lib/never_block/db/pooled_fibered_mysql_connection.rb', line 36 def rollback_db_transaction @pool.hold do |conn| conn.exec("rollback") @pool.release(Fiber.current,conn) end end |