Class: PGx::Connection
- Inherits:
-
Object
- Object
- PGx::Connection
- Defined in:
- lib/pgx.rb
Overview
Extends the PG::Connection methods by adding logging and retrying capabilities (synchronous methods only.) The arguments and method behaviors are identical, except for the ‘new’ method.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Connection
constructor
A new instance of Connection.
- #method_missing(m, *args, &block) ⇒ Object
- #prepare(*args, &block) ⇒ Object
- #reset(*args, &block) ⇒ Object
- #transaction(*args, &block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Connection
Returns a new instance of Connection.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pgx.rb', line 86 def initialize(*args) connect_args = PGx.const_get('CONNECT_ARGS') (our_args, pg_args) = extract_our_args(connect_args, args) log = arg_with_default(connect_args, our_args, :logger) @logger = [ log.is_a?(Proc) ? log.call : log ].flatten @connect_init = arg_with_default(connect_args, our_args, :connect_init) @connect_retry_sleep = arg_with_default(connect_args, our_args, :connect_retry_sleep) @method_try_count_max = arg_with_default(connect_args, our_args, :method_try_count_max) @method_retriable_error_states = arg_with_default(connect_args, our_args, :method_retriable_error_states) # gets set in connect_log_and_try() to the connection handle # needed by reset() to be able to execute the connect_init proc @connection = nil # always include invalid_sql_statement_name (required for prepared statements to work after re-connects) @method_retriable_error_states.push('26000') unless @method_retriable_error_states.include?('26000') @in_transaction_block = false @prepared_statements = {} # hash of all statements ever prepared, key = statement name, value = sql @prepared_live = {} # keeps track which prepared statements are currently prepared @transaction_reprepare = nil # placeholder for statement to be prepared before transaction retry @connect_proc = lambda { @prepared_live = {} # reset on connect connect_log_and_try( lambda { PG::Connection.new(*pg_args) }, our_args, pg_args ) } connect_loop() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
121 122 123 |
# File 'lib/pgx.rb', line 121 def method_missing(m, *args, &block) sql_log_and_try(lambda { @connection.send(m, *args, &block) }, m, args) end |
Instance Method Details
#prepare(*args, &block) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/pgx.rb', line 146 def prepare(*args, &block) # it's possible to call prepare() on an statement that is already prepared # if you have a prepare() inside a transaction, and the transaction is retried # thus we check if it's already live s = args[0] r = nil if @prepared_live.has_key?(s) log_debug %Q{prepared statement #{s} is already live, skipping re-preparing it} else r = sql_log_and_try(lambda { @connection.prepare(*args, &block) }, __method__, args) @prepared_statements[s] = args[1] @prepared_live[s] = true end r end |
#reset(*args, &block) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/pgx.rb', line 162 def reset(*args, &block) r = sql_log_and_try(lambda { @connection.reset(*args, &block) }, __method__, args) @prepared_live = {} # reset on connect @connect_init.call(@connection) r end |
#transaction(*args, &block) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/pgx.rb', line 131 def transaction(*args, &block) @in_transaction_block = true r = sql_log_and_try(lambda { @connection.transaction(*args, &block) }, __method__, args) @in_transaction_block = false r end |