HSLQDB + JPA2(使用 Hibernate)- 尝试 TRUNCATE SCHEMA 时应用程序卡住

Posted

技术标签:

【中文标题】HSLQDB + JPA2(使用 Hibernate)- 尝试 TRUNCATE SCHEMA 时应用程序卡住【英文标题】:HSLQDB + JPA2 (with Hibernate) - The app gets stuck when trying a TRUNCATE SCHEMA 【发布时间】:2020-10-22 17:26:19 【问题描述】:

我的应用实现了 Deltaspike v1.6 并使用了 Hibernate v4.3.8。它还使用 C3P0 v0.9.5.4 我的回归测试使用 TestNG v7 和 HSQLDB v2.4

情况: 我正在编写一些回归测试,使用 TestNG 进行测试本身,并将 HSQLDB 作为 on-mem db(实际上是在文件上)。我也使用 testng 的数据提供者

问题: 在数据提供者数据库中的每个场景之后都必须进行清理。因此,我尝试用这种方式在@AfterMethod方法中清理数据库:

public void resetDb() 
  em.getTransaction().begin();
   em.createNativeQuery("TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK").executeUpdate();
  em.getTransaction().commit();    

运行卡在 em.createNativeQuery... 行。我一步一步调试了这个并启用了所有类型的日志记录,它就停在那里。 这是 hibnernate 在控制台中记录的最后一件事:

Hibernate: 
    TRUNCATE SCHEMA PUBLIC RESTART IDENTITY 
    AND COMMIT NO CHECK

在我的日志文件中,我不断收到这些日志,我认为这是正常的吗?

2020-10-22 14:15:43 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner@7dbbf730: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@284e5784
2020-10-22 14:15:43 TRACE BasicResourcePool:196 - trace com.mchange.v2.resourcepool.BasicResourcePool@31f9f9b3 [managed: 50, unused: 47, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@12f66dd1)
2020-10-22 14:15:49 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1677180d -- Running DeadlockDetector[Exiting. No pending tasks.]
2020-10-22 14:15:59 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1677180d -- Running DeadlockDetector[Exiting. No pending tasks.]
...
2020-10-22 14:17:09 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner@7dbbf730: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@246b9ea4
2020-10-22 14:17:09 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner@7dbbf730: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@7f791d48
2020-10-22 14:17:09 TRACE ThreadPoolAsynchronousRunner:196 - com.mchange.v2.async.ThreadPoolAsynchronousRunner@7dbbf730: Adding task to queue -- com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@162d1ed8
2020-10-22 14:17:09 TRACE BasicResourcePool:196 - trace com.mchange.v2.resourcepool.BasicResourcePool@31f9f9b3 [managed: 50, unused: 48, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@12f66dd1)
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Testing PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@79c8cd43] on IDLE CHECK.
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Testing PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@6c2c47fe] on IDLE CHECK.
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Testing PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@723253a6] on IDLE CHECK.
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@723253a6] on IDLE CHECK has SUCCEEDED.
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@6c2c47fe] on IDLE CHECK has SUCCEEDED.
2020-10-22 14:17:09 DEBUG C3P0PooledConnectionPool:204 - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@79c8cd43] on IDLE CHECK has SUCCEEDED.

在旁注中,我尝试过像这样截断表格:

TRUNCATE TABLE [table_name] RESTART IDENTITY

它有效,但是当我尝试这个时:

TRUNCATE TABLE "+table+" RESTART IDENTITY AND COMMIT NO CHECK

它不起作用,它也会卡住。我认为“AND COMMIT”部分导致了某种僵局

【问题讨论】:

【参考方案1】:

TRUNCATE .. AND COMMIT 语句需要获取数据库的排他锁。似乎另一个会话处于活动状态并启动了未提交的事务。

【讨论】:

谢谢你的信息,我也认为是这样,但不确定。一个会话可能是应用程序本身,另一个可能是引擎需要的会话,由 TRUNCATE .. AND COMMIT 触发

以上是关于HSLQDB + JPA2(使用 Hibernate)- 尝试 TRUNCATE SCHEMA 时应用程序卡住的主要内容,如果未能解决你的问题,请参考以下文章

JPA2:不区分大小写,例如在任何地方匹配

无法使用 Hibernate 和 Maven 运行 JPA2

JPA2 Criteria API 更可取的真实示例都有哪些?

使用 JPA2 Criteria API 选择 MAX 时间戳

6.Spring+Struts+Hibernat注解方式整合

使用 JPA2 时如何对 EJB 进行单元测试?