Permalink
Browse files

Fix issue with XA nesting in sqlite

  • Loading branch information...
1 parent 7d7a2ac commit 5a5ca9babb1456ce413d3702ee8b480e47095258 @mperham mperham committed Nov 30, 2008
Showing with 19 additions and 16 deletions.
  1. +1 −1 example/app/models/figment.rb
  2. +16 −13 lib/data_fabric/ar22.rb
  3. +2 −1 test/database_test.rb
  4. +0 −1 test/test_helper.rb
View
2 example/app/models/figment.rb
@@ -1,4 +1,4 @@
class Figment < ActiveRecord::Base
- connection_topology :shard_by => 'shard', :replicated => false
+ data_fabric :shard_by => 'shard', :replicated => false
belongs_to :account
end
View
29 lib/data_fabric/ar22.rb
@@ -22,7 +22,7 @@ def connected?
end
def remove_connection(klass)
- raise "not implemented"
+ DataFabric.log(Logger::ERROR) { "remove_connection not implemented by data_fabric" }
end
def connection_pool
@@ -52,12 +52,15 @@ def initialize(model_class, options)
delegate :insert_many, :to => :master # ar-extensions bulk insert support
- def cache(&block)
- connection.cache(&block)
- end
-
def transaction(start_db_transaction = true, &block)
- with_master { connection.transaction(start_db_transaction, &block) }
+ # Transaction is not re-entrant in SQLite 3 so we
+ # need to track if we've already started an XA to avoid
+ # calling it twice.
+ return yield if in_transaction?
+
+ with_master do
+ connection.transaction(start_db_transaction, &block)
+ end
end
def method_missing(method, *args, &block)
@@ -69,10 +72,6 @@ def connection_name
connection_name_builder.join('_')
end
- def verify!(arg)
- connection.verify!(arg) if connected?
- end
-
def with_master
# Allow nesting of with_master.
old_role = current_role
@@ -84,6 +83,10 @@ def with_master
private
+ def in_transaction?
+ current_role == 'master'
+ end
+
def current_pool
name = connection_name
self.class.shard_pools[name] ||= begin
@@ -131,12 +134,12 @@ def connection
current_pool.connection
end
- def connected?
- DataFabric.shard_active_for?(@shard_group) and cached_connections[connection_name]
+ def active?
+ DataFabric.shard_active_for?(@shard_group)
end
def set_role(role)
- Thread.current[:data_fabric_role] = role if @replicated
+ Thread.current[:data_fabric_role] = role
end
def current_role
View
3 test/database_test.rb
@@ -27,11 +27,12 @@ def test_live_burrito
# ...but immediately set it back to default to the slave
assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
-
+
# Should use the master
TheWholeBurrito.transaction do
burrito = TheWholeBurrito.find(1)
assert_match 'vr_dallas_master', burrito.name
+ burrito.name = 'foo'
burrito.save!
end
end
View
1 test/test_helper.rb
@@ -17,7 +17,6 @@
require 'active_record/version'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::WARN
-ActiveRecord::Base.logger = Logger.new(STDOUT)
# Bootstrap DF
deps = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : Dependencies

0 comments on commit 5a5ca9b

Please sign in to comment.