@open_transactions initialized with nil


#1

Hi Folks,

while using the Masochism plugin for Master-Slave database setup
(http://github.com/technoweenie/masochism/tree/master) I discovered a
problem
with starting transactions in the ConnectionProxy:

79 def transaction(start_db_transaction = true, &block)
80 with_master(start_db_transaction) do
81 master.transaction(start_db_transaction, &block)
82 end
83 end

Despite there is a problem in Masochism itself I discovered there is
(at least
for me) a strange behavior in the AbstractAdapter within ActiveRecord:

158 def decrement_open_transactions
159 @open_transactions -= 1
160 end

In some cases there is the decrement_open_transactions method called
for
connections with an uninitialized @open_transactions variable (the
value is
nil). I tracked this down to the initialize method in the same class.
Besides
other initializations the @open_transactions variable is not
initialized:

37 def initialize(connection, logger = nil) #:nodoc:
38 @connection, @logger = connection, logger
39 @runtime = 0
40 @last_verification = 0
41 @query_cache_enabled = false
42 end

Is there a reason to init the variable via the attribute accessor?

149 def open_transactions
150 @open_transactions ||= 0
151 end

Thanks in advance!

Dirk


#2

On 13 Feb 2009, at 09:07, dbreuer wrote:

Is there a reason to init the variable via the attribute accessor?

149 def open_transactions
150 @open_transactions ||= 0
151 end

I suspect not. You might find the rubyonrails-core list (or the #rails-
contrib irc channel) can provide a more definitive answer.

Fred