Announcement Announcement Module
No announcement yet.
WebApplicationContext and @Transactional - documentation indicates only controller Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • WebApplicationContext and @Transactional - documentation indicates only controller

    For WebApplicationContext, should I put @Transactional annotations in the controller or in services? The Spring docs have me a bit confused.

    Here is my web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
      <display-name>Alpha v0.02</display-name>
    Here is my application-context.xml defining a spring dispatcher servlet:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <context:annotation-config />
        <mvc:annotation-driven />
        <tx:annotation-driven />
        <context:component-scan base-package="com.visitrend" />
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
         <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="org.postgresql.Driver" />
            <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
            <property name="user" value="someuser" />
            <property name="password" value="somepasswd" />
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:test.hibernate.cfg.xml" />
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="dataSource" ref="dataSource" />
          <property name="sessionFactory" ref="sessionFactory" />
    Here's a service interface:

    public interface LayerService {
        public void createLayer(Integer layerListID, Layer layer);
    Here's a service implementation:

    public class LayerServiceImpl implements LayerService {
        public LayerDAO layerDAO;
        public void createLayer(Integer layerListID, Layer layer) {
            layerDAO.createLayer(layerListID, layer);
    And here's my controller:

    public class MainController {
        private LayerService layerService;
        @RequestMapping(value = "/addLayer.json", method = RequestMethod.POST)
        public @ResponseBody
        LayerListSetGroup addLayer(@RequestBody JSONLayerFactory request) {
            layerService.createLayer(request.getListId(), request.buildLayer());
            return layerService.readLayerListSetGroup(llsgID);
    The Spring documentation has me a bit confused. It seems to indicate that using a WebApplicationContext means only controllers will be investigated for @Transactional annotations and not services. Meanwhile I see tons of recommendations to make services transactional and not controllers. I'm thinking that using <context:component-scan base-package="com..." /> in our spring-servlet.xml above so that it includes the services packages means the services are part of the context, and therefore will be "investigated" for transactional annotations. Is this accurate?

    Here's the Spring documentation blurb that got me confused:

    @EnableTransactionManagement and only looks for @Transactional on beans in the same application context they are defined in. This means that, if you put annotation driven configuration in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.
    Further, is there any performance implications or "badness" if I define a controller method as transactional, and it calls a transactional method in a another class? My hunch is no, based on the documentation, but would love validation on that. Thanks.

  • #2
    Don't define your controller methods as transactional, unless you want your transaction to depend on the successful rendering/handling of the request. The service-layer is the layer that should coordinate your transactions.

    The documentation is a bit misleading @Transactional will be checked on all beans in the same application context and will not only be bound to @Controllers. However in general for a DispatcherServlets ApplicationContext the statement is true as that should, generally speaking, only contain web-related beans (Controllers, hander mappings etc.).