I currently have an INSERT TRIGGER which in Oracle 10g runs a custom defined function that generates a funky alpha-numeric code that is used as part of the insert.
I really need to make sure that the function (or even trigger) is thread safe so that if two users activate the trigger at once, the function used within the trigger does NOT return the same code for both users.
The flow in the trigger is as follows:
START
- determine if we need to continue based on business logic
- run the custom function to get new code
- use the returned code as an insert into a different table
END
The main issue is if while step 2 is running, a separate thread fires the trigger, which also gets into step 2, and returns the same code as the first thread. (I understand that this is a very tight situation, but we need to handle it).
I have thought of two main ways of doing this:
The currently best way that I have thought of so far is to lock the table used in the trigger in "exclusive mode" at the very start of the trigger, and do not specify the NOWAIT attribute of the lock. This way each subsequent activation of the trigger will sort of "stop and wait" for the lock to be available and hence wait for other threads to finish with the trigger.
I would love to lock the table any deny reading of the table, but I could seem to find out how to do this in Oracle.
My idea is not ideal, but it should work, however i would love to hear from anyone who may have better ideas that this!
Thanks a lot for any help given.
Cheers, Mark