title: Update Logic for JPA Entity Fails when using DBUnit and Spring
body: <p>I am currently using DBUnit in conjunction with Spring in order to unit test my application, but have run into an issue where my update logic test always fails because a deadlock occurs on the database and I cannot figure out why this is the case.  Please note that I have been able to get around the issue by removing the method annotated by @After, which really isn't needed because I am using the @TransactionConfiguration annotation, but I'm concerned that I'm misunderstanding something regarding how the transaction processing works and thus am hoping someone can indicate why I always get the following exception when running my updateTerritory method.</p>
  
  <pre><code>java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested
  </code></pre>
  
  <p>One thing that may be helpful to point out is that I am able to perform other actions like querying the database and inserting new records without any lock errors.  In addition I am using OpenJPA and spring is injecting the PersistenceUnit into my DAO.  I'm guessing that mixing up the usage of the PersistenceUnit and the direct use of the datasource within my DBUnit setup code (testSetup and testTeardown) may be part of the issue.  I'm currently using Derby as my database.  </p>
  
  <p>My Code is provided below:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "/applicationContext.xml")
  @TransactionConfiguration(defaultRollback = true)
  public class TerritoryZoneManagerTest {
  
  @Autowired
  private DataSource unitTestingDataSource;
  
  @Autowired
  private ITerritoryZoneDaoManager mgr;
  
  @Before
  public void testSetup() throws DatabaseUnitException, SQLException,
          FileNotFoundException {
      Connection con = DataSourceUtils.getConnection(unitTestingDataSource);
      IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
  
      FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
      IDataSet dataSet = builder
              .build(new FileInputStream(
                      "./src/com.company.territoryzonelookup/dao/test/TerritoryZoneManagerTest.xml"));
  
      try {
  
          // NOTE: There is no need to use the DatabaseOperation.DELETE
          // functionality because spring will automatically remove all
          // inserted records after each test case is executed.
          DatabaseOperation.REFRESH.execute(dbUnitCon, dataSet);
      } finally {
          DataSourceUtils.releaseConnection(con, unitTestingDataSource);
      }
  }
  
      @After
  public void testTeardown() throws DatabaseUnitException, SQLException,
          FileNotFoundException {
      Connection con = DataSourceUtils.getConnection(unitTestingDataSource);
      IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
  
      FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
      IDataSet dataSet = builder
              .build(new FileInputStream(
                      "./src/com.company.territoryzonelookup/dao/test/TerritoryZoneManagerTest.xml"));
  
      try {
  
          // NOTE: There is no need to use the DatabaseOperation.DELETE
          // functionality because spring will automatically remove all
          // inserted records after each test case is executed.
          DatabaseOperation.DELETE.execute(dbUnitCon, dataSet);
      } finally {
          DataSourceUtils.releaseConnection(con, unitTestingDataSource);
      }
  }
  
  @Test
  @Transactional
  public void updateTerritory() {
      TerritoryZone zone = new TerritoryZone();
      int id = 1;
      zone = mgr.getTerritory(id);
  
      String newCity = "Congerville";
      zone.setCity(newCity);
      mgr.updateTerritory(zone);
  
      zone = mgr.getTerritory(id);
      Assert.assertEquals(newCity, zone.getCity());
  }
  }
  </code></pre>
  
  <p>The DAO object is provided below as well in case that is useful.</p>
  
  <pre><code>@Repository
  public class TerritoryZoneDaoManager implements ITerritoryZoneDaoManager {
  
  /*
  @Autowired
  private EntityManagerFactory emf;
  */
  
  /*
   * @PersistenceUnit EntityManagerFactory emf;
   * 
   * @PersistenceContext private EntityManager getEntityManager(){ return
   * emf.createEntityManager(); }
   */
  
  @PersistenceContext
  private EntityManager em;
  
  private EntityManager getEntityManager() {
      // return emf.createEntityManager();
      return em;
  }
  
  /* (non-Javadoc)
   * @see com.company.territoryzonelookup.dao.ITerritoryZoneManager#addTerritory(com.company.territoryzonelookup.dao.TerritoryZone)
   */
  @Override
  public TerritoryZone addTerritory(TerritoryZone territoryZone) {
      EntityManager em = getEntityManager();
      em.persist(territoryZone);
      return territoryZone;
  }
  
  /* (non-Javadoc)
   * @see com.company.territoryzonelookup.dao.ITerritoryZoneManager#getTerritory(int)
   */
  @Override
  public TerritoryZone getTerritory(int id) {
      TerritoryZone obj = null;
      Query query = getEntityManager().createNamedQuery("selectById");
      query.setParameter("id", id);
      obj = (TerritoryZone) query.getSingleResult();
      return obj;
  }
  
  /* (non-Javadoc)
   * @see com.company.territoryzonelookup.dao.ITerritoryZoneManager#updateTerritory(com.company.territoryzonelookup.dao.TerritoryZone)
   */
  @Override
  public TerritoryZone updateTerritory(TerritoryZone territoryZone){
      getEntityManager().merge(territoryZone);
      return territoryZone;
  }
  
  /* (non-Javadoc)
   * @see com.company.territoryzonelookup.dao.ITerritoryZoneManager#getActiveTerritoriesByStateZipLob(java.lang.String, java.lang.String, java.util.Date, java.lang.String)
   */
  @Override
  public List&lt;TerritoryZone&gt; getActiveTerritoriesByStateZipLob(String stateCd, String zipCode, Date effectiveDate, String lobCd){
      List&lt;TerritoryZone&gt; territoryList;
  
      Query query = getEntityManager().createNamedQuery("selectActiveByZipStateLob");
      query.setParameter("zipCode", zipCode);
      query.setParameter("state", stateCd);
      query.setParameter("lob",lobCd);
      query.setParameter("effectiveDate", effectiveDate);
  
      territoryList = (List&lt;TerritoryZone&gt;) query.getResultList();
  
      return territoryList;
  }
  
  /* (non-Javadoc)
   * @see com.company.territoryzonelookup.dao.ITerritoryZoneManager#deleteAll()
   */
  @Override
  public void deleteAll(){
      Query query = getEntityManager().createNativeQuery("Delete from TerritoryZone");
      query.executeUpdate();
  }
  
  /***
   * the load method will remove all existing records from the database and then will reload it using it the data passed.
   * @param terrList
   */
  public void load(List&lt;TerritoryZone&gt; terrList){
      deleteAll();
      for (TerritoryZone terr:terrList){
          addTerritory(terr);
      }
  }
  
  }
  </code></pre>
  
  <p>Thanks in advance for your assistance.
  Jeremy</p>