Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 24

    Introduction to Databases

    Orders cannot stay in a ConcurrentHashMap forever.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Orders cannot stay in a ConcurrentHashMap forever. A database gives durability, query power, and multi-instance safety. This course focuses on relational databases — PostgreSQL in production-shaped labs, with MySQL also covered — accessed through Spring Data JPA in the next lessons.

    At the HTTP edge nothing changes: controllers still return OrderView. Persistence appears behind OrderRepository inside the service. That boundary is why Fundamentals spent so long on layers.

    You will meet schemas, migrations (Flyway/Liquibase), transactions, and N+1 queries later. Here you learn what problem the database solves and which pieces Boot will auto-configure when a driver and JPA starter appear on the classpath.

    Choose PostgreSQL for new TechLearningPRO services unless an existing estate forces MySQL.

    Purpose of this lesson

    Explain why Orders needs a relational database and how it fits the Controller → Service → Repository design before JPA details begin.

    Understanding the topic

    Databases are durability and consistency tools.

    • Durability: process restarts must not erase orders.
    • Concurrency: multiple app instances need one source of truth.
    • Relational model: tables, keys, constraints for money and status.
    • Boot integration: datasource URL + driver + JPA starter → auto-config.
    • Migrations: version schema in Git — do not rely on open-ended ddl-auto in prod.

    Step-by-step explanation

    1. List which Orders fields must survive restart.
    2. Sketch a table: id, sku, qty, amount_cents, status, created_at.
    3. Note the repository interface will replace the map in the service.
    4. Preview next lessons: MySQL/Postgres drivers, then Spring Data JPA.

    Informative example

    Conceptual schema for Orders (SQL).

    sql
    CREATE TABLE orders (
    id BIGSERIAL PRIMARY KEY,
    sku VARCHAR(64) NOT NULL,
    qty INT NOT NULL CHECK (qty > 0),
    amount_cents INT NOT NULL CHECK (amount_cents >= 0),
    status VARCHAR(32) NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
    );

    Real-world use

    Checkout across two TechLearningPRO nodes behind a load balancer cannot keep separate in-memory maps. PostgreSQL becomes the shared ledger for order ids and amounts — Redis may cache later, but the database remains authoritative.

    Security implications

    The database is the crown jewel.

    • Least-privilege DB users for the app.
    • Secrets via env/secret mounts — not images.
    • Encrypt connections (TLS) to managed databases.

    Best practices

    • Prefer PostgreSQL for new services on this track.
    • Keep money as integer cents in columns too.
    • Plan migrations from day one of persistence.

    Common mistakes

    • Using spring.jpa.hibernate.ddl-auto=update as a production migration strategy.
    • Putting datasource passwords in Git-backed YAML.
    • Letting controllers talk to JdbcTemplate directly 'just this once'.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhy move Orders off an in-memory map?+

    Answer

    Durability across restarts and correctness with multiple application instances sharing state.
    2BeginnerQuestionWhere does the database sit in the Boot layering?+

    Answer

    Behind repositories used by services — not behind controllers.
    3IntermediateQuestionWhy avoid ddl-auto update in production?+

    Answer

    Schema changes need reviewable migrations; auto-update can fail dangerously or drift across environments.

    Hands-on exercise

    Lab. Write the orders table DDL above into your notes. Mark which columns map to OrderView fields and which stay internal. Do not enable JPA yet.

    Summary

    Databases make Orders real across restarts and instances. The Data module starts next with concrete drivers and Spring Data JPA.

    Ready to mark this lesson complete?Track your journey across the entire course.