MODULE 7 - Databases and Transactions
Welcome to Module 7! ποΈβ
You now know how to use Spring Data JPA to create repositories and execute queries. But do you know how Spring actually manages database connections? And more importantly, do you understand transactions?
This module reveals the essential mechanisms to manage your data in a reliable and professional manner.
π Learning Objectivesβ
By the end of this module, you will be able to:
- β Configure Spring Boot datasources (HikariCP)
- β Understand and use @Transactional correctly
- β Manage propagation and isolation levels
- β Version your DB schema with Flyway
- β Compare Flyway and Liquibase
- β Connect your application to different DBs (H2, PostgreSQL, MySQL)
π Prerequisitesβ
Before starting this module, you should have:
- β Completed Modules 1 to 6
- β Created JPA entities with relationships
- β Used Spring Data JPA repositories
- β Understood the 3-layer architecture (Controller, Service, Repository)
If these prerequisites are not met, go back to the previous modules!
πΊοΈ Module Outlineβ
Theoretical Part (4 chapters)β
Chapter 1: Datasource Configurationβ
Duration: 45 minutes
Content:
- What is a Datasource?
- Configuration in application.properties/yml
- HikariCP: the default connection pool
- Advanced configuration (min/max connections, timeout)
- Multi-datasource configuration
Why important? Understand how Spring Boot manages connections
Chapter 2: Transaction Management (@Transactional)β
Duration: 1 hour
Content:
- What is a transaction? (ACID)
- @Transactional explained in detail
- Propagation (REQUIRED, REQUIRES_NEW, etc.)
- Isolation levels
- Rollback and exception handling
- Common pitfalls to avoid
Why important? Ensure data integrity
Chapter 3: Schema Migration with Flywayβ
Duration: 45 minutes
Content:
- Why version the DB schema?
- Flyway installation and configuration
- Naming convention (V1__, V2__, etc.)
- SQL vs Java migration
- Rollback and repair
- Best practices
Why important? Deploy DB changes in production
Chapter 4: Migration with Liquibaseβ
Duration: 30 minutes
Content:
- Flyway vs Liquibase: comparison
- XML, YAML, JSON format
- Changelog and changesets
- Automatic rollback
- When to choose Liquibase?
Why important? Know the alternatives
Practical Part (3 Labs)β
Lab 7.1: Datasource and Pool Configurationβ
Duration: 1h
Objective: Configure and optimize DB connections
You will:
- Configure HikariCP for H2 and PostgreSQL
- Monitor the connection pool
- Test connection limits
Lab 7.2: Transactions and Rollbackβ
Duration: 1h30
Objective: Master @Transactional
You will:
- Create transactional operations
- Simulate errors and observe rollbacks
- Test propagation (REQUIRED vs REQUIRES_NEW)
- Manage read-only transactions
Lab 7.3: Migration with Flywayβ
Duration: 1h30
Objective: Version the DB schema
You will:
- Configure Flyway
- Create migration scripts
- Simulate a schema evolution
- Manage initial data
π Total Durationβ
| Type | Duration |
|---|---|
| Theory | 3h (4 chapters) |
| Practice | 4h (3 Labs) |
| Total | 7h (~2-3 sessions) |
π― What You Will Understandβ
Before this Moduleβ
// You wrote this without really understanding:
@Service
public class TransferService {
@Transactional // β Why? What is it exactly?
public void transfer(Long from, Long to, BigDecimal amount) {
accountRepo.debit(from, amount);
accountRepo.credit(to, amount);
}
}
Unanswered questions:
- What happens if
credit()fails afterdebit()? - Why @Transactional and not just try/catch?
- How does Spring manage DB connections?
- How to deploy schema changes in production?
After this Moduleβ
@Service
public class TransferService {
@Transactional(
propagation = Propagation.REQUIRED, // β Reuse or create a transaction
isolation = Isolation.READ_COMMITTED, // β Isolation level
rollbackFor = Exception.class // β Rollback on any exception
)
public void transfer(Long from, Long to, BigDecimal amount) {
// If credit() fails, debit() is AUTOMATICALLY rolled back!
accountRepo.debit(from, amount);
accountRepo.credit(to, amount);
}
}
You will know:
- β How Spring manages connections (HikariCP pool)
- β When and how to use @Transactional
- β Avoid common pitfalls (proxy, self-invocation)
- β Migrate schema with Flyway risk-free
- β Choose between Flyway and Liquibase
π Why This Module Is Crucialβ
1. Data Integrityβ
Catastrophic scenario (without transaction):
1. Debit account A: -100β¬ β
2. Credit account B: ERROR! β
3. Result: 100β¬ "disappeared" π
Transactions ensure: ALL or NOTHING.
2. Production Performanceβ
Without connection pool:
- Each request = new connection = SLOW
- 1000 users = 1000 connections = OVERLOADED DB
With HikariCP (pool):
- Reused connections = FAST
- 1000 users = 10 connections = HAPPY DB π
3. Stress-Free Deploymentβ
Without Flyway:
- "Did you run the SQL script?" π€
- "Which one? V3 or V4?" π°
- "Oops, I forgot a column" π
With Flyway:
- Automatic migrations on startup β
- Tracked versioning β
- Peaceful deployment β
πΊοΈ Training Progressionβ
Module 7 = Master the foundations of persistence!
π How to Approach This Moduleβ
Theory BEFORE Practiceβ
-
Read the 4 theoretical chapters in order
- Chapter 1: Datasources (foundations)
- Chapter 2: Transactions (the most important!)
- Chapter 3: Flyway (main migration)
- Chapter 4: Liquibase (alternative)
-
Do the Labs in order
- Lab 7.1: Configuration
- Lab 7.2: Transactions
- Lab 7.3: Flyway
Recommended Timeβ
Intensive Option (1 day):
- Morning: Chapters 1 and 2 + Lab 7.1
- Afternoon: Lab 7.2 + Chapters 3 and 4
- Late afternoon: Lab 7.3
Progressive Option (2 sessions):
- Session 1: Chapters 1-2 + Lab 7.1 and Lab 7.2
- Session 2: Chapters 3-4 + Lab 7.3
β Self-Assessmentβ
Before starting, answer:
- Do you know what a datasource is? β Yes β No
- Are you familiar with ACID properties? β Yes β No
- Do you know what @Transactional does? β Yes β No
- Have you ever used Flyway or Liquibase? β Yes β No
If you answered "No" to any question β This module is for you!
π― Next Stepβ
Ready to master connections and transactions?
β‘οΈ Start with Chapter 1: Datasource Configuration