Category: Spring Boot
-
How to Change the Embedded Server in Spring Boot
1. Exclude Tomcat By default, spring-boot-starter-web brings in Tomcat. To use a different server, you must exclude Tomcat from it. In Maven, this looks like: 2. Add the Alternative Server Dependency Choose only one of the following: a. Use Jetty b. Use Undertow c. Use Netty (only for reactive applications) If you’re using spring-boot-starter-webflux, Netty…
-
Spring Transaction Management
Transactions are a critical part of applications interacting with databases or other persistent resources. They ensure consistency, integrity, and reliability of data when multiple operations must succeed or fail as a unit. Spring provides advanced tools for transaction management, both declarative and programmatic, allowing developers to focus on business logic while ensuring transactional safety. 1.…
-
How do you handle database connection pooling in Spring Boot?
In Spring Boot, database connection pooling is handled automatically using a connection pool library, which improves application performance by reusing database connections instead of creating a new connection for every request. Here’s a detailed explanation of how it works and how to configure it: 1. What is Connection Pooling 2. Spring Boot Default Connection Pool…
-
What’s the impact of @Transactional(readOnly = true) in a method?
In Spring, @Transactional(readOnly = true) is used to optimize transactions that only perform read operations. It has several effects on the transaction behavior, the persistence context, and the database interaction. Here’s a detailed explanation: 1. Primary Purpose 2. Impact on Hibernate / JPA When using Hibernate with Spring: 3. Impact on the Database 4. Behavior…
-
Optimistic locking using Spring Boot and JPA Hibernate
Optimistic locking is a concurrency control strategy used in applications (including Spring Boot + JPA/Hibernate) to prevent lost updates when multiple transactions try to update the same record without using database-level locks. Unlike pessimistic locking, it assumes conflicts are rare and checks for conflicts only at the time of commit. Here’s a detailed explanation: 1.…
-
Create a custom starter in Spring Boot
Creating a custom Spring Boot starter is a powerful way to package reusable configuration, dependencies, and auto-configuration logic into a single module. This is especially useful when you want to apply common setups across multiple Spring Boot projects in your organization or team. What Is a Spring Boot Starter? A starter is a special kind…
-
Set different logging levels for specific packages in Spring Boot
To set different logging levels for specific packages in a Spring Boot application, you can configure them using the application.properties or application.yml file. This allows you to control how much logging detail you get per package (e.g., your own code, Spring internals, Hibernate, etc.), which is useful for debugging specific modules without cluttering the logs.…
-
Expose Custom Metrics
To expose custom metrics in Spring Boot, you can use Micrometer, which is the metrics collection facade integrated into Spring Boot Actuator. Spring Boot Actuator provides production-ready endpoints like /actuator/metrics, and you can extend it with custom metrics to track application-specific behavior. Step-by-Step Guide to Expose Custom Metrics 1. Add Required Dependencies If you’re using…
-
Application versioning in a Spring Boot API
Implementing application versioning in a Spring Boot REST API is crucial for supporting backward compatibility when your API evolves. There are several strategies to version a Spring Boot API, and the method you choose depends on your design goals and client needs. Common Strategies for API Versioning 1. URI Path Versioning (Recommended) Most widely used…
-
Run specific logic right after Spring Boot application startup
To run specific logic right after your Spring Boot application starts, you can use one of the following standard interfaces provided by Spring: 1. Using CommandLineRunner Example: 2. Using ApplicationRunner Example: 3. Using @EventListener(ApplicationReadyEvent.class) Example: When to Use Which? Use Case Recommended Interface Need command-line arguments CommandLineRunner or ApplicationRunner Need to execute logic after Spring…
