我有一个简单的事务隔离测试,如果我针对我的SQLServer数据库运行,它会按预期工作,但是当我使用DBUnit运行时会挂起。
@Test
@com.google.inject.persist.Transactional
public void testFreeze() {
try {
EntityManager applicationManagedEntityManager = entityMangerFactoryProvider.get().createEntityManager();
EntityManager guiceManagedEntityManager = entityManager.get();
Person p = new Person("Fred");
guiceManagedEntityManager.persist(p);//But not committed
guiceManagedEntityManager.flush();//* Note
runQuery(guiceManagedEntityManager, "select count(*) from Person");//Works just fine
runQuery(applicationManagedEntityManager, "select count(*) from Person");//Hangs when running this query using DBUnit
applicationManagedEntityManager.close();
}
catch (Exception err) {
err.printStackTrace();
}
}
private void runQuery(final EntityManager entityManager, final String query) {
entityManager.createQuery(query).getSingleResult();
//Or try running it as below
//Session session = entityManager.unwrap(Session.class);
//session.doWork(new Work() {
// @Override
// public void execute(Connection connection) throws SQLException {
// connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
// entityManager.unwrap(Session.class).createSQLQuery(query).uniqueResult();
// }
//});
}
注意:我在这里有一个人工冲洗来模拟现实中可能发生的事情。对“person”运行进一步的查询可能会导致刷新。
SQL Server按预期执行。在persistence.xml上设置READ_COMMIT(“hibernate.connection.isolation”设置为“1”)或者在注释掉的代码中动态更改该连接,测试完成。但是,在所有情况下,对DBUnit运行相同的测试都会挂起。这是一个DBUnit错误,还是有一个我不知道的设置?