jam-cloud/ruby/lib/jam_ruby/base_manager.rb

31 lines
946 B
Ruby
Raw Normal View History

2012-11-12 12:28:44 +00:00
module JamRuby
class BaseManager
attr_accessor :pg_conn
def initialize(options={})
@log = Logging.logger[self]
@pg_conn = options[:conn]
unless PG.threadsafe?
raise Exception, "a non-threadsafe build of libpq is present."
end
end
# Creates a connection manager, and associates the connection created by active_record with ourselves
2012-11-30 09:34:50 +00:00
def self.active_record_transaction
2012-11-12 12:28:44 +00:00
manager = self.new
ActiveRecord::Base.connection_pool.with_connection do |connection|
# create a transaction, and pass the current connection to ConnectionManager.
# this lets the entire operation work with the same transaction,
# across Rails ActiveRecord and the pg-gem based code in ConnectionManager.
manager.pg_conn = connection.instance_variable_get("@connection")
connection.transaction do
2012-11-30 09:34:50 +00:00
yield manager
2012-11-12 12:28:44 +00:00
end
end
end
end
end