我有一個使用Perl線程模塊創建一些線程的腳本,並且每個線程都創建了一個到SQLite dbfile的數據庫連接。在線程內部有一個從文件句柄讀取的while循環,用於向zcat處理的文件打開管道輸入。我試圖爲每個線程正在處理的每個X行提交事務。當我嘗試在while循環之外使用$dbh->begin_work
時,單個線程會阻止其餘的線程。當我把$dbh->begin_work
放在while循環內時,它們不會互相阻塞。後者基本上是自動提交每個$dbh->do("insert...")
聲明。爲什麼$dbh->begin_work
似乎不能在while循環之外工作?Perl DBD :: SQLite - while循環塊線程之外的begin_work?
$dbh->begin_work; # This blocks the while loops in other threads
while ($row = <$gz>) {
$dbh->begin_work; # This does not block
@values = split('\|', $row);
@node_ids = split ',', $values[21]
for $node_id (@node_ids) {
$dbh->do("insert ....");
}
$dbh->commit; # This does not block
}
$dbh->commit; # This blocks the while loops in other threads
我正在使用DBD:SQLite版本1.29。我試過使用* sqlite_use_immediate_transaction *,但直到版本1.38_01才似乎是必需的。
請提供一個問題的實際演示。另外,確保你在使用它的線程中創建dbh。如果這沒有幫助,它可能是SQLite的限制。 – ikegami
你檢查過線程上的SQLite文檔嗎? https://www.sqlite.org/threadsafe.html – oalders