![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; -- Update the next id on the table UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#3
| |||
| |||
|
|
What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; -- Update the next id on the table UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#4
| |||
| |||
|
|
Unless you will do some other procedural processing or checking between your SELECT and your UPDATE (w/c you did not include in your code sample), then you could just use one statement: UPDATE owner.my_id_table SET next_id = next_id + 1 WHERE app_id = app_id_in; Otherwise, check out "FOR UPDATE" in the manuals: Concepts, or SQL Guide, or Application Developer's Guide (Fundamentals). But I really think the above would work for your purposes. HTH. webmaster (AT) vbusers (DOT) com (Andrew Baker) wrote in message news:<c19b84e5.0407271054.54ae108a (AT) posting (DOT) google.com>... What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; -- Update the next id on the table UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#5
| |||
| |||
|
|
What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; -- Update the next id on the table UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#6
| |||
| |||
|
|
What is the best way to lock an individual row in the following scenerio: --TODO - LOCK THIS ROW -- Return the next id SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; -- Update the next id on the table UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#7
| |||
| |||
|
|
Thanks for the reply, but I think you may have miss-read the SQL (with hindsight I wasn't being very clear!). The first statement does a select into the store procs output parameter called "next_id_out": SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; The next sql updates the table to move the next available Id on by one: UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; my problem is that this sp will be called v. intensively by lots of processes. So I think that the same Id could be returned twice unless I lock the row... thanks in advance andrew [snip] I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() |
#8
| |||
| |||
|
|
webmaster (AT) vbusers (DOT) com (Andrew Baker) wrote in message news:<c19b84e5.0407280224.3651abb (AT) posting (DOT) google.com>... Thanks for the reply, but I think you may have miss-read the SQL (with hindsight I wasn't being very clear!). The first statement does a select into the store procs output parameter called "next_id_out": SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; The next sql updates the table to move the next available Id on by one: UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; my problem is that this sp will be called v. intensively by lots of processes. So I think that the same Id could be returned twice unless I lock the row... thanks in advance andrew [snip] I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() Since you are new, you may not realize, you are reimplementing an ORACLE feature known as a SEQUENCE. A SEQUENCE will provide exactly what you need without the need to implement stored procedures which single thread processing by putting locks on resources. So instead of mystoredproc( next_id_out ); use the SEQUENCE as a pseudo-column on dual: select sequencename.nextval into next_id_out from dual ; SEQUENCE advantages include: fast returns of next value (no locking to slow things down) session independence (my value will never be the same as your value) application independent (each sequence is named so each can be used independently) robust (it works for many oracle customers, why write your own?) SEQUENCE, look for it in an ORACLE SQL Manual near you! HTH, ed |
#9
| |||
| |||
|
|
webmaster (AT) vbusers (DOT) com (Andrew Baker) wrote in message news:<c19b84e5.0407280224.3651abb (AT) posting (DOT) google.com>... Thanks for the reply, but I think you may have miss-read the SQL (with hindsight I wasn't being very clear!). The first statement does a select into the store procs output parameter called "next_id_out": SELECT next_id INTO next_id_out FROM owner.my_id_table WHERE app_id = app_id_in; The next sql updates the table to move the next available Id on by one: UPDATE owner.my_id_table SET next_id = next_id_out + 1 WHERE app_id = app_id_in; my problem is that this sp will be called v. intensively by lots of processes. So I think that the same Id could be returned twice unless I lock the row... thanks in advance andrew [snip] I need to make sure that nothing changes the id table between me selecting the id and updating the table with the next available id. thanks andrew PS. yes I am new to oracle ![]() Since you are new, you may not realize, you are reimplementing an ORACLE feature known as a SEQUENCE. A SEQUENCE will provide exactly what you need without the need to implement stored procedures which single thread processing by putting locks on resources. So instead of mystoredproc( next_id_out ); use the SEQUENCE as a pseudo-column on dual: select sequencename.nextval into next_id_out from dual ; SEQUENCE advantages include: fast returns of next value (no locking to slow things down) session independence (my value will never be the same as your value) application independent (each sequence is named so each can be used independently) robust (it works for many oracle customers, why write your own?) SEQUENCE, look for it in an ORACLE SQL Manual near you! HTH, ed |
#10
| ||||
| ||||
|
|
After thinking through swapping to using a sequence number I came up with a couple of potential gotchas: 1. When we fail over to our DR (disaster recovery) boxes the numbers must continue their sequence. Would a sequence number work under this circumstance? |
|
2. When the box reboots/crashes does it continue the sequence number from where it was before the restart? |
|
3. Can you manually tune the sequence numbers to increment by specified amounts? |
|
4. Is a sequence number system wide? ie. I can it be table specific like IDENTITY columns in SQL server? |
![]() |
| Thread Tools | |
| Display Modes | |
| |