Hibernate 被锁定在 alter table 命令上
Hibernate gets locked on alter table command
我有一个 Play Framework 应用程序,它使用 Hibernate (5.2.12.Final
) 连接到 Postgresql(库版本:42.1.4
,Postgresql 9.6
)。我在我的应用程序中添加了以下代码(我想开始使用 Hibernate Search,所以我需要会话对象):
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.flush();
代码挂在第二行,应用程序日志显示:
...
Hibernate: alter table creative_works_inner_tags drop constraint FK1m3t9vv4yl0o36k9nv0bjko87
Hibernate: alter table creative_works_inner_tags drop constraint FKcodcu7qrti27rqnv54bg9o0ma
Hibernate: alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
所以看起来 Hibernate 在模式更新中间的某个地方挂起,这是由会话启动触发的。我还在 Postresql 中看到一个锁:
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
pid | usename | blocked_by | blocked_query
-----+---------+------------+------------------------------------------------------------------------
18804| pbl | {18499} | alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
我不知道该怎么办。它也发生在以前的库版本中。
更新
阻止 ALTER TABLE 命令的 pid 来自同一个应用程序:
datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | state | backend_xid | backend_xmin | query
-------+---------+-------+----------+---------+------------------------+-------------+-----------------+-------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+---------------------+-------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16388 | pbl | 18499 | 16387 | pbl | PostgreSQL JDBC Driver | 127.0.0.1 | | 51192 | 2018-01-12 10:23:11.62866+01 | 2018-01-12 10:24:11.363172+01 | 2018-01-12 10:24:11.574648+01 | 2018-01-12 10:24:11.574693+01 | | | idle in transaction | | 373556 | select children0_.parent_code as parent_c3_179_0_, children0_.code as code1_179_0_, children0_.code as code1_179_1_, children0_.ord as ord2_179_1_, children0_.parent_code as parent_c3_179_1_ from record_types children0_ where children0_.parent_code=
我没有在我的代码中明确触发此 SELECT。它必须是另一个自动发生的 Hibernate 操作。此外,此代码片段不是明确并行的。
非常感谢您的评论。他们帮我解决了问题。
问题是:我同时使用了 hibernate.cfg.xml
(从 SessionFactory
获得的会话)和 persistance.xml
(从 EntityManager
获得的另一个会话)。这是将两个应用程序集成到一个应用程序中的结果,我认为这导致创建了两个 Hibernate 会话。我将 hibernate.cfg.xml
中的必要配置集成到 persistence.xml
中(必须更改格式)并且它有所帮助。
我有一个 Play Framework 应用程序,它使用 Hibernate (5.2.12.Final
) 连接到 Postgresql(库版本:42.1.4
,Postgresql 9.6
)。我在我的应用程序中添加了以下代码(我想开始使用 Hibernate Search,所以我需要会话对象):
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.flush();
代码挂在第二行,应用程序日志显示:
...
Hibernate: alter table creative_works_inner_tags drop constraint FK1m3t9vv4yl0o36k9nv0bjko87
Hibernate: alter table creative_works_inner_tags drop constraint FKcodcu7qrti27rqnv54bg9o0ma
Hibernate: alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
所以看起来 Hibernate 在模式更新中间的某个地方挂起,这是由会话启动触发的。我还在 Postresql 中看到一个锁:
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
pid | usename | blocked_by | blocked_query
-----+---------+------------+------------------------------------------------------------------------
18804| pbl | {18499} | alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
我不知道该怎么办。它也发生在以前的库版本中。
更新
阻止 ALTER TABLE 命令的 pid 来自同一个应用程序:
datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | state | backend_xid | backend_xmin | query
-------+---------+-------+----------+---------+------------------------+-------------+-----------------+-------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+---------------------+-------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16388 | pbl | 18499 | 16387 | pbl | PostgreSQL JDBC Driver | 127.0.0.1 | | 51192 | 2018-01-12 10:23:11.62866+01 | 2018-01-12 10:24:11.363172+01 | 2018-01-12 10:24:11.574648+01 | 2018-01-12 10:24:11.574693+01 | | | idle in transaction | | 373556 | select children0_.parent_code as parent_c3_179_0_, children0_.code as code1_179_0_, children0_.code as code1_179_1_, children0_.ord as ord2_179_1_, children0_.parent_code as parent_c3_179_1_ from record_types children0_ where children0_.parent_code=
我没有在我的代码中明确触发此 SELECT。它必须是另一个自动发生的 Hibernate 操作。此外,此代码片段不是明确并行的。
非常感谢您的评论。他们帮我解决了问题。
问题是:我同时使用了 hibernate.cfg.xml
(从 SessionFactory
获得的会话)和 persistance.xml
(从 EntityManager
获得的另一个会话)。这是将两个应用程序集成到一个应用程序中的结果,我认为这导致创建了两个 Hibernate 会话。我将 hibernate.cfg.xml
中的必要配置集成到 persistence.xml
中(必须更改格式)并且它有所帮助。