{"id":3456,"date":"2026-07-08T04:17:24","date_gmt":"2026-07-08T04:17:24","guid":{"rendered":"https:\/\/tucumandevelopers.com\/index.php\/2026\/07\/08\/2-transactional-traps-that-catch-even-senior-java-developers-in-interviews\/"},"modified":"2026-07-08T04:17:24","modified_gmt":"2026-07-08T04:17:24","slug":"2-transactional-traps-that-catch-even-senior-java-developers-in-interviews","status":"publish","type":"post","link":"https:\/\/tucumandevelopers.com\/index.php\/2026\/07\/08\/2-transactional-traps-that-catch-even-senior-java-developers-in-interviews\/","title":{"rendered":"2 @Transactional Traps That Catch Even Senior Java Developers in interviews"},"content":{"rendered":"<div>\n<div><\/div>\n<div data-article-id=\"4084292\" id=\"article-body\">\n<hr>\n<p>I have 13 years of Java experience. I have also interviewed hundreds of developers at MNCs.<\/p>\n<p>Two questions about <code>@Transactional<\/code> that catch even senior developers. Try answering before you scroll down.<\/p>\n<hr>\n<h3> <a name=\"q1-if-a-nontransactional-method-calls-a-raw-transactional-endraw-method-in-the-same-class-does-the-transaction-apply\" href=\"#q1-if-a-nontransactional-method-calls-a-raw-transactional-endraw-method-in-the-same-class-does-the-transaction-apply\"> <\/a> Q1: If a non-transactional method calls a <code>@Transactional<\/code> method in the same class, does the transaction apply? <\/h3>\n<div>\n<pre><code><span>@Service<\/span> <span>public<\/span> <span>class<\/span> <span>OrderService<\/span> <span>{<\/span> <span>public<\/span> <span>void<\/span> <span>placeOrder<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>{<\/span> <span>this<\/span><span>.<\/span><span>saveOrder<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>}<\/span> <span>@Transactional<\/span> <span>public<\/span> <span>void<\/span> <span>saveOrder<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>{<\/span> <span>orderRepository<\/span><span>.<\/span><span>save<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>inventoryRepository<\/span><span>.<\/span><span>update<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>}<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p><strong>Answer: No.<\/strong><\/p>\n<p><strong>Reason, in plain terms:<\/strong> when you add <code>@Transactional<\/code>, Spring does not add the transaction logic inside your class. Instead, it creates a second object that sits in front of your class \u2014 think of it as a wrapper. Other classes that call <code>saveOrder()<\/code> (through <code>@Autowired<\/code>) actually call this wrapper first. The wrapper starts the transaction, then calls your real method, then commits or rolls back.<\/p>\n<p>This wrapper only exists <em>outside<\/em> your class. So when your own code calls <code>this.saveOrder(order)<\/code>, it skips the wrapper completely and goes straight to the real method. No wrapper means no transaction logic. No rollback if <code>inventoryRepository.update()<\/code> fails.<\/p>\n<p>This bug never shows up in tests. It only shows up in production, when a failure leaves your data half-updated.<\/p>\n<p><strong>Fix:<\/strong> call the method from a different class, or inject the wrapper of your own class: <\/p>\n<div>\n<pre><code><span>@Service<\/span> <span>public<\/span> <span>class<\/span> <span>OrderService<\/span> <span>{<\/span> <span>@Autowired<\/span> <span>private<\/span> <span>OrderService<\/span> <span>self<\/span><span>;<\/span> <span>\/\/ this is the wrapper, not the raw object<\/span> <span>public<\/span> <span>void<\/span> <span>placeOrder<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>{<\/span> <span>self<\/span><span>.<\/span><span>saveOrder<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>\/\/ now it goes through the wrapper<\/span> <span>}<\/span> <span>@Transactional<\/span> <span>public<\/span> <span>void<\/span> <span>saveOrder<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>{<\/span> <span>orderRepository<\/span><span>.<\/span><span>save<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>inventoryRepository<\/span><span>.<\/span><span>update<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>}<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<hr>\n<h3> <a name=\"q2-does-raw-transactional-endraw-roll-back-on-every-exception\" href=\"#q2-does-raw-transactional-endraw-roll-back-on-every-exception\"> <\/a> Q2: Does <code>@Transactional<\/code> roll back on every exception? <\/h3>\n<div>\n<pre><code><span>@Transactional<\/span> <span>public<\/span> <span>void<\/span> <span>processRefund<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>throws<\/span> <span>RefundException<\/span> <span>{<\/span> <span>paymentRepository<\/span><span>.<\/span><span>markRefunded<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>externalRefundService<\/span><span>.<\/span><span>notify<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>\/\/ throws RefundException<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p><strong>Answer: No.<\/strong><\/p>\n<p><strong>Reason:<\/strong> by default, <code>@Transactional<\/code> only rolls back on unchecked exceptions (<code>RuntimeException<\/code> and its subclasses). <code>RefundException<\/code> above is a checked exception. So if it is thrown, Spring lets the transaction <strong>commit anyway<\/strong> \u2014 including the <code>markRefunded<\/code> write that happened right before the failure.<\/p>\n<p><strong>Fix:<\/strong> tell Spring to roll back on any exception, not just unchecked ones: <\/p>\n<div>\n<pre><code><span>@Transactional<\/span><span>(<\/span><span>rollbackFor<\/span> <span>=<\/span> <span>Exception<\/span><span>.<\/span><span>class<\/span><span>)<\/span> <span>public<\/span> <span>void<\/span> <span>processRefund<\/span><span>(<\/span><span>Order<\/span> <span>order<\/span><span>)<\/span> <span>throws<\/span> <span>RefundException<\/span> <span>{<\/span> <span>paymentRepository<\/span><span>.<\/span><span>markRefunded<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>externalRefundService<\/span><span>.<\/span><span>notify<\/span><span>(<\/span><span>order<\/span><span>);<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<hr>\n<p>I cover topics like this in more depth in my guide \u2014 Core Java, Java 8 to 21, Multithreading, Spring Boot, Microservices, Design Patterns, and Coding Round Patterns.<\/p>\n<p>Free sample: <a href=\"https:\/\/drive.google.com\/file\/d\/1u3PQbTY1gLn34UmWG7Cxx4cmdibD2dvU\/view?usp=sharing\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/drive.google.com\/file\/d\/1u3PQbTY1gLn34UmWG7Cxx4cmdibD2dvU\/view?usp=sharing<\/a><\/p>\n<p>Full guide: <a href=\"https:\/\/kamaninikhil.gumroad.com\/l\/java-interview-guide\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/kamaninikhil.gumroad.com\/l\/java-interview-guide<\/a><\/p>\n<p>Got either one right? Drop it in the comments.<\/p>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Fuente: <a href=\"https:\/\/dev.to\/nikz11\/2-transactional-traps-that-catch-even-senior-java-developers-in-interviews-41eb\">Art\u00edculo original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have 13 years of Java experience. I have also interviewed hundreds of developers at MNCs. Two questions about @Transactional that catch even senior developers. Try answering before you scroll down. Q1: If a non-transactional method calls a @Transactional method in the same class, does the transaction apply? @Service public class OrderService { public void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[41],"tags":[],"class_list":["post-3456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devto"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/3456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/comments?post=3456"}],"version-history":[{"count":0,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/3456\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media\/3455"}],"wp:attachment":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media?parent=3456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/categories?post=3456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/tags?post=3456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}