-1

I have an old controller within my app that is defined as a spring bean in xml and makes use of Spring's SimpleFormController. I've tried to make the processes within the onSubmit method of the controller transactional by adding the @Transactional annotation but it doesn't work. According to this guide the invocation of the annotation must happen "outside of the bean", does this mean that the annotation cannot be used in old Spring controllers like mine? Are there any alternatives or workarounds?

The reason I know it's not working is because 1) changes to the db are not rolled back on error (this is despite the fact that I have defined rollbackFor = Exception.class, and even in some instances used TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();, in this instances where it tries to use the latter it throws an error stating there is no transaction present. 2) I've added breakpoints to where @Transactional is instantiated within Spring and none of them get hit.

EDIT: So people are asking for reproducible examples of code. The problem doesn't lie within the business logic code, I'm looking for clarity on the usage of the annotation within a Spring 2 controller. So what I have for example is this:

public class ImportController extends SimpleFormController {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    ...
    }
}
mohammedkhan
  • 953
  • 6
  • 14
  • Please provide a [mcve] – Lino Jun 15 '20 at 13:53
  • @Lino It's an annotation on a method, there is no reproducible example that I can give you here, particular as the code is business sensitive it's not something I can post on a public forum and it wouldn't provide any indications as to what the problem is. – mohammedkhan Jun 15 '20 at 14:03
  • 1
    One of the purposes of the reproducible example is for the _asker_ to better understand the problem. You create a simple example first and it will either work so you can check the difference between the example and your actual code by gradually moving from it to the configuration you have in actual code. Or it will not work and you post the simple example here so we can help. Without seeing the code that does not work I'm afraid it is very hard to understand the problem and help with it. – Roman-Stop RU aggression in UA Jun 15 '20 at 14:12
  • @RomanKonoval the business logic code I have is not the problem, it's the use of the annotation within a spring controller I need clarity on. I can post some pseudo-code, but you will see it has no benefit to anyone. – mohammedkhan Jun 15 '20 at 14:14

1 Answers1

1

You are right. @Transactional will not work here because onSubmit is invoked by the same bean.

And in this case the call is done directly and the default spring transaction handling does not work.

See answers in this question for a detailed explanation of the options you have

  • So it would seem this answer is best placed for my scenario: https://stackoverflow.com/a/3429757/3415090, we already have `` but adding ` ` breaks the app on startup as I assume this interferes with the other annotations in the system that are used in our spring 3 controllers. – mohammedkhan Jun 15 '20 at 14:35
  • Maybe because of the mishmish of different ways of setting up controllers in our system means there is no viable solution for this problem. – mohammedkhan Jun 15 '20 at 14:37
  • I think my only real viable option is to create a service class that gets instantiated as a separate bean which can be called from controller. I'm pretty sure that will work. Thanks for the link. – mohammedkhan Jun 15 '20 at 15:19