Hibernate在alter table命令上被锁定

时间:2018-01-12 09:38:55

标签: java postgresql hibernate playframework hibernate-search

我有一个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=$1

我没有在我的代码中明确触发此SELECT。它必须是另一个自动发生的Hibernate操作。此外,此代码片段未明确并行。

1 个答案:

答案 0 :(得分:0)

非常感谢你的评论。他们帮我解决了这个问题。

问题是:我正在使用hibernate.cfg.xml(从SessionFactory获得的会话)和persistance.xml(从EntityManager获得的另一个会话)。这是将两个应用程序集成到一个中的结果,我认为这会导致创建Hibernate会话。我将必要的配置从hibernate.cfg.xml集成到persistence.xml(必须更改格式)并且有所帮助。