【发布时间】:2012-01-17 16:29:44
【问题描述】:
我目前正在结合使用 DBUnit 和 Spring 来对我的应用程序进行单元测试,但是遇到了一个问题,即我的更新逻辑测试总是失败,因为数据库上发生了死锁,我无法弄清楚为什么会出现这种情况.请注意,我已经能够通过删除@After 注释的方法来解决这个问题,这实际上是不需要的,因为我使用的是@TransactionConfiguration 注释,但我担心我误解了一些关于如何事务处理有效,因此希望有人能指出为什么我在运行 updateTerritory 方法时总是得到以下异常。
java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested
需要指出的一点是,我能够执行其他操作,例如查询数据库和插入新记录,而不会出现任何锁定错误。此外,我正在使用 OpenJPA,spring 正在将 PersistenceUnit 注入我的 DAO。我猜测在我的 DBUnit 设置代码(testSetup 和 testTeardown)中混合使用 PersistenceUnit 和直接使用数据源可能是问题的一部分。我目前使用 Derby 作为我的数据库。
我的代码如下:
@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());
}
}
下面还提供了 DAO 对象,以防万一。
@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<TerritoryZone> getActiveTerritoriesByStateZipLob(String stateCd, String zipCode, Date effectiveDate, String lobCd){
List<TerritoryZone> territoryList;
Query query = getEntityManager().createNamedQuery("selectActiveByZipStateLob");
query.setParameter("zipCode", zipCode);
query.setParameter("state", stateCd);
query.setParameter("lob",lobCd);
query.setParameter("effectiveDate", effectiveDate);
territoryList = (List<TerritoryZone>) 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<TerritoryZone> terrList){
deleteAll();
for (TerritoryZone terr:terrList){
addTerritory(terr);
}
}
}
提前感谢您的帮助。 杰里米
【问题讨论】:
-
为什么要在 setup/teardown 方法中手动释放连接? DataSourceUtils.getConnection 应该为您提供与弹簧测试框架打开的活动事务的线程绑定连接。这个连接也应该被框架关闭。
-
好点@mrembisz。我已经删除了对 DataSourceUtils.releaseConnection 的调用,但仍然遇到同样的问题。
-
我唯一能建议的是为 org.springframework.transaction/jdbc 启用调试日志记录并确认事务和连接按预期访问。