6

My @Transactionnal annotations seems to be ignored. I have no errors on the initialization of the Spring container. It looks like my method has not been proxied by Spring TX framework. During the execution of my service's method, an expected RuntimeException is thrown by the JDBCTemplate. The problem is that the JDBC connection is not rolledbacked and changes are persisted. The stacktrace doesn't show any sign of the proxy who is supposed to wrap my service's method.

Edit : Added the Controller's code

Edit 2 : Added the Service's Interface

Here is my Service Interface.

public interface ApplicationsService {
    public Application getApplicationById(int id);

    public void createApplication(Application application);

    public void createInstance(Application application);

    public Map<Integer, Application> getUserApplications(String username);

    public Application newApplication(String email);
}

Here is my Service.

@Service
public class ApplicationsServiceImpl implements ApplicationsService {
    ...
    @Transactional
    public void createApplication(Application application){
        // Persisting the application.
        applicationDAO.createApplication(application);
        application.setId(
            applicationDAO.findApplicationId(application.getName(), application.getAccount().getEmail())
        );

        // Creating the physical instance.
        createInstance(application);
    }
    ...
}

Spring Controller responsible for the method call.

@Controller
@RequestMapping("/applications")
public class ApplicationsController {
    ...
    @Autowired
    private ApplicationsService applicationsService;
    ...

    @RequestMapping(method=RequestMethod.POST)
    public String saveApplication(
        @Valid Application application, 
        BindingResult bindingResult, 
        Principal principal
    ){  
        application.setAccount(this.accountService.getAccount(principal.getName()));
        this.applicationsService.createApplication(application);

        return "application/creatingApplication";
    }
    ...
}

Here is my Spring transaction configuration

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="    http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="DADataSource"/>
    </bean>

    <tx:annotation-driven />
</beans>

During the execution of the createApplication, a RuntimeException is launched by the JDBCTemplate and the transaction is not rollbacked.

GRAVE: Servlet.service() for servlet [DACloudWeb] in context with path [/DACloudWeb] threw exception [Request processing failed; 
nested exception is org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO instances (  serverId,   appId,  lastDeployment ) VALUES (   ?,?,? ) ]; SQL state [HY000]; error code [1364]; Field 'status' doesn't have a default value; nested exception is java.sql.SQLException: Field 'status' doesn't have a default value] with root cause
    java.sql.SQLException: Field 'status' doesn't have a default value
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
        at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
        at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
        at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
        at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:1)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:812)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:868)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:876)
        at com.cspinformatique.dacloudWeb.applications.dao.InstanceJDBCDAO.createInstance(InstanceJDBCDAO.java:50)
        at com.cspinformatique.dacloudWeb.applications.service.InstanceService.createInstance(InstanceService.java:42)
        at com.cspinformatique.dacloudWeb.applications.service.ApplicationsService.createInstance(ApplicationsService.java:63)
        at com.cspinformatique.dacloudWeb.applications.service.ApplicationsService.createApplication(ApplicationsService.java:52)
        at com.cspinformatique.dacloudWeb.applications.controller.ApplicationsController.saveApplication(ApplicationsController.java:64)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:680)
Daniel Lavoie
  • 1,852
  • 1
  • 16
  • 19
  • 1
    Is your `createApplication()` method being invoked by a different method in the same class? Or is it being invoked from outside, by another class that gets `ApplicationService` injected? – Kevin Welker May 10 '12 at 16:58
  • Never mind, I just saw in your stack trace that it is being invoked externally. – Kevin Welker May 10 '12 at 17:01
  • Does your entity object `Application` has a field called `status`. Please check the constraints for that in the database level. Does it require any default values? Which database are you using? It would be good if you post your entity code as well. – raddykrish May 10 '12 at 18:54
  • I am well aware of the reason why i have an SQLException. In fact I'm expecting this exception to trigger a rollback on my transaction which doesn't occur. – Daniel Lavoie May 10 '12 at 21:08
  • It looks like someone else is having the same kind of problem : http://stackoverflow.com/q/9930965/1387094 – Daniel Lavoie May 11 '12 at 09:41

3 Answers3

13

My guess is that you've put your service beans in the context that belongs to the dispatcher servlet, where only controller beans are supposed to live, and then you've declared your transaction beans in the root context. The annotation-based transaction auto-proxying only applies within a single context, so your service beans in the other (wrong) context won't be affected. See my answer to "Why DispatcherServlet creates another application context?" for a fuller description of the problem. The root problem is that you don't understand how contexts are organized in a Spring MVC application.

Community
  • 1
  • 1
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
4

You need to define an interface for the @Transactional annotations to work:

public interface ApplicationsService {
    public void createApplication(Application application);
}

And the concrete class:

@Service
public class ApplicationsServiceImpl {
    @Transactional
    public void createApplication(Application application) {
        // ...
    }
}

Alternatively, per Kevin Welker's comment, if don't want an interface (though you probably should write an interface), you can configure use proxy-target-class:

<tx:annotation-driven proxy-target-class="true" />

edit

The message from your SQLException is:

Field 'status' doesn't have a default value

So maybe you're passing in null where you should be providing a value? Alternatively, check this post for some weirdness associated with this error.

Community
  • 1
  • 1
beerbajay
  • 19,652
  • 6
  • 58
  • 75
  • 4
    It doesn't have to be an interface, if you configure for class proxying. See [this answer for more](http://stackoverflow.com/a/4143586/433348) – Kevin Welker May 10 '12 at 17:19
  • I tried using an interface and still have the same results. I will update the changes I made. – Daniel Lavoie May 10 '12 at 17:45
  • If together with restlets, you do not need the interface and no need to configure anything but only methods that are called from the restlet gain transactions. – Audrius Meškauskas Jun 05 '15 at 14:42
2

After three days of debugging, I finally found the reason why my annotations were ignored.

The <tx:annotation-driven/> instruction located in a child context file doesn't have access to the beans created by its parent Spring context.

I had to move it to the myapp-servlet.xml used by my request dispatcher.

Now, it is working properly.

Daniel Lavoie
  • 1,852
  • 1
  • 16
  • 19
  • This isn't the correct solution. It's a workaround at best, and you've misunderstood which context is the parent and which is the child. See [my answer](http://stackoverflow.com/a/10564908/839646) for the correct explanation/solution. – Ryan Stewart May 12 '12 at 15:16