title: JBoss 7 Spring JPA + Hibernate Application: no transaction is in progress
body: <p>I'm trying to develop a simple Payroll Application with Spring 3.2 using JPA and Hibernate and the JPA Provider. I'm deploying it on the JBoss AS 7 Server. I keep getting the following error:</p>
  
  <pre><code>16:42:13,811 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/PayrollApp].[payrollApp]] (http--127.0.0.1-8080-1) Servlet.service() for servlet payrollApp threw exception: javax.persistence.TransactionRequiredException: no transaction is in progress
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:993) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
      at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at com.sun.proxy.$Proxy30.flush(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
      at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at com.sun.proxy.$Proxy30.flush(Unknown Source)
      at com.colenso.payrollapp.persistence.daos.GenericDAO.add(GenericDAO.java:34) [classes:]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
      at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at com.sun.proxy.$Proxy31.add(Unknown Source)
      at com.colenso.payrollapp.service.HRServiceImpl.addEmployee(HRServiceImpl.java:29) [classes:]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
      at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at com.sun.proxy.$Proxy32.addEmployee(Unknown Source)
      at com.colenso.payrollapp.controllers.PaySlipController.showHomePage(PaySlipController.java:42) [classes:]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
      at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
      at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
      at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
      at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
  </code></pre>
  
  <p>I'm using Spring 3.2 without any <strong>persistence.xml</strong> file.
  Relevant parts of my <strong>Spring App Config</strong> file:</p>
  
  <pre><code>&lt;!--    Turn on annotation driven features of spring--&gt;
      &lt;mvc:annotation-driven/&gt;
      &lt;!--&lt;context:annotation-config /&gt;--&gt;
  
      &lt;!--Enable Spring to detect the Controllers and other components automatically and configure them as beans--&gt;
      &lt;context:component-scan base-package="com.colenso.payrollapp"  /&gt;
  &lt;!--Look up the Datasource that's been configured in JBoss as a JNDI datasource--&gt;
      &lt;jee:jndi-lookup id="datasource" jndi-name="java:jboss/datasources/MySqlDS" resource-ref="false" /&gt;
  
  
  
      &lt;!--Configuration for the Entity Manger Factory--&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;  
          &lt;property name="packagesToScan" value="com.colenso.payrollapp.persistence.entities" /&gt;  
          &lt;property name="jpaVendorAdapter"&gt;  
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;  
          &lt;/property&gt;  
          &lt;property name="dataSource" ref="datasource"/&gt;  
          &lt;property name="jpaPropertyMap" ref="jpaPropertyMap" /&gt;
          &lt;property name="persistenceUnitName" value="payrollPU" /&gt;
      &lt;/bean&gt;  
  
      &lt;util:map id="jpaPropertyMap"&gt;  
          &lt;entry key="show_sql" value="true" /&gt;  
          &lt;entry key="dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;  
          &lt;entry key="hibernate.hbm2ddl.auto" value="validate" /&gt;  
          &lt;entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/&gt;  
          &lt;entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/&gt;  
          &lt;entry key="hibernate.cache.use_second_level_cache" value="true" /&gt;  
          &lt;entry key="hibernate.cache.use_query_cache" value="false" /&gt;  
          &lt;entry key="hibernate.generate_statistics" value="false" /&gt;  
          &lt;entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" /&gt;
      &lt;/util:map&gt;
  
  
      &lt;!--Enable injection of the Entity Manager--&gt;
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
      &lt;!--Enable Spring's Exception Translation Mechanism--&gt;
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  
  
      &lt;!--Configuration for the Transactions--&gt;
      &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
          &lt;property name="transactionManagerName" value="java:jboss/TransactionManager"/&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven /&gt;
      &lt;tx:jta-transaction-manager /&gt;
  </code></pre>
  
  <p>As you can see, I'm pulling the Datasource that I have configured in the <strong>standalone.xml</strong> file. </p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@Controller
  public class PaySlipController {
  
      @Autowired
      HRService hrService;
  
  
      public PaySlipController() {
      }
  
      @RequestMapping("/")
      public String showHomePage(Map&lt;String, Object&gt; model) {
          Employee employee1;
          employee1 = new Employee();
          employee1.setFirstName("FN");
          employee1.setLastName("LN");
          Calendar myDOB = new GregorianCalendar(1965, 6, 15);
          employee1.setDateOfBirth(myDOB);
  
  
          hrService.addEmployee(employee1);
  
          return "home";
      }
  }
  </code></pre>
  
  <p>I'm following the <code>The type-safe generic DAO pattern</code> as mentioned in this article <a href="http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/" rel="nofollow">enter link description here</a>
  Here is all the relevant code:
  <strong>Interface</strong>
      public interface GenericDAOInterface {</p>
  
  <pre><code>void add(E entitry);
  
  void remove(E entity);
  
  E findById(K id);
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Implementation</strong></p>
  
  <pre><code>public abstract class GenericDAO&lt;E, K&gt; implements GenericDAOInterface&lt;E, K&gt; {
  
  protected Class entityClass;
  @PersistenceContext
  protected EntityManager entityManager;
  
  public GenericDAO() {
      ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
      this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[1];
  }
  
  @Override
  public void add(E entity) {
      if (entityManager==null)
          System.out.println("Entity Manager Not Initialised!!");
      entityManager.persist(entity);
      entityManager.flush();
  }
  
  @Override
  public void remove(E entity) {
      entityManager.remove(entity);
  }
  
  @Override
  public E findById(K id) {
      return (E) entityManager.find(entityClass, id);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Interface:</strong></p>
  
  <pre><code>public interface EmployeeDAO extends GenericDAOInterface&lt;Employee, Integer&gt;{
  
  }
  </code></pre>
  
  <p><strong>Implementation</strong></p>
  
  <pre><code>@Repository
  public class EmployeeDAOImpl extends GenericDAO&lt;Employee, Integer&gt; implements EmployeeDAO{
  
  }
  </code></pre>
  
  <p><strong>Interface:</strong></p>
  
  <pre><code>public interface HRService {
  
      public void addEmployee(Employee employee);
  }
  </code></pre>
  
  <p><strong>Implementation</strong></p>
  
  <pre><code>@Component
  @Transactional(propagation = Propagation.REQUIRED)
  public class HRServiceImpl implements HRService{
  
      @Autowired
      private EmployeeDAO employeeDao;
  
  
      @Override
      public void addEmployee(Employee employee){
          employeeDao.add(employee);
      }
  
  }
  </code></pre>