title: Atomikos rollback doesn't clear JPA persistence context?
body: <p>I have a Spring/JPA/Hibernate application and am trying to get it to pass my Junit integration tests against H2 and MySQL.  Currently I am using Atomikos for transactions and C3P0 for connection pooling.  </p>
  
  <p>Despite my best efforts my DAO integration one of the tests is failing with org.hibernate.NonUniqueObjectException.  In the failing test I create an object with the "new" operator, set the ID and call persist on it.  </p>
  
  <pre><code>@Test
  @Transactional
  public void save_UserTestDataNewObject_RecordSetOneLarger() {
      int expectedNumberRecords = 4;
      User newUser = createNewUser();
  
      dao.persist(newUser);   
      List&lt;User&gt; allUsers = dao.findAll(0, 1000);
  
      assertEquals(expectedNumberRecords, allUsers.size());
  }
  </code></pre>
  
  <p>In the previous testmethod I do the same thing (createNewUser() is a helper method that creates an object with the same ID everytime).  I am sure that creating and persisting a second object with the same Id is the cause, but each test method is in own transaction and the object I created is bound to a private test method variable.  I can even see in the logs that Spring Test and Atomikos are rolling back the transaction associated with each test method.</p>
  
  <p>I would have thought the rollback would have also cleared the persistence context too.  On a hunch, I added an a call to dao.clear() at the beginning of the faulty test method and the problem went away!!  So rollback doesn't clear the persistence context???  If not, then who does??</p>
  
  <p>My EntityManagerFactory config is as follows:</p>
  
  <pre><code>&lt;bean id="myappTestLocalEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="myapp-core" /&gt;
          &lt;property name="persistenceUnitPostProcessors"&gt;
              &lt;bean class="com.myapp.core.persist.util.JtaPersistenceUnitPostProcessor"&gt;
                  &lt;property name="jtaDataSource" ref="myappPersistTestJdbcDataSource" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
  
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="database" value="$DS{hibernate.database}" /&gt;
                  &lt;property name="databasePlatform" value="$DS{hibernate.dialect}" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
  
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.transaction.factory_class"&gt;com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory&lt;/prop&gt;
                  &lt;prop key="hibernate.transaction.manager_lookup_class"&gt;com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.autocommit"&gt;false&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true"&lt;/prop&gt;
                  &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>