title: Spring - JPA -m@Transactional not committing data in DB
body: <p>I have seen many questions but all of their solution still not clear.
  here is my set up
  my Spring Context file</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:context="http://www.springframework.org/schema/context"
      ..
      ...
  
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      ...
      ...
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="entityManagerFactory1"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          autowire="byName"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan" value="com.my.dom.domain" /&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;
                      org.hibernate.dialect.Oracle10gDialect
                  &lt;/prop&gt;             
                  &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="persistenceUnitName" value="PersistenceUnit1"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="EntityManagerFactory2"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          autowire="byName"&gt;
          &lt;property name="dataSource" ref="myDataSource" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan" value="com.my.dom.domain" /&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;
                      org.hibernate.dialect.H2Dialect
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
       ..
       ...     
  
  
      &lt;bean id="entityManager1" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory1" /&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven transaction-manager="entityManager1" /&gt;
  
      &lt;bean id="EntityManager2" factory-bean="EntityManagerFactory2" factory-method="createEntityManager" autowire="byName" scope="prototype"/&gt;
      &lt;tx:annotation-driven transaction-manager="EntityManager2" /&gt;
  
  
      &lt;context:component-scan base-package="com.my.dom" /&gt;
  
      &lt;jpa:repositories base-package="com.my.dom.repository"
          entity-manager-factory-ref="entityManagerFactory"
          transaction-manager-ref="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <hr>
  
  <p>All data source and other connection beans are in place and working fine.</p>
  
  <p>My DAO code is as follows
      package com.my.dom.dao;</p>
  
  <pre><code>@Component
  public class MyDAOImpl implements MyDAO {
  
  @PersistenceContext(unitName="PersistenceUnit1")
  EntityManager entityManager; // this is IAR EntityManager.
  
  @Override
  @Transactional
  public boolean saveMyGroups(
          List&lt;MyGroups&gt; theGroups) {
  
      logger.info("Entering method -  saveFuturetheGroups ");
      //entityManager.getTransaction().begin();
      for(MyGroups pg : theGroups){
          MyGroups attachedENtity = entityManager.merge(pg);
          entityManager.persist(pg);
      }
      //entityManager.getTransaction().commit();
      entityManager.flush();
      logger.info("Exiting method -  saveFuturetheGroups ");
      //List&lt;MyGroups&gt; savedEntities = futureProcGrpRepository.save(theGroups);
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have all the cglib and aopalliance1.0.jars in classpath as suggested in <a href="https://stackoverflow.com/questions/5201498/springjpa-transactional-not-committing">Spring+JPA @Transactional not committing </a></p>
  
  <p>Still my code is not committing the changes to DB. ANy help pointers will be really helpful.</p>
  
  <p>The code shows select and insert sql statements and no exceptions throws but code doesnt commit. 
  Is there any issues in @Transactional annotation?</p>
  
  <p>Can defining multiple entitymanagers be any issues here?</p>
  
  <hr>
  
  <p>Ok what I have found is that if I use 
      @Transactional(propagation=Propagation.REQUIRES_NEW)</p>
  
  <p>on top of my method it commits the changes !! But as per my under standing the default value for Propagation is 'REQUIRED' which any way will start a new transction and should commit at end? Any pointers I am now really confused!!</p>
  
  <p>Thanks
  HK</p>
  
  <p>Just a note , I am running this from A Junit test case , is it possible that it is explicitely rolling back the changes? I see something like following in logs</p>
  
  <pre><code>INFO : org.springframework.test.context.transaction.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [[TestContext@a5503a testClass = ProcessingGroupsTestCase, testInstance = null(com.wellmanage.dos.processingGroups.ProcessingGroupsTestCase), testMethod = testSave@ProcessingGroupsTestCase, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1363f5a testClass = ProcessingGroupsTestCase, locations = '{classpath:applicationContext.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]
  </code></pre>
  
  <p>Thanks
  HK</p>