title: "no transaction is in progress" error while using @Transactional
body: <p>All,
  I am trying to use the annotational transaction in Spring and getting the 
  javax.persistence.TransactionRequiredException: no transaction is in progress</p>
  
  <p>error all the time.</p>
  
  <p>Eror stacktrace:</p>
  
  <pre><code>[22:40:11:354] 00000024 SystemErr     R javax.persistence.TransactionRequiredException: no transaction is in progress
  [22:40:11:354] 00000024 SystemErr     R     at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
  [22:40:11:354] 00000024 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
  [22:40:11:354] 00000024 SystemErr     R     at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
  [22:40:11:354] 00000024 SystemErr     R     at $Proxy45.flush(Unknown Source)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
  [22:40:11:354] 00000024 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
  [22:40:11:354] 00000024 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
  [22:40:11:354] 00000024 SystemErr     R     at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
  [22:40:11:354] 00000024 SystemErr     R     at $Proxy42.flush(Unknown Source)
  [22:40:11:354] 00000024 SystemErr     R     at com.test.MyDAO.saveOrUpdateData
  .
  .
  .
  </code></pre>
  
  <p>Please note that, the same code runs fine when I use AOP.</p>
  
  <p>The technology stack looks like this:</p>
  
  <pre><code>Spring 3.0.5
  Struts 2.2.1.1
  Hibernate 3.3.2GA
  JPA 1.0
  JBPM 4.4
  </code></pre>
  
  <p>Here are my application configurations:</p>
  
  <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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;!--    &lt;bean--&gt;
      &lt;!--        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;--&gt;
  
  
      &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/my_dataSource1.1"
          cache="true" resource-ref="true" lookup-on-startup="true"
          proxy-interface="javax.sql.DataSource" /&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="persistenceXmlLocation" value="META-INF/my-persistence.xml" /&gt;
      &lt;/bean&gt;
  
  &lt;!--    
  Also tried the JTA transaction manager....
  
  &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
  
  --&gt;
      &lt;bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/&gt;
  
      &lt;tx:annotation-driven/&gt;
  
  
  &lt;!--    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;--&gt;
  &lt;!--        &lt;tx:attributes&gt;--&gt;
  &lt;!--            &lt;tx:method name="get*" read-only="true" /&gt;--&gt;
  &lt;!--            &lt;tx:method name="saveOrUpdate*" propagation="REQUIRES_NEW" read-only="false"/&gt;--&gt;
  &lt;!--        &lt;/tx:attributes&gt;--&gt;
  &lt;!--    &lt;/tx:advice&gt;--&gt;
  &lt;!--    &lt;aop:config&gt;--&gt;
  &lt;!--        &lt;aop:pointcut id="daoOperations"--&gt;
  &lt;!--            expression="execution(* com.honda.naps.dao..*.*(..))" /&gt;--&gt;
  &lt;!--        &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperations" /&gt;--&gt;
  &lt;!--    &lt;/aop:config&gt;--&gt;
  
  
       &lt;!-- This is the DAO that's injected --&gt;
       &lt;bean id="myDAO" class="com.test.MyDAO" /&gt;
       &lt;bean id="myBaseDAO" class="com.test.BaseDAO" /&gt;
       &lt;!-- 
            This is the struts action that's created .. in struts.xml I have used
            &lt;constant name="struts.objectFactory" value="spring" /&gt;
        --&gt;    
        &lt;bean id="myAction" scope="prototype" class="com.test.MyAction"&gt;
        &lt;/bean&gt;
  
         &lt;!-- My BO instance --&gt;
         &lt;bean id="myBO" class="com.test.MyBOImpl"&gt;
            &lt;property name="myDAO" ref="myDAO" /&gt;
            &lt;property name="myBaseDAO" ref="myBaseDAO" /&gt;
          &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Also in 
  struts.xml, I invoke the saveOrUpdateData() on the MyDAO class.</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.5"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" metadata-complete="true"&gt;
      &lt;display-name&gt;NAPSWeb&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;
              org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/tiles.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt; 
          &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; 
          &lt;param-value&gt;/WEB-INF/resources/log4j.properties&lt;/param-value&gt; 
      &lt;/context-param&gt;
  
      &lt;context-param&gt;   
          &lt;param-name&gt;log4jRefreshInterval&lt;/param-name&gt; 
          &lt;param-value&gt;1000&lt;/param-value&gt; 
      &lt;/context-param&gt;    
  
      &lt;listener&gt; 
          &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt; 
      &lt;/listener&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.apache.struts2.tiles.StrutsTilesListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring_config/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;filter-class&gt;
              org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;*.action&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/struts/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;distributable&gt;&lt;/distributable&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>my-persistence.xml</strong></p>
  
  <p>http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
      
       org.hibernate.ejb.HibernatePersistence 
      jdbc/my_dataSource1.1</p>
  
  <pre><code>&lt;class&gt;com.test.MyTbl1&lt;/class&gt;
  &lt;class&gt;com.test.MyTbl2&lt;/class&gt;
  &lt;class&gt;com.test.MyTbl3&lt;/class&gt;    
  
  &lt;properties&gt;
          &lt;!-- HIBERNATE properties --&gt; 
          &lt;property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/&gt; 
          &lt;!--  &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/&gt; --&gt;
          &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereTransactionManagerLookup"/&gt;
          &lt;property name="jta.UserTransaction" value="java:comp/UserTransaction"/&gt; 
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/&gt;
          &lt;!--        &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/&gt;--&gt;
          &lt;property name="hibernate.showsql" value="true"/&gt;
          &lt;!--        &lt;property name="hibernate.cache.use_second_level_cache" value="false"/&gt;        --&gt;
          &lt;!--   &lt;property name="jta.UserTransaction" value="java:comp/UserTransaction"/&gt; --&gt;
          &lt;property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect"/&gt; 
  
  &lt;/properties&gt;
  
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>The DAO method class that uses @Transactional looks like this:</p>
  
  <p><strong>//Class MyDAO</strong></p>
  
  <pre><code>public class MyDAO extends BaseDAO
  {
      @Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
      public void saveOrUpdateData()
      {
          MyTbl1 mt1 = new MyTbl1();
          //do something with mt1
          getEntityManager().persist(mt1);
          getEntityManager().flush();
      }
  }
  </code></pre>
  
  <p><strong>BaseDAO class:</strong></p>
  
  <pre><code>public class BaseDAO {
  
      /**
       * Variable which stores the injected 
       * JPA Entity Manager.
       */ 
      private EntityManager entityManager;
  
      public EntityManager getEntityManager() {
          System.out.println("getting the entity manager.");
          return entityManager;
      }
  
      @PersistenceContext(unitName="MY_JPA")
      public void setEntityManager(EntityManager entityManager) {
          logger.debug("Setting the entity manager.");
          this.entityManager = entityManager;
      }
  
  }
  </code></pre>
  
  <p><strong>Libraries used:</strong></p>
  
  <pre><code>ejb3-persistence.jar
  hibernate-annotations.jar
  hibernate-commons-annotations.jar
  hibernate-entitymanager.jar
  hibernate3.3.2.GA.jar    
  antlr-2.7.6.jar
  aopalliance-1.0.jar
  asm-1.5.3.jar
  asm-all-2.2.3.jar
  aspectjrt.jar
  aspectjweaver.jar
  cglib-nodep-2.1_3.jar
  commons-beanutils-1.8.0.jar
  commons-collections-3.1.jar
  commons-digester-1.8.1.jar
  commons-io-1.3.2.jar
  commons-logging-1.0.4.jar
  commons-logging-api-1.1.jar
  dom4j-1.6.1.jar
  freemarker-2.3.8.jar
  javassist-3.12.0.GA.jar
  jbpm.jar
  juel-api.jar
  juel-engine.jar
  juel-impl.jar
  log4j-1.2.16.jar
  ognl-3.0.jar
  org.springframework.aop-3.0.5.RELEASE.jar
  org.springframework.asm-3.0.5.RELEASE.jar
  org.springframework.aspects-3.0.5.RELEASE.jar
  org.springframework.beans-3.0.5.RELEASE.jar
  org.springframework.context-3.0.5.RELEASE.jar
  org.springframework.context.support-3.0.5.RELEASE.jar
  org.springframework.core-3.0.5.RELEASE.jar
  org.springframework.expression-3.0.5.RELEASE.jar
  org.springframework.jdbc-3.0.5.RELEASE.jar
  org.springframework.orm-3.0.5.RELEASE.jar
  org.springframework.transaction-3.0.5.RELEASE.jar
  org.springframework.web-3.0.5.RELEASE.jar
  slf4j-api-1.6.1.jar
  slf4j-nop-1.6.1.jar
  struts2-convention-plugin-2.2.1.1.jar
  struts2-core-2.2.1.1.jar
  struts2-dojo-plugin-2.2.1.1.jar
  struts2-json-plugin-2.2.1.jar
  struts2-spring-plugin-2.2.1.1.jar
  struts2-tiles-plugin-2.1.6.jar
  tiles-api-2.1.2.jar
  tiles-compat-2.1.2.jar
  tiles-core-2.1.2.jar
  tiles-jsp-2.1.2.jar
  tiles-servlet-2.1.2.jar
  xwork-core-2.2.1.1.jar
  </code></pre>
  
  <p><strong>System confiuration:</strong></p>
  
  <pre><code>Websphere App server TE 7.0.0.15 in RAD IDE 7.5
  OS: Windows XP SP3
  DB: MS SQL Server 2008 enterprise edition
  </code></pre>
  
  <p>I have parent_last class loading enabled and assigned the default JPA provider as "org.hibernate.ejb.HibernatePersistence" in my Websphere app server.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Thanks</p>
  
  <p>=======================================</p>
  
  <p>Adding this for everybody's usage...</p>
  
  <p>I had to finally go woth AOP based transaction management. I could not find a way to get @Transactional working in my application.</p>
  
  <p>Adding below the part I added for AOP integration to my application (in the applicationContext.xml):</p>
  
  <pre><code>&lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="get*" read-only="true" /&gt;
          &lt;tx:method name="saveOrUpdate*" propagation="REQUIRES_NEW" read-only="false"/&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  &lt;aop:config&gt;
      &lt;aop:pointcut id="daoOperations"
          expression="execution(* com.test..*.*(..))" /&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperations" /&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <p>Hope this helps,</p>