Code Explainers

Code explainers tagged #transactions

php
<?php
 
namespace App\Services\Checkout;
 

Validating coupons with Laravel's Pipeline

pipeline chain of responsibility transactions
Intermediate 7 steps
java
@Component
@RequiredArgsConstructor
@Slf4j
public class FailedJobReprocessor {

Retrying failed jobs on a Spring schedule

scheduling retry exponential-backoff
Intermediate 10 steps
python
from django.db import transaction
from django.db.models import F
from django.http import JsonResponse
from django.views.decorators.http import require_POST

Safely reordering tasks with a Django action

transactions row-locking validation
Advanced 8 steps
ruby
class Admin::AccountsController < Admin::BaseController
  before_action :set_account, only: :destroy
 
  def destroy

A guarded destroy action in Rails

controllers callbacks transactions
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Idempotent role assignment in Laravel

many-to-many pivot-data idempotency
Intermediate 8 steps
go
package middleware
 
import (
	"net/http"

Wrapping Gin requests in a DB transaction

middleware transactions error-handling
Intermediate 8 steps
javascript
const { Pool } = require('pg');
 
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,

Per-request Postgres connections in Express

connection-pooling middleware transactions
Intermediate 8 steps
ruby
class Document < ApplicationRecord
  class StaleObjectError < StandardError
    def initialize(id)
      super("Document ##{id} was modified by another process")

Optimistic locking with retries in Rails

optimistic-locking concurrency transactions
Advanced 8 steps
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