2

我有一个使用Hibernateand的 Web 应用程序PostgreSQL

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

@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 实体期间有一个锁。

从这个链接http://wiki.postgresql.org/wiki/Lock_Monitoring我试图弄清楚什么是锁定的。

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

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)

因为这个输出很难阅读,这里是blocked_statement:

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

这是blocking_statement:

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

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

4

0 回答 0