Code Explainers

Code explainers tagged #transactions

php
<?php
 
namespace App\Console\Commands;
 

Releasing stale document locks in Laravel

artisan-command transactions row-locking
Intermediate 6 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, catchError, concatMap, finalize } from 'rxjs';
import { DataSource, QueryRunner } from 'typeorm';
 

Wrapping requests in a transaction with NestJS

interceptors transactions rxjs
Advanced 7 steps
python
from decimal import Decimal, ROUND_HALF_UP
 
from django.db import transaction
from django.db.models import F

Batch price updates safely in Django

decimal-precision batch-processing transactions
Advanced 8 steps
python
from django.db import transaction
from django.forms import inlineformset_factory
from django.shortcuts import get_object_or_404, redirect, render
 

Editing an order with inline formsets in Django

formsets transactions crud
Intermediate 9 steps
python
import os
import shutil
import tempfile
from contextlib import contextmanager

Transactional file operations in Python

transactions rollback closures
Intermediate 8 steps
ruby
class TasksController < ApplicationController
  before_action :set_project
 
  def reorder

Bulk task reordering with upsert_all in Rails

bulk-update upsert authorization
Intermediate 8 steps
java
public List<OrderSummary> streamRecentOrders(LocalDateTime since, Consumer<OrderSummary> handler) {
    String sql = """
        SELECT id, customer_id, total_cents, status, created_at
        FROM orders

Streaming large JDBC result sets safely

jdbc streaming resource-management
Intermediate 7 steps
javascript
export function diffIds(current, desired) {
  const currentSet = new Set(current);
  const desiredSet = new Set(desired);
 

Diffing sets to sync group membership

set-operations diffing transactions
Intermediate 8 steps
php
public function transaction(callable $callback, int $maxAttempts = 3): mixed
{
    $attempt = 0;
 

Retrying database deadlocks in PHP

retry transactions deadlock
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Optimistic locking on invoices in Laravel

optimistic-locking transactions concurrency
Advanced 6 steps
python
from flask import Flask, g, jsonify, request, abort
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
 

Per-request SQLAlchemy sessions in Flask

database sessions request lifecycle connection management
Intermediate 7 steps
ruby
class Account < ApplicationRecord
  has_many :projects, dependent: :destroy_async
  has_many :memberships, dependent: :destroy_async
  has_many :invoices, dependent: :restrict_with_error

Safely closing an Account in Rails

associations callbacks transactions
Intermediate 9 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
python
from decimal import Decimal
 
from django.db import transaction
from django.core.exceptions import ValidationError

Placing an order atomically in Django

transactions row-locking concurrency
Advanced 9 steps
php
<?php
 
namespace App\Services;
 

Placing an order atomically in Laravel

transactions pessimistic-locking concurrency
Advanced 8 steps
java
public class OrderRepository {
 
    private static final int BATCH_SIZE = 500;
    private static final String INSERT_SQL =

Batched JDBC inserts with transaction safety

jdbc batching transactions
Intermediate 8 steps
ruby
class CartMergeService
  def initialize(guest_cart:, user_cart:)
    @guest_cart = guest_cart
    @user_cart = user_cart

Merging a guest cart into a user cart in Rails

service object transactions idempotency
Intermediate 7 steps
php
<?php
 
final class OrderRepository
{

Atomic order placement with PDO transactions

transactions prepared-statements atomicity
Intermediate 8 steps