Library Management API
A robust, secure institutional library backend built using **Spring Boot, Spring Data JPA, and PostgreSQL**. Designed with strict transactional constraints to enforce ACID properties across high-concurrency borrow, return, and penalty cycles.
Managing inventory consistency under peak university traffic demands an enterprise architecture. This API exposes standard REST interfaces with comprehensive request validation, dynamic exception handlers, query optimizations, and scheduled database cleanups.
Core Architecture & Capabilities
Engineered for high performance, ACID compliance, and secure transactional records.
🌱 Spring Boot Core
Stateless MVC REST controllers handle request mappings, map schemas using dynamic DTOS, and leverage Spring context IoC containers for seamless dependency injection.
💾 JPA & Hibernate ORM
Utilizes Spring Data JPA repository interfaces for database access, with mapped entity objects, sub-entity mappings, and customized query methods.
🔒 Transaction Integrity
Employs declarative transaction control (@Transactional) to enforce relational rollbacks, guaranteeing data integrity if any single part of the borrow process fails.
⏰ Fine Calculation Service
Dynamic scheduled batches check due status timelines and automatically append daily late penalties using configurable institution tariff metrics.
🛡️ Data Validation
Uses Java Bean Validation (JSR 380) annotations (@NotNull, @Size, @Min) on REST request bodies to validate inputs before hitting core logic.
🐘 PostgreSQL Database
Normalized entity relationships (3NF) housing indices, constraints, and audit logging mappings to maintain transaction logs with microsecond precision.
Relational Database Entity & Schema Structures
📖 Book & Copy Hierarchy
Decouples metadata (Title, Author, ISBN) from individual physical book items (Inventory barcodes, states: Available, Borrowed, Maintenance). Enables scaling inventory logs safely.
⏳ Borrowing Lifecycle States
Utilizes JPA entity listeners to automate timestamp tracking on borrows and returns, calculating elapsed loan intervals natively.
📋 API Exception Handler Interceptors
Uses @ControllerAdvice to map internal persistence validation checks and custom exceptions (e.g. BookNotAvailableException) to readable REST JSON messages.
Key Engineering Breakthroughs
Database Deadlocks under High Borrow Concurrency
The Problem: When hundreds of students attempt to borrow the same high-demand textbooks concurrently, parallel thread transactions attempt to update the same copy records, causing database deadlocks.
The Solution: Implemented **Pessimistic Write Locking** (@Lock(LockModeType.PESSIMISTIC_WRITE)) on transactional query methods. This forces database threads to block and execute sequentially, preserving copy counts and preventing database state exceptions.
The Relational N+1 Query Fetching Defect
The Problem: Querying user details with active loans caused Spring Data JPA to generate an initial query followed by N subsequent database select statements to fetch nested loan relationships, generating severe network latency.
The Solution: Restructured entity fetch modes. Used custom Hibernate **Entity Graphs** (@EntityGraph) and explicit JPQL **JOIN FETCH** statements to force Hibernate to compile nested associations inside a single combined SQL query.
Interested in the code?
Explore the clean architecture database bindings, rest controller layers, and schema definitions on GitHub.