Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PostgreSQL和Hibernate的奇怪锁_Java_Sql_Hibernate_Jdbc_Orm - Fatal编程技术网

Java PostgreSQL和Hibernate的奇怪锁

Java PostgreSQL和Hibernate的奇怪锁,java,sql,hibernate,jdbc,orm,Java,Sql,Hibernate,Jdbc,Orm,我有一个使用Hibernate和PostgreSQL的web应用程序 在此应用程序中,我有几个实体,其中之一是: @Entity public class UniverseTime extends Model { @Required public Date realDate; // Day of month @Required public Long diesse; // Month of year @Required pub

我有一个使用
Hibernate
PostgreSQL
的web应用程序

在此应用程序中,我有几个实体,其中之一是:

@Entity
public class UniverseTime extends Model {

    @Required
    public Date realDate;

    // Day of month
    @Required
    public Long diesse;

    // Month of year
    @Required
    public Long cycle;

    // year
    @Required
    public Long soleme;

    //next current cycle
    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Cycle currentCycle;

    // Tell if this universe time is the global one
    @Required
    public Boolean global;
}
当我的web应用程序执行时,我在保存UniverseTime实体的过程中有一个锁

从这个链接中,我试图找出什么是锁定

下面是我使用第一个命令得到的结果:

db=# SELECT relation::regclass, * FROM pg_locks WHERE NOT granted;                                                                                                                                                                                                    relation |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |   mode    | granted | fastpath 
----------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+-----------+---------+----------
          | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock | f       | f
(1 row)

db=# SELECT relation::regclass, * FROM pg_locks;
      relation      |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |       mode       | granted | fastpath 
--------------------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+------------------+---------+----------
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 hibernate_sequence | relation      |    16393 |    17294 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 4/16298    |               |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 14/1479    |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | t
 pg_locks           | relation      |    16393 |    11090 |      |       |            |               |         |       |          | 2/9902             | 5206 | AccessShareLock  | t       | t
                    | virtualxid    |          |          |      |       | 2/9902     |               |         |       |          | 2/9902             | 5206 | ExclusiveLock    | t       | t
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | f
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock        | f       | f
                    | transactionid |          |          |      |       |            |          2773 |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
 universetime       | tuple         |    16393 |    22883 |    0 |    13 |            |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
(25 rows)
db=# SELECT bl.pid                 AS blocked_pid,                                                                                                                                                                                                                            a.usename              AS blocked_user,                                                                                                                                                                                                                                        ka.query               AS blocking_statement,                                                                                                                                                                                                                                  now() - ka.query_start AS blocking_duration,                                                                                                                                                                                                                                   kl.pid                 AS blocking_pid,                                                                                                                                                                                                                                        ka.usename             AS blocking_user,                                                                                                                                                                                                                                       a.query                AS blocked_statement,                                                                                                                                                                                                                                   now() - a.query_start  AS blocked_duration                                                                                                                                                                                                                              FROM  pg_catalog.pg_locks         bl                                                                                                                                                                                                                                            JOIN pg_catalog.pg_stat_activity a  ON a.pid = bl.pid                                                                                                                                                                                                                          JOIN pg_catalog.pg_locks         kl ON kl.transactionid = bl.transactionid AND kl.pid != bl.pid                                                                                                                                                                                JOIN pg_catalog.pg_stat_activity ka ON ka.pid = kl.pid                                                                                                                                                                                                                        WHERE NOT bl.granted;
 blocked_pid | blocked_user |                                                                                                                                                        blocking_statement                                                                                                                                                        | blocking_duration | blocking_pid | blocking_user |                                               blocked_statement                                                | blocked_duration 
-------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------+--------------+---------------+----------------------------------------------------------------------------------------------------------------+------------------
        6901 | postgres     | select universeti0_.id as id30_0_, universeti0_.currentCycle_id as currentC7_30_0_, universeti0_.cycle as cycle30_0_, universeti0_.diesse as diesse30_0_, universeti0_.global as global30_0_, universeti0_.realDate as realDate30_0_, universeti0_.soleme as soleme30_0_ from UniverseTime universeti0_ where universeti0_.id=$1 | 00:07:36.576371   |         6880 | postgres      | update UniverseTime set currentCycle_id=$1, cycle=$2, diesse=$3, global=$4, realDate=$5, soleme=$6 where id=$7 | 00:07:31.574721
(1 row)
这是我最后一个命令得到的结果:

db=# SELECT relation::regclass, * FROM pg_locks WHERE NOT granted;                                                                                                                                                                                                    relation |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |   mode    | granted | fastpath 
----------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+-----------+---------+----------
          | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock | f       | f
(1 row)

db=# SELECT relation::regclass, * FROM pg_locks;
      relation      |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |       mode       | granted | fastpath 
--------------------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+------------------+---------+----------
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 hibernate_sequence | relation      |    16393 |    17294 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 4/16298    |               |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 14/1479    |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | t
 pg_locks           | relation      |    16393 |    11090 |      |       |            |               |         |       |          | 2/9902             | 5206 | AccessShareLock  | t       | t
                    | virtualxid    |          |          |      |       | 2/9902     |               |         |       |          | 2/9902             | 5206 | ExclusiveLock    | t       | t
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | f
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock        | f       | f
                    | transactionid |          |          |      |       |            |          2773 |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
 universetime       | tuple         |    16393 |    22883 |    0 |    13 |            |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
(25 rows)
db=# SELECT bl.pid                 AS blocked_pid,                                                                                                                                                                                                                            a.usename              AS blocked_user,                                                                                                                                                                                                                                        ka.query               AS blocking_statement,                                                                                                                                                                                                                                  now() - ka.query_start AS blocking_duration,                                                                                                                                                                                                                                   kl.pid                 AS blocking_pid,                                                                                                                                                                                                                                        ka.usename             AS blocking_user,                                                                                                                                                                                                                                       a.query                AS blocked_statement,                                                                                                                                                                                                                                   now() - a.query_start  AS blocked_duration                                                                                                                                                                                                                              FROM  pg_catalog.pg_locks         bl                                                                                                                                                                                                                                            JOIN pg_catalog.pg_stat_activity a  ON a.pid = bl.pid                                                                                                                                                                                                                          JOIN pg_catalog.pg_locks         kl ON kl.transactionid = bl.transactionid AND kl.pid != bl.pid                                                                                                                                                                                JOIN pg_catalog.pg_stat_activity ka ON ka.pid = kl.pid                                                                                                                                                                                                                        WHERE NOT bl.granted;
 blocked_pid | blocked_user |                                                                                                                                                        blocking_statement                                                                                                                                                        | blocking_duration | blocking_pid | blocking_user |                                               blocked_statement                                                | blocked_duration 
-------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------+--------------+---------------+----------------------------------------------------------------------------------------------------------------+------------------
        6901 | postgres     | select universeti0_.id as id30_0_, universeti0_.currentCycle_id as currentC7_30_0_, universeti0_.cycle as cycle30_0_, universeti0_.diesse as diesse30_0_, universeti0_.global as global30_0_, universeti0_.realDate as realDate30_0_, universeti0_.soleme as soleme30_0_ from UniverseTime universeti0_ where universeti0_.id=$1 | 00:07:36.576371   |         6880 | postgres      | update UniverseTime set currentCycle_id=$1, cycle=$2, diesse=$3, global=$4, realDate=$5, soleme=$6 where id=$7 | 00:07:31.574721
(1 row)
由于此输出很难读取,以下是阻塞的_语句:

select universeti0_.id as id30_0_, universeti0_.currentCycle_id as currentC7_30_0_, universeti0_.cycle as cycle30_0_, universeti0_.diesse as diesse30_0_, universeti0_.global as global30_0_, universeti0_.realDate as realDate30_0_, universeti0_.soleme as soleme30_0_ from UniverseTime universeti0_ where universeti0_.id=$1
下面是一个声明:

update UniverseTime set currentCycle_id=$1, cycle=$2, diesse=$3, global=$4, realDate=$5, soleme=$6 where id=$7

我不确定,
UPDATE
查询似乎阻止了
SELECT
查询,但是为什么
UPDATE
查询被锁定而不执行呢?这是一个简单的
更新
,它应该快速执行并释放锁,但它没有,为什么?

这看起来很奇怪,我认为您的分析中缺少了一些东西。默认情况下,读卡器从不阻止写卡器,写卡器从不阻止Postgres中的读卡器。
SELECT
仅当它是
SELECT时才能被阻止。。。对于更新
,另一个事务也会更新该表,或者如果该表锁定在
访问独占
模式。您建议我如何查找此问题的根源?