Spring @Transactional annotations ignored

前端 未结 3 1142
野趣味
野趣味 2020-12-05 21:52

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

相关标签:
3条回答
  • 2020-12-05 22:16

    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.

    0 讨论(0)
  • 2020-12-05 22:37

    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.

    0 讨论(0)
  • 2020-12-05 22:40

    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.

    0 讨论(0)
提交回复
热议问题