DBM::Deep:事务问题
我从未做过交易(就编程而言),因此我不知道我的脚本是否有问题或其他问题:
#!/usr/bin/env perl
use warnings;
use 5.012;
use DBM::Deep;
my $db = DBM::Deep->new( 'foo.db' );
my $trans = $db->supports( 'transactions' );
say 'Does ', $trans ? '' : 'NOT ', 'support transactions';
$db->{key} = 'value';
$db->begin_work;
$db->{key1} = 'value2';
$db->rollback;
$db->{key1} = 'value1';
$db->commit;
输出:
# Does support transactions
# DBM::Deep: Cannot allocate transaction ID at ./perl1.pl line 12
部分评论:
my $db = DBM::Deep->new( file => 'my.db', num_txns => 1 );
$db->{key} = 'value';
$db->begin_work;
$db->{key1} = 'value2';
$db->rollback;
$db->begin_work;
$db->{key1} = 'value1';
$db->commit;
I've never done transactions (in terms of programming), therefore I don't know if there is something wrong with my script or something else:
#!/usr/bin/env perl
use warnings;
use 5.012;
use DBM::Deep;
my $db = DBM::Deep->new( 'foo.db' );
my $trans = $db->supports( 'transactions' );
say 'Does ', $trans ? '' : 'NOT ', 'support transactions';
$db->{key} = 'value';
$db->begin_work;
$db->{key1} = 'value2';
$db->rollback;
$db->{key1} = 'value1';
$db->commit;
Output:
# Does support transactions
# DBM::Deep: Cannot allocate transaction ID at ./perl1.pl line 12
Part of comment:
my $db = DBM::Deep->new( file => 'my.db', num_txns => 1 );
$db->{key} = 'value';
$db->begin_work;
$db->{key1} = 'value2';
$db->rollback;
$db->begin_work;
$db->{key1} = 'value1';
$db->commit;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉花了这么长时间才回答这个问题 - 我几天前才发现它。 (我是 DBM::Deep 的维护者。)
问题是 num_txns 仅在创建文件时设置。 (这是因为 DBM 文件在磁盘上的布局方式不同。)创建 DBM 文件后,将从文件中读取 num_txns 值,并在调用
时忽略该值。新的()。因此,一旦您更改了调用以指定
num_txns
,除非您还使用了新的 DBM 文件,否则它不会有任何帮助。虽然我无法在不显着改变 DBM 文件结构工作方式的情况下改变这种行为(这可能是一个好主意,但要做的事情很大),但您应该已经收到警告,并且应该有更好的文档。我已经打开 https://github.com/robkinyon/dbm-deep/issues/12< /a> 跟踪此问题及其修复。
Sorry about taking so long to answer this question - I only just found it a few days ago. (I'm the maintainer of DBM::Deep.)
The issue is that
num_txns
is only set when the file is created. (This is because of how the DBM file is laid out on disk.) Once you've created a DBM file, then thenum_txns
value is read from the file and ignored in the call tonew()
. So, once you changed your invocation to specifynum_txns
, it wouldn't help unless you also used a new DBM file.While I cannot change this behavior without significantly changing how the DBM file structure works (which may be a good idea, but is a huge thing to do), you should have been warned and there should have been better documentation. I have opened https://github.com/robkinyon/dbm-deep/issues/12 to track this problem and the fix(es) for it.
根据 文档 < code>rollback 命令结束事务。
因此,您需要在回滚后启动新的事务。
或者你可以做类似
或 一些黑魔法的事情,你可以保持面向对象的风格
Accoring to the documentation the
rollback
command ends the transaction.Therefore you need to start a new transaction after a rollback.
or you could do something like
or with a little black magic, you can keep the OO style