Module: PGMQ::Transaction

Included in:
Client
Defined in:
lib/pgmq/transaction.rb

Overview

Low-level transaction support for PGMQ operations

Provides atomic execution of PGMQ operations within PostgreSQL transactions. Transactions are a database primitive - this is a thin wrapper around PostgreSQL’s native transaction support.

This is analogous to rdkafka-ruby providing Kafka transaction support - it’s a protocol/database feature, not a framework abstraction.

Examples:

Atomic multi-queue operations

client.transaction do |txn|
  txn.produce("orders", '{"order_id":123}')
  txn.produce("notifications", '{"type":"order_created"}')
end

Automatic rollback on error

client.transaction do |txn|
  txn.produce("orders", '{"order_id":123}')
  raise "Error"  # Both operations rolled back
end

Defined Under Namespace

Classes: TransactionalClient

Instance Method Summary collapse

Instance Method Details

#transaction {|PGMQ::Client| ... } ⇒ Object

Executes PGMQ operations atomically within a database transaction

Obtains a connection from the pool, starts a transaction, and yields a client that uses that transaction for all operations.

Examples:

client.transaction do |txn|
  msg_id = txn.produce("queue", '{"data":"test"}')
  txn.delete("queue", msg_id)
end

Yields:

  • (PGMQ::Client)

    transactional client using the same connection

Returns:

  • (Object)

    result of the block

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/pgmq/transaction.rb', line 39

def transaction
  @connection.with_connection do |conn|
    conn.transaction do
      yield TransactionalClient.new(self, conn)
    end
  end
rescue PG::Error, StandardError => e
  raise PGMQ::Errors::ConnectionError, "Transaction failed: #{e.message}"
end