ASi

の PESSIMISTIC_READ

http://k-webs.jp/javaindex/post-1881# の記事、OPTIMISTIC lock の説明は分かり易く良いのだが、PESSIMISTIC_READ の説明が間違っている。

DBMS の read lock は他の transaction による書き込みを待たせるロックであって、読み込みは妨げない。

JPAJavadoc にも下記とある

LockModeType.PESSIMISTIC_READ can be used to query data using repeatable-read semantics without the need to reread the data at the end of the transaction to obtain a lock, and without blocking other transactions reading the data.

read lock は、ある transaction が完了するまでは、lock対象のレコードへの他の書き込みが発生しては困る場合に使う。

組み方にも依るが、割とよく使う lock である。

対して write lock は mutex のようなもので、完全に I/O を serialize してしまうので、安全だがパフォーマンスに悪影響が出る。