title: Cannot get @Transactional to work properly in Spring 3
body: <p>UPDATE:  Sorry I seemed to have forgot some things...see bottom of the post for dispatcher-servlet.xml as well as the beans that call the @Repository:</p>
  
  <p>I can't seem to get this to work for some reason. I keep getting the following exception on a em.flush() call:</p>
  
  <p>here is the exception:</p>
  
  <pre><code>2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.DispatcherServlet] - &lt;DispatcherServlet with name 'dispatcher' processing GET request for [/orphanware.com/]&gt;
  2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - &lt;Mapping [/] to HandlerExecutionChain with handler [com.orphanware.web.controller.HomeController@55ca6954] and 2 interceptors&gt;
  2011-08-29 00:29:32,143 DEBUG [org.springframework.web.servlet.DispatcherServlet] - &lt;Last-Modified value for [/orphanware.com/] is: -1&gt;
  2011-08-29 00:29:32,143 DEBUG [org.springframework.web.bind.annotation.support.HandlerMethodInvoker] - &lt;Invoking request handler method: public java.lang.String com.orphanware.web.controller.HomeController.index(java.util.Map)&gt;
  2011-08-29 00:29:32,144 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - &lt;Creating new EntityManager for shared EntityManager invocation&gt;
  2011-08-29 00:29:32,144 DEBUG [org.hibernate.impl.SessionImpl] - &lt;opened session at timestamp: 13146029721&gt;
  2011-08-29 00:29:32,144 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - &lt;generated identifier: 111, using strategy: org.hibernate.id.SequenceHiLoGenerator&gt;
  2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - &lt;Closing JPA EntityManager&gt;
  2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - &lt;Creating new EntityManager for shared EntityManager invocation&gt;
  2011-08-29 00:29:32,146 DEBUG [org.hibernate.impl.SessionImpl] - &lt;opened session at timestamp: 13146029721&gt;
  2011-08-29 00:29:32,146 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - &lt;Closing JPA EntityManager&gt;
  2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] - &lt;Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress&gt;
  2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - &lt;Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress&gt;
  2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - &lt;Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress&gt;
  2011-08-29 00:29:32,148 DEBUG [org.springframework.web.servlet.DispatcherServlet] - &lt;Could not complete request&gt;
  javax.persistence.TransactionRequiredException: no transaction is in progress
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
      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.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
      at $Proxy15.flush(Unknown Source)
      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.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
      at $Proxy15.flush(Unknown Source)
      at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
      at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
      at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
      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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
      at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
      at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:662)
  Aug 29, 2011 12:29:32 AM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet dispatcher threw exception
  javax.persistence.TransactionRequiredException: no transaction is in progress
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
      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.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
      at $Proxy15.flush(Unknown Source)
      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.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
      at $Proxy15.flush(Unknown Source)
      at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
      at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
      at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
      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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
      at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
      at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>applicationContext.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.1.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt; 
  
      &lt;!-- entity manager factory --&gt;
  
      &lt;bean id="emf"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
           &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- jpaVendorAdapter (works in conjunction with the persistence.xml) --&gt;
  
      &lt;bean id="jpaVendorAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="database" value="HSQL" /&gt;
          &lt;property name="showSql" value="true" /&gt;
          &lt;property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
      &lt;tx:annotation-driven 
           transaction-manager="transactionManager" 
           proxy-target-class="false" /&gt;
  
      &lt;bean id="transactionManager" 
            class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="emf" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is the repository code:</p>
  
  <pre><code>import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.orphanware.web.domain.SampleEntity;
  
  @Repository
  @Transactional
  public class SampleDao {
  
      @PersistenceContext(unitName="appPU")
      private EntityManager em;
  
      public void saveSample(SampleEntity sample) {
          em.persist(sample);
          em.flush();
      }
  
      public SampleEntity getSample(long id) {
          return em.find(SampleEntity.class, id);
      }
  }
  </code></pre>
  
  <p>Here is the Service that calls this repository:</p>
  
  <pre><code>package com.orphanware.web.services;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import com.orphanware.web.dao.SampleDao;
  import com.orphanware.web.domain.SampleEntity;
  
  @Service
  public class SampleService {
  
      @Autowired
      private SampleDao sampleDao;
  
      public void saveSample(String name){
          SampleEntity se = new SampleEntity();
          se.setName(name);
  
          sampleDao.saveSample(se);
      }
  
      public SampleEntity getSample(long id) {
          return sampleDao.getSample(id);
      }
  }
  </code></pre>
  
  <p>and the controller that calls the service:</p>
  
  <pre><code>package com.orphanware.web.controller;
  
  import java.util.Map;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import com.orphanware.web.domain.SampleEntity;
  import com.orphanware.web.services.SampleService;
  
  @Controller
  public class HomeController {
  
      @Autowired
      private SampleService sampleService;
  
      @RequestMapping({"/home", "/"})
      public String index(Map&lt;String, Object&gt; model) {
          String[] stuff ={"stuff0", "stuff1", "stuff3"};
  
          for(String item : stuff) {
          sampleService.saveSample(item);
          }
  
          SampleEntity firstOne = sampleService.getSample(0);
  
          model.put("myVar", firstOne.name);
          return "home/index";
      }
  }
  </code></pre>
  
  <p>and finally the dispatcher-servlet.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:p="http://www.springframework.org/schema/p"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;context:component-scan base-package="com.orphanware.web" /&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>If I don't call flush then I don't get an exception but nothing is persisted.  I've tried also putting the @Transactional annotation on the method and also change the propagation, etc....</p>
  
  <p>Any help would be greatly appriciated.  Maybe I've just been looking at it for too long</p>