Code Explainers

Code explainers tagged #transactions

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
java
@Service
public class OrderCheckoutService {
 
    private final OrderRepository orderRepository;

How @Transactional guards a Spring checkout

dependency-injection transactions atomicity
Intermediate 6 steps
java
package com.example.maintenance;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

How a scheduled cleanup job runs in Spring

scheduling cron transactions
Intermediate 6 steps
java
@Service
public class ProductCatalogService {
 
    private final ProductRepository productRepository;

How Spring cache annotations keep data fresh

caching cache-invalidation dependency-injection
Intermediate 7 steps
java
@Service
public class OrderService {
 
    private final OrderRepository orderRepository;

Decoupling side effects with Spring events

event-driven transactions decoupling
Intermediate 8 steps
java
@Entity
@Table(name = "accounts")
public class Account {
 

Optimistic locking with @Version in Spring

optimistic-locking jpa concurrency
Advanced 7 steps
ruby
class PaymentsController < ApplicationController
  before_action :require_idempotency_key
 
  def create

Idempotent payment creation in Rails

idempotency transactions race-conditions
Advanced 10 steps
java
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
 
    @QueryHints(@QueryHint(name = HINT_FETCH_SIZE, value = "1000"))

Streaming large result sets in Spring Data JPA

streaming jpa memory-management
Advanced 6 steps
ruby
class CheckoutService
  Result = Struct.new(:success?, :order, :error, keyword_init: true)
 
  def self.call(...)

How a Rails checkout service object works

service object transactions result object
Intermediate 9 steps
ruby
class RegistrationForm
  include ActiveModel::Model
  include ActiveModel::Attributes
 

How a Rails form object spans two models

form object validations transactions
Intermediate 7 steps
ruby
module FileResource
  # Opens a file, yields it to the caller, and guarantees the
  # handle is closed even if the block raises an exception.
  def self.open(path, mode = "r")

Guaranteeing cleanup with begin/ensure in Ruby

resource-management exception-safety blocks
Intermediate 8 steps