【问题标题】:How to set transaction isolation level with DBUnit如何使用 DBUnit 设置事务隔离级别
【发布时间】:2014-08-27 11:29:46
【问题描述】:

我在下面有一个简单的事务隔离测试,如果我针对我的 SQLServer DB 运行它会按预期工作,但是当我使用 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();
  //  }
  //});
}

注意:我这里有一个人工冲洗来模拟现实中可能(和确实)发生的事情。对“人”运行进一步的查询可能会导致刷新。

SQL Server 按预期执行。在persistence.xml(“hibernate.connection.isolation”设置为“1”)上设置READ_COMMIT,或者在注释掉的代码中为该连接动态更改它,测试完成。但是,对 DBUnit 运行相同的测试在所有情况下都会挂起。这是一个 DBUnit 错误,还是有我不知道的设置?

【问题讨论】:

    标签: hibernate jpa transactions guice dbunit


    【解决方案1】:

    问题实际上与 HSQLDB (2.3.2) 有关。它不支持 READ_UNCOMMITTED 事务隔离级别。 This post 此处提供了更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多