I've a scenario with multiple threads that performs select/insert/update on the same table.
Basically, the flow is:
Expand|Select|Wrap|Line Numbers
- SELECT * FROM tablex where pk=?
Expand|Select|Wrap|Line Numbers
- INSERT into tablex values(...)
Expand|Select|Wrap|Line Numbers
- UPDATE tablex set ... where pk=?
To improve performance, I'd like that threads with different Pks will run without blocking, but threads with the same PK will be "serialized".
Using SELECT FOR UPDATE WITH RS is fine when the row already exists, but produce duplicate insert when the row doesn't exist yet, and two thread are trying to insert it.
Using TRANSACTION_SERIALIZABLE or SELECT FOR UPDATE WITH RR cause a table lock, and so threads with different Pks are blocked.
Is there a basic pattern to solve this problem?
Environment: Db2 V9.1 on Windows. Using a normal Java application