[英]How can I lock a table in DBIx:Class?
我正在寫一個小應用程序
在我的應用程序中,我需要執行以下操作;
這是從第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方法時如何避免競爭條件?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.