You cannot selectively release locks on rows. Once you lock row1, that lock will only be released at the end of your transaction. But the end of your transaction will also release any other locks held by your transaction (i.e. the lock on row2).
Depending on the business problem you are trying to solve, potentially you don't really want to lock individual rows. Potentially, you want to use the dbms_lock
package to acquire and release some user-defined locks. If you have user-defined locks lock1
, lock2
, and lock3
, then you could acquire and release the three locks just as you've outlined within a single transaction. Setting up user-defined locking, however, can be quite dangerous both because it requires a lot more work from developers who have to protect the right sections of their code with the appropriate locks and because it is possible to request a user-defined lock that will not be released when a transaction commits or is rolled back which makes it possible to really shoot yourself in the foot if you don't handle your exceptions correctly.