transactions

go
package dbutil

import (
  "context"

  "github.com/jackc/pgconn"

Retry Postgres serialization failures with bounded attempts

go postgres transactions
by Leah Thompson 1 tab
ruby
class SignupForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :account_name, :string
  attribute :email, :string

Shallow Controller, Deep Params: Form Object Pattern

rails activemodel form-object
by codesnips 3 tabs
go
package store

import (
  "context"

  "github.com/jackc/pgx/v5"

Postgres transaction pattern with pgx: defer rollback, commit explicitly

go postgres pgx
by Leah Thompson 1 tab
go
package store

import (
  "context"
  "database/sql"
)

sqlc transaction wrapper that keeps call sites clean

go sql postgres
by Leah Thompson 1 tab
sql
CREATE TABLE outbox (
    id             BIGSERIAL PRIMARY KEY,
    aggregate_type TEXT        NOT NULL,
    aggregate_id   TEXT        NOT NULL,
    event_type     TEXT        NOT NULL,
    payload        JSONB       NOT NULL,

Transactional outbox in Node (DB write + event)

node postgres reliability
by codesnips 3 tabs
ruby
class CreateInventoryReservations < ActiveRecord::Migration[7.1]
  def change
    create_table :inventory_items do |t|
      t.string  :sku, null: false
      t.integer :quantity_on_hand,  null: false, default: 0
      t.integer :quantity_reserved, null: false, default: 0

Transactional “Reserve Inventory” with SELECT … FOR UPDATE

rails activerecord transactions
by codesnips 3 tabs
sql
-- Row-level locks with SELECT FOR UPDATE
BEGIN;

SELECT * FROM inventory
WHERE product_id = 123
FOR UPDATE;  -- Locks selected rows

Advanced database locking and concurrency control

postgresql locks concurrency
by Maria Garcia 2 tabs
ruby
module RetryableTransaction
  module_function

  RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze

  def run(max_retries: 3, base_delay: 0.05, &block)

Deadlock-Aware Retry Wrapper

rails postgres reliability
by codesnips 3 tabs
python
from django.db import transaction
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Order, OrderItem, Product

Django transaction handling with atomic decorator

django python database
by Priya Sharma 1 tab
sql
-- Basic transaction
BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

Database transactions and ACID properties

database transactions acid
by Maria Garcia 2 tabs
ruby
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
  def change
    create_table :email_deliveries do |t|
      t.string :dedupe_key, null: false
      t.string :mailer, null: false
      t.string :action, null: false

Transactional Email “Send Once” with Delivered Marker

rails reliability activerecord
by codesnips 4 tabs
ruby
class Document < ApplicationRecord
  belongs_to :account

  validates :source_url, presence: true

  before_save :normalize_checksum

Prevent Long Transactions with after_save_commit for Heavy Work

rails activerecord background-jobs
by codesnips 3 tabs