💡 Spring Transaction Propagation — What Really Happens When Transactions Call Each Other In my last post, I shared why a @Transactional method might not even start a transaction. This time, let’s go one level deeper — into propagation types, which define how transactions behave when one method calls another. 👇 1️⃣ REQUIRED (default) Joins the current transaction or creates a new one if none exists. → Most common — perfect for simple service-to-repository calls. 2️⃣ REQUIRES_NEW Always starts a new transaction, suspending the current one. → Great for independent operations like logging or audit trails. 3️⃣ SUPPORTS Runs within a transaction if one exists; otherwise, runs non-transactional. → Useful for read-only methods that can work with or without transactions. 4️⃣ MANDATORY Throws an exception if there’s no existing transaction. → Enforces strict transactional context. 5️⃣ NOT_SUPPORTED Suspends the active transaction and runs non-transactional. 6️⃣ NEVER Throws an exception if a transaction exists. 7️⃣ NESTED Creates a sub-transaction that can roll back independently (requires JDBC save-points). 🧠 Real-world example: In a payment system, you might use REQUIRES_NEW for saving audit logs — even if the main payment transaction fails, the log should still commit. Understanding propagation isn’t just academic — it defines how your data behaves in real-world failures. It’s the difference between safe rollbacks and invisible inconsistencies. Which propagation type do you use most often — and why? 👇 #java #springboot #transactionmanagement #hibernate #backenddevelopment #programming #softwareengineering #transactions
Understanding Spring Transaction Propagation Types
More Relevant Posts
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝗧𝗶𝗽 - 𝐌𝐢𝐬𝐬𝐢𝐧𝐠 𝐕𝐚𝐥𝐮𝐞𝐬 🔥 💎 𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻 𝘃𝘀 𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹 💡 𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻𝘀 are designed for truly exceptional, unexpected errors that occur outside normal program flow. When thrown, they propagate up the call stack until caught by an appropriate handler. ✔ Exceptions should never be used for routine control flow due to significant performance costs from stack unwinding and object creation. 🔥 𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹 provides an explicit alternative for representing absence of a value. It wraps a value that may or may not be present, making null-safe code more expressive and functional. ✔ Optional is ideal for modeling "value might be missing" scenarios and works seamlessly with streams and lambda expressions. ✅ 𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗘𝗮𝗰𝗵 ◾ 𝗘𝘅𝗰𝗲𝗽𝘁𝗶𝗼𝗻𝘀: Rare failures like I/O errors, database connection issues, or invalid business transactions. ◾ 𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹: Expected absence like cache misses, lookup results not found, or optional configuration values. ◾ 𝗛𝘆𝗯𝗿𝗶𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Use both strategically for optimal code clarity and performance. 🤔 Which approach do you prefer for handling missing values? #java #springboot #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
328. Odd Even Linked List — A Clean O(1) Space, O(n) Time Solution Here’s the challenge 👇 Problem: Given the head of a singly linked list, group all the odd-indexed nodes first (1st, 3rd, 5th, …), followed by all the even-indexed nodes (2nd, 4th, 6th, …). The relative order within both groups must remain the same. Example: Input: [1, 2, 3, 4, 5] Output: [1, 3, 5, 2, 4] 🧠 Approach: The trick is to rearrange the pointers, not the node values — ensuring O(1) extra space and O(n) time. We maintain two separate chains: One for odd-indexed nodes One for even-indexed nodes At the end, we simply connect the tail of the odd list to the head of the even list. 💻 Code: public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) return head; ListNode odd = head; ListNode even = head.next; ListNode evenHead = even; // Save starting point of even list while (even != null && even.next != null) { odd.next = even.next; // connect current odd to next odd odd = odd.next; // move odd pointer forward even.next = odd.next; // connect current even to next even even = even.next; // move even pointer forward } odd.next = evenHead; // attach even list at the end of odd list return head; } 🧩 Example Walkthrough: For input 1 → 2 → 3 → 4 → 5 Odd sequence builds as 1 → 3 → 5 Even sequence builds as 2 → 4 Finally merged → 1 → 3 → 5 → 2 → 4 ⚙️ Complexity: Time: O(n) — each node visited once Space: O(1) — no extra list or data structure used In-place reordering with clean pointer manipulation 💬 My Take: This problem beautifully tests your understanding of linked list pointer manipulation. Once you get the logic right, the solution feels almost magical — simple, elegant, and efficient. #LeetCode #Java #LinkedList #DSA #LinkedInLearning
To view or add a comment, sign in
-
-
💻 Day 53 of #100DaysOfCode – LeetCode #206: Reverse Linked List Today’s challenge was one of the most classic problems in data structures — reversing a linked list. This problem is a great way to strengthen your understanding of pointers and linked list manipulation. 🧩 Problem: Given the head of a singly linked list, reverse it and return the new head. ⚙ Approach: I used an iterative approach with three pointers: prev, curr, and next. Store the next node Reverse the current node’s pointer Move forward until the list is reversed 🧠 Key Learning: This problem really helped me visualize how pointers work in linked structures — a great exercise for understanding memory links and iterative logic. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Here’s my simple Java solution: class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } 🔥 Every day, one step closer to mastering data structures! #Day53 #LeetCode #Java #100DaysOfCode #CodingJourney #LinkedList #DSA #Programming #ReverseLinkedList
To view or add a comment, sign in
-
-
💻 Day 53 of #100DaysOfCode – LeetCode #206: Reverse Linked List Today’s challenge was one of the most classic problems in data structures — reversing a linked list. This problem is a great way to strengthen your understanding of pointers and linked list manipulation. 🧩 Problem: Given the head of a singly linked list, reverse it and return the new head. ⚙ Approach: I used an iterative approach with three pointers: prev, curr, and next. Store the next node Reverse the current node’s pointer Move forward until the list is reversed 🧠 Key Learning: This problem really helped me visualize how pointers work in linked structures — a great exercise for understanding memory links and iterative logic. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Here’s my simple Java solution: class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } 🔥 Every day, one step closer to mastering data structures! #Day53 #LeetCode #Java #100DaysOfCode #CodingJourney #LinkedList #DSA #Programming #ReverseLinkedList
To view or add a comment, sign in
-
-
Have you ever needed to delay task execution or rate-limit events — but didn’t want the complexity of schedulers or timers? ⏳ Turns out, Java already provides a hidden gem for that — the DelayQueue 💡 In my latest Medium article, I dive deep into how DelayQueue works under the hood, along with a hands-on walkthrough of implementing your own DelayedItem class. You’ll see how it can be used for: ✅ Throttling or rate-limiting events ✅ Retry queues for failed jobs ✅ Cache eviction with TTL ✅ Lightweight task scheduling It’s one of those underrated concurrency utilities that can simplify complex timing logic with just a few elegant lines of code. 🔗 Full article here 👉 https://lnkd.in/gzYjVchS 💬 I’d love to hear how you’ve used (or would use) DelayQueue in your systems!
To view or add a comment, sign in
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝗣𝗮𝗴𝗶𝗻𝗮𝘁𝗶𝗼𝗻 𝐓𝐢𝐩 🔥 💎 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗣𝗮𝗴𝗶𝗻𝗮𝘁𝗶𝗼𝗻? Pagination divides large datasets into smaller, manageable pages, making it essential for handling thousands of records efficiently. Instead of loading all data at once, you retrieve only what users need to see right now. This approach dramatically improves both server performance and user experience. 💡 𝗛𝗼𝘄 𝘁𝗼 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗶𝘁? Use the 𝗣𝗮𝗴𝗲𝗥𝗲𝗾𝘂𝗲𝘀𝘁 and 𝗣𝗮𝗴𝗲𝗮𝗯𝗹𝗲 in Spring Data JPA to fetch specific page records. PageRequest.of(page, size) calculates which records to retrieve based on the page number and page size. Spring Data JPA translates this into efficient SQL with OFFSET and FETCH clauses automatically. ✅ 𝗞𝗲𝘆 𝗕𝗲𝗻𝗲𝗳𝗶𝘁𝘀 ◾ Reduced database load and faster response times. ◾ Lower memory consumption on both server and client. ◾ Improved user navigation through large datasets. ◾ Better scalability as your data grows. 🤔 Did you try it before? #java #springboot #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
💡 Writing Efficient Code: Lessons from Real-World Optimization After working on multiple backend systems, I’ve learned that writing efficient code isn’t about shorter syntax — it’s about designing smarter data flow and understanding your system’s behavior under real-world conditions. One of the most impactful optimizations I’ve implemented was preloading master data and using in-memory maps instead of frequent database lookups. This single change reduced latency and DB load significantly — especially in high-traffic modules. Key takeaways from performance tuning in real projects 👇 ⚙️ Leverage Java Streams & Collectors — they help process collections in a functional, parallel, and more readable way. ⚙️ Use Optional to simplify null handling and improve code safety. ⚙️ Design with caching layers (Spring Cache, Redis) to minimize redundant queries. ⚙️ Replace nested loops with efficient stream-based transformations like Grouping by, filter, and map. ⚙️ Profile early — tools like JProfiler or VisualVM can uncover performance bottlenecks long before production. Over time, I’ve realized: Efficiency is not just about speed — it’s about clarity, maintainability, and thoughtful system design. Clean, performant code evolves from understanding data flow, load patterns, and scalability, not just syntax. 🚀 #Java #SpringBoot #PerformanceOptimization #SystemDesign #CleanCode #FunctionalProgramming #BackendDevelopment #EngineeringExcellence
To view or add a comment, sign in
-
🚀 𝗦𝗽𝗿𝗶𝗻𝗴 𝗜𝗼𝗖 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿 — 𝗧𝗵𝗲 𝗛𝗲𝗮𝗿𝘁 𝗼𝗳 𝘁𝗵𝗲 𝗦𝗽𝗿𝗶𝗻𝗴 𝗙𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗜𝗼𝗖 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿 𝗶𝗻 𝗦𝗽𝗿𝗶𝗻𝗴? The Inversion of Control (IoC) Container is the core of the Spring Framework. It’s responsible for: ✅ Creating and configuring objects (beans) ✅ Injecting dependencies automatically ✅ Managing their complete lifecycle In short — you don’t create or manage objects manually; Spring does it for you! The IoC container uses Dependency Injection (DI) to manage object dependencies automatically. 𝗦𝗽𝗿𝗶𝗻𝗴 𝗽𝗿𝗼𝘃𝗶𝗱𝗲𝘀 𝘁𝘄𝗼 𝘁𝘆𝗽𝗲𝘀 𝗼𝗳 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿𝘀: 🔸 𝗕𝗲𝗮𝗻𝗙𝗮𝗰𝘁𝗼𝗿𝘆 — The simplest container, responsible for managing beans defined in an XML file. It lazily loads beans (creates them only when needed). Used in older versions (now mostly deprecated). 🔸 𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝘁𝗲𝘅𝘁 — A more advanced container built on top of BeanFactory. It eagerly loads beans, provides internationalization, event propagation, annotation-based configuration, and AOP integration. Used in almost all modern Spring applications. 🧩 𝗖𝗼𝗺𝗺𝗼𝗻 𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝘁𝗲𝘅𝘁 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻𝘀 • 𝗖𝗹𝗮𝘀𝘀𝗣𝗮𝘁𝗵𝗫𝗺𝗹𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝘁𝗲𝘅𝘁 - Loads the XML configuration file from the classpath. • 𝗙𝗶𝗹𝗲𝗦𝘆𝘀𝘁𝗲𝗺𝗫𝗺𝗹𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝘁𝗲𝘅𝘁 - Loads configuration from an external file system path. • 𝗔𝗻𝗻𝗼𝘁𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝗳𝗶𝗴𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝗖𝗼𝗻𝘁𝗲𝘅𝘁 - Loads configuration from Java-based classes annotated with @𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻. Spring’s container reads metadata (XML or annotations), creates all required beans, and wires them — you never use new again! 💡 Think of it as a smart factory: you describe what you need, and Spring assembles and manages it for you. #SpringFramework #IoC #DependencyInjection #ApplicationContext #JavaDeveloper #SpringBoot #BackendDevelopment #BeanFactory #SpringEcosystem
To view or add a comment, sign in
-
-
🚀 Day 96 of #100DaysOfCode Today’s challenge was all about implementing an LRU (Least Recently Used) Cache in Java — a problem that truly tests your understanding of data structures and memory optimization. 💡 🧩 Problem Statement: Design a data structure that supports: get(key) → Returns value if key exists, else -1 put(key, value) → Inserts or updates value Both should work in O(1) time complexity. It should automatically remove the least recently used entry when the cache exceeds its capacity. ⚙️ What I Implemented: ✅ Used LinkedHashMap with access order enabled. ✅ Overrode removeEldestEntry() to maintain fixed capacity. ✅ Ensured get() and put() operations are constant time. ✅ Tested the cache with sample inputs to confirm LRU behavior. 📘 Sample Output: When I executed the program, I got: {3=30, 1=10, 4=40} This confirmed that the oldest (least recently used) entry was correctly removed when the cache was full. 🧠 Concepts Strengthened: Understanding LinkedHashMap internals Efficient memory management O(1) data retrieval and replacement logic Practical use of overriding and encapsulation 🔥 Each day of this challenge pushes me closer to mastering clean, efficient, and optimized Java code. This one made me appreciate how real-world caching systems like browsers and databases manage performance. 💬 Next Goal: To explore advanced multithreading problems and optimize concurrent data structures. ✨ Almost at the finish line — Day 96/100! Every line of code is another step toward growth. 💪 #100DaysOfCode #Java #DataStructures #CodingChallenge #ProblemSolving #LinkedHashMap #SoftwareEngineering #LearningJourney #DeveloperCommunity
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