The other day i was struggeling with some strange behaviour, of two concurrently running scripts, that i want to share here. I am omitting most of the error handling for brevity.
script1:
$h = dba_open("file_a", 'r', 'gdbm');
while (true)
{
dba_fetch("somekey", $h);
sleep(1);
}
dba_close($h);
______________________
script2:
// while script1 is running until eternity...
$ok = rename("file_a", "file_b");
if (! $ok)
echo "Could not move\n";
else
echo "I like to move it, move it!\n";
// GOAL: Manipulate the file only if it is not opened for reading...
RESULT:
I like to move it, move it!
So you can very well rename a file, while it is opened. The lookup in script1 is still working. Manipulating the file in script2 will fail, as there is still an gdbm lock on the file from script1.
dba_open
(PHP 4, PHP 5)
dba_open — データベースをオープンする
説明
dba_open() は、handler を用いて
mode を指定し、path
にデータベースインスタンスを生成します。
パラメータ
-
path -
通常のファイルシステムのパス。
-
mode -
既存のデータベースへの読み込みアクセスには "r"、読込み/書き込み アクセスには "w" を指定します。 読み込み/書き込みアクセスおよび存在しない場合にデータベースの作成を するには "c" を、そして作成、削除、読込み/書き込みアクセス用には "n" を指定します。データベースは BTree モードで作られます。 その他のモード (Hash や Queue) には対応していません。
さらに、次の文字でデータベースのロック方法を指定することができます。 .lck でデータベースをロックする場合には "l"、 データベースファイル自体をロックする場合は "d" を使用してください。 アプリケーション全体で統一した方法を用いることが重要です。
アクセスのテストを行う際にロックのために待ちたくない場合、"t" を 3 番目の文字に追加することができます。明らかにデータベースのロックが 不要な場合には、"l" や "d" の代わりに "-" を使用してロックを 行わないことができます。"d"、"l" または "-" のどれも指定されない場合、 "d"が指定されたものとしてデータベースファイルをロックします。
注意:
ひとつのデータベースファイルに同時に書き込めるのは、ひとつだけです。 dba を Web サーバーで使用している際に複数のリクエストが書き込み処理を 行う必要がある場合、他の書き込みが終わってからでないと次の書き込みを 行うことができません。また、書き込み中に読み込むことはできません。 dba 拡張モジュールは、このようなことを防止するためにロックを使用します。 以下の表を参照してください:
DBA のロック処理 既にオープンしているデータベース mode= "rl"mode= "rlt"mode= "wl"mode= "wlt"mode= "rd"mode= "rdt"mode= "wd"mode= "wdt"not open ok ok ok ok ok ok ok ok mode= "rl"ok ok wait false illegal illegal illegal illegal mode= "wl"wait false wait false illegal illegal illegal illegal mode= "rd"illegal illegal illegal illegal ok ok wait false mode= "wd"illegal illegal illegal illegal wait false wait false - ok: 2 番目のコールは成功します。
- wait: 2 番目のコールは、最初のデータベースで dba_close() がコールされるまで待ちます。
- false: 2 番目のコールは、false を返します。
- illegal:
modeパラメータで "l" および "d" を同時に使用することはできません。
-
handler -
pathにアクセスする際に使用する ハンドラの名前。 dba_open() に指定したすべてのオプションが 渡され、その機能を用いることができます。
返り値
成功した場合に正のハンドル、失敗した場合に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.3.0 | ネットワーク接続されたデータベースファイルをオープンすることができます。 しかし、(http や ftp のような)ソケット接続が使用された場合、 リソース自体ではなくこの接続がロックされます。このような場合、 このリソースに関してのロック処理は単に無視されることになり、 他の解決策を見付ける必要があることに留意する必要があります。 |
| 4.3.0 |
ロック処理と mode 修正子
"l", "d", "-",
"t"
が追加されました。
以前のバージョンの PHP では、GDBM 以外のデータベースハンドラで
同時データベースアクセスに対する保護を行うためには、セマフォを
使用する必要がありました。
System V セマフォサポート を参照ください。
|
| 4.3.5 以前 | オープンモード "c" はいくつかの内部ハンドラでは正常に動作せず、 既存のデータベースにデータを追加するのではなく、データベースを 切り捨ててしまっていました。また、dbm と ndbm は典型的な設定で モード "c" の処理が正常に動作しません(これは修正できません)。 |
Apache doesn't support Berkeley DB Btree, so you can't manipulate use db4 as the type of database if you want to do DBM authentication with Apache.
gdbm seemed to work fine though, even though it supposedly using Btree instead of hash. It makes you wonder why Apache would use hash for one dbmtype versus btree for another.
So since Apache and PHP don't have options to choose the method for the Berkeley DBs, you are out of luck.
Note the “c” create flag does not work if MySQL was built with the “cdb” DBA handler compile option which is common for many distros. By definition the cdb DBA handler is optimized for reading/writing and “no updates are allowed.”
<?php
$dbh = dba_open( "./data2/productz", "c", "cdb") or die( "Couldn't open Database" );
?>
instead use
<?php
$dbh = dba_open( "./data2/productz", "n", "cdb" ) or die( "Couldnt open Database" );
?>
generates this error message in the /var/log/apache2/error.log:
[Sun Sep 06 04:18:15 2009] [error] [client 192.168.1.125] PHP Warning: dba_open(./data2/productz,c) [<a href='function.dba-open'>function.dba-open</a>]: Driver initialization failed for handler: cdb: Update operations are not supported in /var/www/projects/testcdb-c.php on line 43
see user contributed comment under dba_handlers() to see which DBA handlers are supported by your build of MySQL and note about using “cdb” compiled DBA systems:
also see user contributed comment under dba_replace() about incompatibilities with cdb DBA handler compiled MySQL systems.
As of GDBM version 1.8.3, GDBM's underlying open call uses non-blocking calls to flock() on systems that have flock(). As a result, calls with "rd" or "wd" locking modes will return error ("Can't be reader" or "Can't be writer") instead of waiting. Use "rl" or "wl" instead, to make PHP do its own locking external to GDBM.
Here's a simple example to use the dba_open function
<?php
$id = dba_open("/tmp/test.db", "n", "gdbm");
if (!$id) {
echo "dba_open failed\n";
exit;
}
dba_replace("key", "This is an example!", $id);
if (dba_exists("key", $id)) {
echo dba_fetch("key", $id);
dba_delete("key", $id);
}
dba_close($id);
?>
Windows does not support locking the database. You may use $_ENV to determine the OS:
$locking = (stripos($_ENV['OS'],'windows') === false ? 'd' : 'l');
If you get some strange errors like
dba_open(): myDbFilename.db : Permission denied
than you are propably using PHP on a Windoze machine. You have to make sure that the following conditions are met:
1) Use an absolute path to your db file. Relative paths will cause problems with locking
2) Specify a locking mode - that's the second character of the mode-argument, or else opening a dba-file will cause several notices/warnings etc.
And a final, general note:
3) Always use the english PHP doc on this site - the translations are often old as hell and miss important informations
HTH, Nils.
