Forum: Ruby on Rails @open_transactions initialized with nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b6f3bdd655ba2a19cda645e2150f34d?d=identicon&s=25 Dirk Breuer (samwise23)
on 2009-02-13 10:08
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-13 11:35
(Received via mailing list)
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
This topic is locked and can not be replied to.