A Spring Boot mistake that slows down your API. Using @Transactional on methods that don't need it. I've seen this pattern everywhere: @Transactional public User getUserById(Long id) { return userRepository.findById(id); } This is a read-only query. No updates. No writes. But @Transactional still opens a database connection, starts a transaction, and holds it until the method finishes. For simple reads, this adds unnecessary overhead. The fix is simple: @Transactional(readOnly = true) public User getUserById(Long id) { return userRepository.findById(id); } Or remove it entirely if you don't need transaction management. I optimized an API that had @Transactional on every method. Response time dropped by 25% just by cleaning this up. Small annotation. Big performance difference. What Spring Boot optimization gave you the best results? #SpringBoot #Java #Programming #BackendDevelopment #CodeReview
Optimize Spring Boot API Performance with @Transactional
More Relevant Posts
-
🤔 Ever wondered why Spring applications are so flexible? The answer lies in one powerful concept👇 🌱 Dependency Injection (DI) If you truly understand DI, Spring stops feeling complex. 🔗 Let’s talk about dependencies (real life first) Think about this: ▪️A Programmer needs a Computer ▪️A Student needs Transport ▪️A Car needs an Engine None of them work in isolation. In software, this relationship is called a dependency. ⚠️ Where most beginners go wrong When a class creates and controls its own dependencies, the system becomes tightly coupled. That leads to: ❌ Hard-to-change code ❌ Difficult testing ❌ Low flexibility ❌ Poor scalability ✅ Enter Dependency Injection Dependency Injection means: “Don’t create what you depend on — receive it.” Dependencies are: ▪️Provided from outside ▪️Swapped at runtime ▪️Managed efficiently 🎯 Result? Clean, flexible, and maintainable applications. 🔄 How Spring handles DI Spring supports: ▪️Constructor Injection (most recommended) ▪️Setter Injection (for optional dependencies) ▪️Field Injection (least preferred) This approach promotes loose coupling and better design. 💡 Final Thought Dependency Injection isn’t just a feature of Spring — it’s the foundation of modern Java applications. Master DI, and Spring becomes intuitive. #DependencyInjection #SpringFramework #Java #SpringBoot #SoftwareDesign #BackendDevelopment #JavaDevelopers #TechConcepts #ProgrammingBasics
To view or add a comment, sign in
-
-
💻 Crash vs 🧩 Exception 💻 Errors / Crashes OS/JVM/Hardware level serious issues 🔴 Serious system-level issues 🔴 Usually unrecoverable Examples: OutOfMemoryError StackOverflowError 🧩 Exceptions 🟡 Program logic or runtime issues 🟡 Can be handled Examples: Divide by zero Null pointer File not found 💥Exception An exception is an unexpected problem during program execution that breaks the normal flow of a program 🚧 📌 Key clarity: 👉 An exception is NOT the user’s mistake itself 👉 It is the error event created because of that mistake 🧠 Example: User enters 0 ➝ Code tries 15 / 0 ➝ 💥 ArithmeticException ➗ Division by Zero — Important Clarity ⚖️ ✅ Integer division: 15 / 0 💥 Throws ArithmeticException ❌ Program stops if not handled ⚠️ Floating-point division: 15.0 / 0 ♾️ Result = Infinity ✅ No exception thrown 👉 So yes, “it gives infinity” is correct, but only for float/double, not int ✔️ 🚨 What Happens When an Exception Is NOT Handled When an exception occurs: ✔️ Execution stops from that exact line ✔️ Java creates an exception object ✔️ JVM searches for a handler ✔️ No handler found ➝ 💀 program terminates ✔️ Stack trace printed 📄 📌 This behavior is called: 👉 Abnormal termination of the program ⏱️ Compile-Time vs Runtime Exceptions ✅ Checked Exceptions (Compile-time checked) 🔹 Compiler forces handling 🔹 Example: File handling (IOException) ✅ Unchecked Exceptions (Runtime) 🔹 Occur during execution 🔹 Compiler does NOT force handling Examples: ArithmeticException NullPointerException 🧠 Throwable Hierarchy Object ↓ Throwable ↓ Exception ↓ ↓ CTE RTE 🔖Frontlines EduTech (FLM) #Java #ExceptionHandling #ProgrammingConcepts #CoreJava #DeveloperMindset #TechAustralia #AustraliaJobs #SydneyTech #MelbourneTech #BrisbaneTech #AustraliaIT #LearningInPublic #SoftwareEngineering #CodingLife #TechExplained
To view or add a comment, sign in
-
-
One of the best things about Go is 𝗴𝗼 𝗯𝘂𝗶𝗹𝗱. You get a single, static binary that runs anywhere. No dependency hell, no "installing the JVM." But if it’s native machine code, why do we keep talking about the 𝗚𝗼 𝗥𝘂𝗻𝘁𝗶𝗺𝗲? Here is the secret: Go doesn't rely on an external runtime (like Java relies on the JVM installed on your OS). Instead, it embeds the runtime directly into your binary. Think of it like packing a mini-operating system inside your application. This invisible workhorse handles the heavy lifting so you don’t have to: • 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻: It automatically cleans up memory (unlike C/C++ where you manage every byte). • 𝗚𝗼𝗿𝗼𝘂𝘁𝗶𝗻𝗲 𝗦𝗰𝗵𝗲𝗱𝘂𝗹𝗶𝗻𝗴: The runtime acts as a traffic cop, multiplexing thousands of lightweight Goroutines onto a handful of OS threads. • 𝗦𝘁𝗮𝗰𝗸 𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁: It dynamically grows and shrinks stack sizes as needed. 𝗧𝗵𝗲 𝗧𝗿𝗮𝗱𝗲-𝗼𝗳𝗳? Your "Hello World" binary is larger than a C equivalent because it’s carrying this luggage. 𝗧𝗵𝗲 𝗪𝗶𝗻? You get the performance of compiled code with the developer experience of a dynamic language. The Go binary isn't just your code; it's your code plus the engine required to run it efficiently. Does your project require the control of manual memory management (Rust/C) or the ease of a runtime (Go/Java)? #Golang #SoftwareEngineering #BackendDevelopment #Programming #Tech
To view or add a comment, sign in
-
-
📌 Spring Boot Annotation Series – Part 2 ✅ @ComponentScan Ever faced a situation where Spring says ❌ NoSuchBeanDefinitionException even though your class is annotated correctly? That’s where @ComponentScan comes in 👇 🔹 WHY do we use @ComponentScan? Spring needs to know where your classes are. @ComponentScan tells Spring: 👉 “Scan these packages and create beans from them.” Without scanning, Spring doesn’t even see your classes. Spring needs to scan packages to find classes annotated with: @Component @Service @Repository @Controller 🔹 WHEN do we need @ComponentScan? When your service or config class is in a different package. When working on multi-module projects. When Spring can’t find a bean at runtime. 🔹 WHERE does Spring scan by default? By default, Spring scans: 👉 The package of the main application class 👉 And all its sub-packages If your class is outside this, Spring will miss it. @ComponentScan(basePackages = "com.example.app") #SpringBoot #Java #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
📌 Spring Boot Annotation Series – Part 2 ✅ @ComponentScan Ever faced a situation where Spring says ❌ NoSuchBeanDefinitionException even though your class is annotated correctly? That’s where @ComponentScan comes in 👇 🔹 WHY do we use @ComponentScan? Spring needs to know where your classes are. @ComponentScan tells Spring: 👉 “Scan these packages and create beans from them.” Without scanning, Spring doesn’t even see your classes. Spring needs to scan packages to find classes annotated with: @Component @Service @Repository @Controller 🔹 WHEN do we need @ComponentScan? When your service or config class is in a different package. When working on multi-module projects. When Spring can’t find a bean at runtime. 🔹 WHERE does Spring scan by default? By default, Spring scans: 👉 The package of the main application class 👉 And all its sub-packages If your class is outside this, Spring will miss it. @ComponentScan(basePackages = "com.example.app") #SpringBoot #Java #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
🔧 𝐃𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧 (𝐃𝐈) 𝐢𝐧 𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 Dependency Injection is one of the key principles that makes Spring Boot powerful and developer-friendly. Instead of a class creating its own dependencies using new, Spring’s IoC (Inversion of Control) container creates, manages, and injects objects automatically. ✨ Key Benefits ▸ Loose coupling – classes depend on abstractions, not concrete implementations ▸ Better testability – easy to mock dependencies during unit testing ▸ Cleaner & maintainable code – responsibilities are clearly separated 🔌 Types of Dependency Injection in Spring Boot ▸ Constructor Injection (recommended) – ensures immutability and required dependencies ▸ Setter Injection – useful for optional dependencies ▸ Field Injection – simple, but less test-friendly Using annotations such as @Component, @Service, @Repository, and @Autowired, Spring Boot automatically manages dependency wiring, allowing developers to focus on business logic instead of object lifecycles. 🚀 Mastering DI is a big step toward building scalable, enterprise-ready Spring Boot applications. #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #Tech
To view or add a comment, sign in
-
-
Most developers use Spring Beans daily. Very few understand what actually happens inside the container. Here’s the Spring Bean lifecycle — technically simplified: 🔁 Container Startup 1️⃣ Instantiation Bean object is created (via reflection). 2️⃣ Dependency Injection @Autowired / constructor injection resolves dependencies. 3️⃣ Aware Interfaces (optional) BeanNameAware, ApplicationContextAware, etc. 4️⃣ BeanPostProcessor (Before Init) postProcessBeforeInitialization() runs. 5️⃣ Initialization Phase @PostConstruct InitializingBean.afterPropertiesSet() Custom init-method 6️⃣ BeanPostProcessor (After Init) postProcessAfterInitialization() runs. 👉 This is where Spring may wrap your bean with a proxy. That’s how @Transactional, @Async, @Cacheable, and AOP work. The injected object is often a proxy — not your original class. 7️⃣ Bean Ready for Use 🔻 Shutdown Phase 8️⃣ Destruction @PreDestroy DisposableBean.destroy() (Default applies to singletons.) Why this matters: • @Transactional fails in self-invocation • Proxy-based behavior confuses debugging • Prototype inside Singleton behaves unexpectedly • Circular dependency issues during early creation Once you understand the lifecycle, Spring stops feeling like magic. It becomes predictable. #SpringBoot #SpringFramework #Java #BackendEngineering #AOP #Microservices
To view or add a comment, sign in
-
🚀 Day 14/100 - Dependency Injection (DI) - 1️⃣ One of the most important concepts in Spring Boot and modern backend development❗ ➡️ What is it? A design/programming technique where an object receives its dependencies from an external source (like the Spring container) instead of creating them itself. In simple words: 👉 Objects don’t create dependencies, they are provided. ➡️ Why to use? DI helps you write clean, scalable, and maintainable code: 🔹Promotes loose coupling between classes 🔹 Improves testability and maintainability 🔹 Encourages modular & reusable code 🔹 Follows SOLID principles - especially the "D: Dependency Inversion Principle" ➡️ Example: The attached image shows Dependency Injection via Constructor 👇 📌 Key Takeaway DI shifts responsibility from objects to the Spring container, making applications: - Easier to read - Easier to test - Easier to maintain Next post: https://lnkd.in/de9hpKXR Previous post: https://lnkd.in/dYC2XdM3 #100Days #SpringBoot #DependencyInjection #Java #BackendEngineering #SoftwareEngineering
To view or add a comment, sign in
-
-
Small but meaningful update in 𝐃𝐫𝐨𝐩𝐁𝐆 today While improving my 𝐉𝐖𝐓 𝐯𝐚𝐥𝐢𝐝𝐚𝐭𝐨𝐫, I integrated proper header validation instead of the earlier 𝐡𝐚𝐫𝐝𝐜𝐨𝐝𝐞𝐝 𝐭𝐫𝐮𝐞 check. Understanding this line took some time: 𝐇𝐭𝐭𝐩𝐇𝐞𝐚𝐝𝐞𝐫𝐬 𝐡𝐭𝐭𝐩𝐇𝐞𝐚𝐝𝐞𝐫𝐬 = 𝐇𝐭𝐭𝐩𝐇𝐞𝐚𝐝𝐞𝐫𝐬.𝐨𝐟(𝐡𝐞𝐚𝐝𝐞𝐫𝐬, (𝐤, 𝐯) -> 𝐭𝐫𝐮𝐞); Initially, I couldn’t grasp why a 𝐌𝐚𝐩<𝐒𝐭𝐫𝐢𝐧𝐠, 𝐋𝐢𝐬𝐭<𝐒𝐭𝐫𝐢𝐧𝐠>> 𝐡𝐞𝐚𝐝𝐞𝐫𝐬 was required not 𝐌𝐚𝐩<𝐒𝐭𝐫𝐢𝐧𝐠, 𝐒𝐭𝐫𝐢𝐧𝐠> 𝐡𝐞𝐚𝐝𝐞𝐫𝐬. After going 𝐭𝐡𝐫𝐨𝐮𝐠𝐡 𝐭𝐡𝐞 𝐝𝐨𝐜𝐮𝐦𝐞𝐧𝐭𝐚𝐭𝐢𝐨𝐧, it clicked — this structure is needed to correctly convert headers into HttpHeaders. The validation flow is now cleaner, safer, and closer to real production logic. Learning happens when you dig into why, not just what. 𝐀𝐧𝐨𝐭𝐡𝐞𝐫 𝐫𝐞𝐦𝐢𝐧𝐝𝐞𝐫 𝐭𝐡𝐚𝐭 𝐫𝐞𝐚𝐝𝐢𝐧𝐠 𝐝𝐨𝐜𝐬 𝐩𝐚𝐭𝐢𝐞𝐧𝐭𝐥𝐲 𝐚𝐥𝐰𝐚𝐲𝐬 𝐩𝐚𝐲𝐬 𝐨𝐟𝐟. Github Link - https://lnkd.in/gPKB5j5B #SpringBoot #Java #BackendDevelopment
To view or add a comment, sign in
-
-
Multi-Stage Docker Builds 🏗️📉 My Docker image went from 1 GB to 80MB. Here is the magic. 📉🐳 I used to think a Dockerfile was just about getting the app to run. But when I looked at my image size, I was shocked. It was huge. The Problem: I was including everything in the final image: the Source Code, the Compiler (JDK), and the build tools (Maven/Gradle). In production, I don't need javac or source code; I only need the compiled application. The Solution: Multi-Stage Builds. Today, I learned how to separate the "Construction Site" from the "Final House." 🧩 How it works (The 2-Stage Rocket): 1️⃣ Stage 1 (The Builder): Base Image: maven or jdk (Large). Task: Compile the code, download dependencies, build the .jar. Status: Discarded after use. 2️⃣ Stage 2 (The Runner): Base Image: jre-alpine (Tiny). Task: I COPY --from=builder only the final executable jar file. Status: This is the actual production image. 💡 The Result: Security: No source code or build tools left inside the container for hackers to exploit. Speed: Faster deployments because the image is 80% smaller. It’s like building a house with scaffolding, but removing the scaffolding before the tenants move in. #DevOps #Docker #Optimization #Java #Containerization #LearningInPublic #Engineering
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Overusing @Transactional is easy to miss but adds up fast. What optimizations have you made?