diff --git a/rails_event_store_active_record/lib/rails_event_store_active_record/event_repository.rb b/rails_event_store_active_record/lib/rails_event_store_active_record/event_repository.rb index f677f75b5e..53aac21e66 100644 --- a/rails_event_store_active_record/lib/rails_event_store_active_record/event_repository.rb +++ b/rails_event_store_active_record/lib/rails_event_store_active_record/event_repository.rb @@ -57,7 +57,7 @@ def count(specification) def update_messages(records) hashes = records.map { |record| import_hash(record, record.serialize(serializer)) } for_update = records.map(&:event_id) - start_transaction do + start_transaction(false) do existing = @event_klass.where(event_id: for_update).pluck(:event_id, :id).to_h (for_update - existing.keys).each{|id| raise RubyEventStore::EventNotFound.new(id) } hashes.each { |h| h[:id] = existing.fetch(h.fetch(:event_id)) } @@ -84,7 +84,7 @@ def add_to_stream(event_ids, stream, expected_version) last_stream_version = ->(stream_) { @stream_klass.where(stream: stream_.name).order("position DESC").first.try(:position) } resolved_version = expected_version.resolve_for(stream, last_stream_version) - start_transaction do + start_transaction(!!resolved_version) do yield if block_given? in_stream = event_ids.map.with_index do |event_id, index| { @@ -132,8 +132,8 @@ def optimize_timestamp(valid_at, created_at) valid_at unless valid_at.eql?(created_at) end - def start_transaction(&block) - @event_klass.transaction(requires_new: true, &block) + def start_transaction(requires_new, &block) + @event_klass.transaction(requires_new: requires_new, &block) end end diff --git a/rails_event_store_active_record/lib/rails_event_store_active_record/pg_linearized_event_repository.rb b/rails_event_store_active_record/lib/rails_event_store_active_record/pg_linearized_event_repository.rb index 585e57c92d..64c783f135 100644 --- a/rails_event_store_active_record/lib/rails_event_store_active_record/pg_linearized_event_repository.rb +++ b/rails_event_store_active_record/lib/rails_event_store_active_record/pg_linearized_event_repository.rb @@ -5,8 +5,8 @@ module RailsEventStoreActiveRecord class PgLinearizedEventRepository < EventRepository - def start_transaction(&proc) - ActiveRecord::Base.transaction(requires_new: true) do + def start_transaction(requires_new, &proc) + ActiveRecord::Base.transaction(requires_new: requires_new) do ActiveRecord::Base .connection .execute("SELECT pg_advisory_xact_lock(1845240511599988039) as l")