Spring @Transaction not starting transactions Spring @Transaction not starting transactions spring spring

Spring @Transaction not starting transactions


My guess would be that you're trying to do something like:

ServiceLocator locator = new ServiceLocatorImpl();...locator.executeService(someMap);

and then being surprised that there's no transaction. Transaction management and all other Spring services only apply to beans in the application context*. You need to get your instance from the context one way or another instead of just instantiating one. Or else your locator bean is in a separate application context than the one where you declare tx:annotation-driven.

*Unless you're using AspectJ build- or load-time bytecode weaving with Spring.

Edit: The problem was exactly what I said (the second part). You create two application contexts. You were creating your ServiceLocator in the first one, but you only enabled annotation-driven transactions in the second one. You appear to not understand the boundaries between the contexts. Generally--at least in my experience--the "business" beans, like your ServiceLocator, live in the root context, which is the one started by the ContextLoaderListener and configured via contextConfigLocation. Controllers and other beans that configure or are used by a DispatcherServlet live in another context associated with that servlet which is configured by the *-servlet.xml file. This context becomes a child context of the root context, and the beans in it can be injected with beans from the root context, though not vice versa.

From my perspective, you've broken things worse than they were before by adding tx:annotation-driven to the child context associated to your DispatcherServlet. Instead, you should ensure that the ServiceLocator is created in the root context, where transactional services are already available and are where they belong.


You should simply rename your "txManager" to "transactionManager". From the EnableTransactionManagement's JavaDoc:

…in the XML case, the name is "transactionManager". The <tx:annotation-driven/> is hard-wired to look for a bean named "transactionManager" by default…