简体   繁体   English

如何在DBIx:Class中锁定表?

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

I am writing a small application with 我正在写一个小应用程序

  • Mojolicious Mojolicious
  • DBIx::Class DBIx :: Class
  • Hypnotoad which is a pre-forking web server. Hypnotoad,这是一个预分支的Web服务器。
  • MySQL 的MySQL

In my application I need to do the following; 在我的应用程序中,我需要执行以下操作;

  1. Do some complex processing ( takes a minute of so to complete ) 做一些复杂的处理(需要一分钟左右的时间来完成)
  2. insert resulting data from above processing into tables 将上述处理后的结果数据插入表中
  3. obtain the last auto increment of some tables, do some more processing. 获取某些表的最后一个自动增量,进行更多处理。
  4. use the values from (3) as part of an insert into another table ( a junction table ) 将(3)中的值用作插入另一个表(联结表)的一部分

Here is some sample code starting at step 2 这是从第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);

Now due to having multiple threads going a once the values obtained from step 3 may not be correct ( for the current 'device' ). 现在由于一次执行多个线程,从步骤3获得的值可能不正确(对于当前“设备”)。

I'm trying to find a way around this issue. 我正在尝试找到解决此问题的方法。 The only thing I can think of at the moment is putting a lock on the database tables before step 2) and unlocking after step 4). 我目前唯一能想到的就是在步骤2)之前对数据库表进行锁定,并在步骤4)之后进行解锁。

How can I do this in DBIx::Class and is this likely to resolve my issue? 如何在DBIx :: Class中执行此操作,这是否可能解决我的问题?

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM