Note that there are some explanatory texts on larger screens.

plurals
  1. POTransaction not working correctly - Spring/MyBatis
    text
    copied!<p>I have a Spring MVC Controller method which is tagged as "Transactional" which makes several service calls which are also tagged "Transactional" but they are treated as independent transactions and are committed separately instead of all under one transaction as I desire.</p> <p>Based on the debug output, it appears Spring is only creating transactions when it reaches the service calls. I will see the output "Creating new transaction with name..." only for them but not one for the controller method who calls them. </p> <p>Is there something obvious I am missing?</p> <p>Sample code (abbreviated) below, controller:</p> <pre><code>@Controller public class BlahBlah etc... @Autowired SomeService someService; @Transactional @RequestMapping(value="windows/submit", method=RequestMethod.POST) @ResponseBody public String submit (@RequestBody SomeObject blah) throws Exception { someService.doInsert(blah); if (true) throw Exception("blah"); ... other stuff } </code></pre> <p>service code:</p> <pre><code>public interface SomeService { @Transactional public void doInsert (SomeObject blah); } </code></pre> <p>I tried removing the Transactional tag from the service call thinking maybe I messed it up and I am telling it to create one for each call but then in the debug output no transaction is created.</p> <p>So the result is once I get my forced exception and check the table, the insert has committed instead of rolled back like I want.</p> <p>So what did I do wrong?</p> <p>Why is Spring ignoring the Transactional tag on the controller?</p> <hr> <p>Posting relevant part of my context as per request from commenter:</p> <p>Web.xml:</p> <pre><code>&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; classpath:spring.xml classpath:spring-security.xml classpath:spring-datasource.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;servlet&gt; &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:spring-mvc.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>sping-mvc.xml:</p> <pre><code> &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.blah"/&gt; &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1" /&gt; ... non-relevent stuff &lt;mvc:interceptors&gt; &lt;mvc:interceptor&gt; &lt;mvc:mapping path="/**"/&gt; &lt;bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"&gt; &lt;property name="cacheSeconds" value="0"/&gt; &lt;property name="useExpiresHeader" value="true"/&gt; &lt;property name="useCacheControlHeader" value="true"/&gt; &lt;property name="useCacheControlNoStore" value="true"/&gt; &lt;/bean&gt; &lt;/mvc:interceptor&gt; &lt;mvc:interceptor&gt; &lt;mvc:mapping path="/**"/&gt; &lt;bean id="httpInterceptor" class="com.blah.BlahInterceptor" /&gt; &lt;/mvc:interceptor&gt; &lt;/mvc:interceptors&gt; &lt;mvc:view-controller path="/" view-name="blah"/&gt; &lt;context:component-scan base-package="com.blah"/&gt; </code></pre> <p>Hmm, thats interested and unintended - I have component scan duplicated. Could that be causing problems with this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload