- 2009/12/30 Wed
-
以前書いたモジュールの AutoCommit を手当たり次第に切っていったら自動テストの出力にIssuing rollback() for database handle being DESTROY'd without explicit disconnect().
とかいうのが混じるようになって ちょと調べたのでメモ。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; $dbh->do('update foo set bar=1'); $dbh->commit; $dbh->disconnect;
まず普通に。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; $dbh->do('update foo set bar=1'); $dbh->commit; #$dbh->disconnect;
disconnectを省略しても変化なし。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; $dbh->do('update foo set bar=1'); #$dbh->commit; $dbh->disconnect;
commitを省略。警告はでない。当然、コミットもされない。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; $dbh->do('update foo set bar=1'); #$dbh->commit; #$dbh->disconnect;
両方省略したところで件の警告。
モジュールを眺めたら、select の後の条件分岐で disconnect せずに抜けてる箇所があった。
disconnect の漏れはともかくとして、select でも commit/rollback が必要なものなのか。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; my $sth = $dbh->prepare('select * from foo'); $sth->execute; undef $sth; #$dbh->commit; #$dbh->disconnect;
これだと警告。use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db'); $dbh->{AutoCommit} = 0; my $sth = $dbh->prepare('select * from foo'); $sth->execute; undef $sth; $dbh->commit; #$dbh->disconnect;
警告でない。rollback でも同様。