title: JPA Error : Transaction is currently active
body: <p>When I run my unit tests in isolation they work fine ie. (omitted the asserts)</p>
  
  <pre><code>@Test
  public void testSave()
   {
  EntityManagerHelper emh = new EntityManagerHelper();
  LevelDAO dao = new LevelDAO();
  Level l = new Level();
  l.setName("aname");
  emh.beginTransaction();
  dao.save(l);
  emh.commit();
  }
  </code></pre>
  
  <p>then running this individual test below no problem</p>
  
  <pre><code>@Test
  public void testUpdate()
   {
  EntityManagerHelper emh = new EntityManagerHelper();
  LevelDAO dao = new LevelDAO();
  Level l = new Level();
  l.setName("bname");
  l.setLevelid(1);
  emh.beginTransaction();
  dao.update(l);
  emh.commit();
  }
  </code></pre>
  
  <p>When they run at same time in sequence I recieve that error - <strong>Transaction is currently active</strong>. Is there a way to allow each unit test to run only after a transaction from previous piece of work is not active? Should I be looking at Spring instead?</p>
  
  <p><strong>Update</strong></p>
  
  <p>The EntityManagerHelper gains access to the persistence context like so</p>
  
  <pre><code>emf = Persistence.createEntityManagerFactory("bw_beta");        
  threadLocal = new ThreadLocal&lt;EntityManager&gt;();
  </code></pre>
  
  <p>which looks like the problem</p>
  
  <p>So a hacky workaround was to use define locally ie.</p>
  
  <pre><code>    EntityManagerFactory factory = Persistence.createEntityManagerFactory("bw_beta");
      EntityManager entityManager = factory.createEntityManager();
      entityManager.getTransaction().begin();
      dao.save(l);
      entityManager.persist(l);
      entityManager.getTransaction().commit();
  </code></pre>
  
  <p>Pretty sure there's a better way - maybe using Spring?</p>