[kansaipm] DBIモジュールのfinishメソッド

mishima at momo.so-net.ne.jp mishima at momo.so-net.ne.jp
Sat Mar 25 05:53:57 CST 2000


三嶋@奈良 ActivePerl屋です。

気になっていた DBIモジュールの finish メソッドについて DBI (1.06) の 
POD で調べてみました。私の理解した範囲でまとめてみます。より詳しい方が
おられましたら、フォローをお願いします。

結論:
 山本さんのプレゼンにあった例(全レコードを取り終えるまで、while ルー
プでfetchrow_arrayref を繰り返す)の場合、ドライバがちゃんとしていたら、
finish は不要です。


データベースハンドルの disconnect メソッドの説明から抜粋

        If you disconnect from a database while you still have
        active statement handles you will get a warning. The
        statement handles should either be cleared (destroyed)
        before disconnecting or the finish method called on each
        one.

	アクティブなステートメントハンドルを残したまま disconnect 
	したら、警告が出る。

ステートメントハンドルの finish メソッドの説明から抜粋

        Indicates that no more data will be fetched from this
        statement handle before it is either executed again or
        destroyed. *It is rarely needed* but can sometimes be
        helpful in order to allow the server to free up resources
        currently being held (such as sort buffers).

	*これが必要になることはまれである。*

        When all the data has been fetched from a select statement
        the driver should automatically call finish for you. So you
        should not normally need to call it explicitly.

	SELEC文ですべてのデータを取り終えたら、ドライバは自動的に
	finish するはずなので、普通は明示的に finish しなくていい。


注意すべきケースは、以下の2つくらいだと思います。

・$dbh->prepare, $sth->execute, の後 $sth->fetchrow_ほげほげ で一部の
 レコードのみ取得する場合。
・$dbh->prepare で用意したハンドルを使い $dbh->selectrow_ほげほげ($sth)
 で先頭行 だけ取り出す場合。

但し、ステートメントハンドルの変数($sth)自体がスコープを終えて無くなっ
ているような場合には finish は不要(しようと思ってもオブジェクトが無い
のでできない ^^;)です。
$dbh->selectrow_ほげほげ("SQL文") で、SQL文字列を直接指定する場合にも、
ステートメントハンドルはおもてに出てこないため、finish は不要です。

--
$p='Perl'; $_='Masahiro Mishima'; sub _{pack'c*',$x=110+ at _*5,$x+1}
tr/oma/fa_/;s/./chr(ord($&)+2)/ge;@x=(sort(grep{!$_{$_}++}split//),
$p=~/(.(..).)/);$x[7]=~s/^/_/e;$x[8].=_ 1;for(@w=(47,1639,8,31259))
{s/\d/$x[$&+1]/g} print ucfirst "@w.\n";



More information about the Kansai-pm mailing list