title: How to manually force a commit in a @Transactional method?
body: <p>I'm using Spring / Spring-data-JPA and find myself needing to manually force a commit in a unit test.  My use case is that I am doing a multi-threaded test in which I have to use data that is persisted before the threads are spawned.</p>
  
  <p>Unfortunately, given that the test is running in a <code>@Transactional</code> transaction, even a <code>flush</code> does not make it accessible to the spawned threads.</p>
  
  <pre><code>   @Transactional   
     public void testAddAttachment() throws Exception{
          final Contract c1 = contractDOD.getNewTransientContract(15);
          contractRepository.save(c1);
  
          // Need to commit the saveContract here, but don't know how!                
          em.getTransaction().commit();
  
          List&lt;Thread&gt; threads = new ArrayList&lt;&gt;();
          for( int i = 0; i &lt; 5; i++){
              final int threadNumber = i; 
              Thread t =  new Thread( new Runnable() {
                  @Override
                  @Transactional
                  public void run() {
                      try {
                          // do stuff here with c1
  
                          // sleep to ensure that the thread is not finished before another thread catches up
                          Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
              });
              threads.add(t);
              t.start();
          }
  
          // have to wait for all threads to complete
          for( Thread t : threads )
              t.join();
  
          // Need to validate test results.  Need to be within a transaction here
          Contract c2 = contractRepository.findOne(c1.getId());
      }
  </code></pre>
  
  <p>I've tried using the entity manager to, but get an error message when I do:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead; nested exception is java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:293)
      at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
  </code></pre>
  
  <p>Is there any way to commit the transaction and continue it?  I have been unable to find any method that allows me to call a <code>commit()</code>.</p>