1

我有一个更新查询,有时会卡住并锁定整个数据库。每当发生这种情况时,我必须要求 DBA 手动终止查询。db 中查询的状态是“sleeping”,如下图所示。

https://imgur.com/a/fFN6rlJ

我正在使用 MS SQL 2012 Server 和 hibernate-core 5.2.6-Final。

我试过实现查询超时,但没有成功。即使超时,查询也碰巧再次卡住。我在下面粘贴了执行更新的代码片段:

try {
    if (!entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().begin();
    }
    String query = "UPDATE FooBarTable i SET i.foo = :foo"
                + "WHERE i.id.bar = :bar AND i.id.foobar = :foobar";
    entityManager.createQuery(query)
            .setParameter("foo", foo)
            .setParameter("bar", bar)
            .setParameter("foobar", foobar)
            .setHint("org.hibernate.timeout", "15")
            .executeUpdate();
    entityManager.getTransaction().commit();

} catch (Exception e) {
    entityManager.getTransaction().rollback();
    throw new Exception(e);
}

我还尝试插入一个“finally”块并调用entityManager.close(). 由于插入了finally块,错误还没有发生。

但我不相信它会永远解决。如何确保查询不会卡住并锁定数据库?

4

0 回答 0