Sha256: 09bae671a0917e2f167ca596ac43791090529936d932fd786943198686de07e6

Contents?: true

Size: 970 Bytes

Versions: 14

Compression:

Stored size: 970 Bytes

Contents

# frozen_string_literal: true

module PgEventstore
  # @!visibility private
  class TransactionQueries
    attr_reader :connection
    private :connection

    # @param connection [PgEventstore::Connection]
    def initialize(connection)
      @connection = connection
    end

    # @return [void]
    def transaction
      connection.with do |conn|
        # We are inside a transaction already - no need to start another one
        if [PG::PQTRANS_ACTIVE, PG::PQTRANS_INTRANS].include?(conn.transaction_status)
          next yield
        end

        pg_transaction(conn) do
          yield
        end
      end
    end

    private

    # @param pg_connection [PG::Connection]
    # @return [void]
    def pg_transaction(pg_connection)
      pg_connection.transaction do
        pg_connection.exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
        yield
      end
    rescue PG::TRSerializationFailure, PG::TRDeadlockDetected
      retry
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
pg_eventstore-0.9.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.8.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.7.2 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.7.1 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.7.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.6.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.5.3 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.5.2 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.5.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.4.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.3.0 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.2.6 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.2.5 lib/pg_eventstore/queries/transaction_queries.rb
pg_eventstore-0.2.4 lib/pg_eventstore/queries/transaction_queries.rb