# @open\_transactions initialized with nil

**URL:** <https://www.ruby-forum.com/t/open-transactions-initialized-with-nil/160236>\
**Category:** Rails\
**Created:** [February 13, 2009, 10:08am UTC](https://www.ruby-forum.com/t/open-transactions-initialized-with-nil/160236 "2009-02-13T10:08:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samwise23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@samwise23](https://www.ruby-forum.com/u/samwise23)\
**Post date:** [February 13, 2009, 10:08am UTC](https://www.ruby-forum.com/t/open-transactions-initialized-with-nil/160236/1 "2009-02-13T10:08:04Z")

</div>

Hi Folks,

while using the Masochism plugin for Master-Slave database setup  
([GitHub - technoweenie/masochism: ActiveRecord connection proxy for master/slave connections](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

---

<div class="post-metadata">

**Author:** ![samwise23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@samwise23](https://www.ruby-forum.com/u/samwise23)\
**Post date:** [February 13, 2009, 11:35am UTC](https://www.ruby-forum.com/t/open-transactions-initialized-with-nil/160236/2 "2009-02-13T11:35:15Z")

</div>

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
