簡體   English   中英

如何在DBIx:Class中鎖定表?

[英]How can I lock a table in DBIx:Class?

我正在寫一個小應用程序

  • Mojolicious
  • DBIx :: Class
  • Hypnotoad,這是一個預分支的Web服務器。
  • 的MySQL

在我的應用程序中,我需要執行以下操作;

  1. 做一些復雜的處理(需要一分鍾左右的時間來完成)
  2. 將上述處理后的結果數據插入表中
  3. 獲取某些表的最后一個自動增量,進行更多處理。
  4. 將(3)中的值用作插入另一個表(聯結表)的一部分

這是從第2步開始的一些示例代碼

#step 2
my $device = $device_rs->create(
 {
  devicename      => $deviceName,
  objects         => \@objects
  object_groups   => \@objectgroups,

}
);

#step 3
my $lastogid  = $db->resultset('ObjectGroup')->get_column('objectgroupid')->max;
my $lastobid  = $db->resultset('Object')->get_column('objectid')->max;

my $obgcount = scalar(@objectgroups);
my $objcount = scalar(@objects);

my $ogoffset = $lastogid - $obgcount;
my $oboffset = $lastobid - $objcount;

#now increment the object/group ids by the offset which will be inserted into the many-  many table
foreach my $hash (@childobjects) {
 $hash->{'objectgroup_objectgroupid'} += $ogoffset;
 $hash->{'object_objectid'}           += $oboffset;
}

#step 4  - populate the junction table
$db->resultset('ObjectGroupHasObjects’)->populate(\@childobjects);

現在由於一次執行多個線程,從步驟3獲得的值可能不正確(對於當前“設備”)。

我正在嘗試找到解決此問題的方法。 我目前唯一能想到的就是在步驟2)之前對數據庫表進行鎖定,並在步驟4)之后進行解鎖。

如何在DBIx :: Class中執行此操作,這是否可能解決我的問題?

謝謝。

就像是

$schema->dbh_do("LOCK TABLES names");
...
...
$schema->dbh_do("UNLOCK TABLES");

資料來源: http//www.perlmonks.org/? node_id = 854538

另請參見: 使用DBIx :: Class :: ResultSet的find_or_create方法時如何避免競爭條件?

SQLHackers :: SELECT#SELECT _..._ FOR_UPDATE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM