Preventing inserting same value twice in an oracle database

Go To StackoverFlow.com

1

I have a number which is in this form : 2012-01 (2012 as current year) and 01 is just a the maximum value of a field in my database incremented by 1, and each year that number is reset to 0.

but if there are two users that try to do the same operation at the same time the value is the same for both and thus i get the same number inserted twice in my database .

I thought of creating a sequence but that requires a job that resets the sequence each year and i would prefer if there is a way to make a lock before i get the next number and release it after an insert is done ?

Thanks.

2012-04-05 17:06
by Mouna Cheikhna
Take a look at this Ask Tom link: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11QUESTIONID:4343369880986 Since you are trying to reset the sequence, you're basically trying for a "gap-free" approach, which is highly discouraged because of its performance implications (locking rows to ensure concurrency does not scale well) - Dan A. 2012-04-05 19:45


2

You don't specify where you store the field that is used as the counter. But maybe it is possible to use a SELECT FOR UPDATE statement.

Before you increment the value of your counter field by 1 you can lock that record by using a SELECT FOR UPDATE. Then update the counter.

Something like this, assuming the table has only 1 record:

SELECT *  
FROM   CounterTable
FOR UPDATE;

UPDATE CounterTable
SET    Counter = Counter + 1;

COMMIT;

If one session (user) has done the SELECT FOR UPDATE and not yet committed or rolled back, the other session (user) doing a SELECT FOR UPDATE will block waiting to be able to get a lock. This prevents two users from getting the same number.

2012-04-05 19:30
by Remko Jansen
thanks a lot , this worked grea - Mouna Cheikhna 2012-04-06 12:41


5

CREATE UNIQUE INDEX index_name ON table_name (column_name);

or

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);

2012-04-05 17:09
by cagcowboy
thanks , that seems cool , but can i catch this particular exception (unique constaint violation ) to insert the next row with my column incremented again - Mouna Cheikhna 2012-04-05 17:14
It is generally not recommended to use exceptions as a means to control the flow of your program - Remko Jansen 2012-04-05 19:34
@RemkoJansen, I don't necessarily agree with you on the "generally" but in this case you're correct. The OP needs to be using a sequence if they care so little about whether there's a PK violation or not - Ben 2012-04-05 19:51
@Remko, exceptions are designed for controlling the flow of a program. Without them you'd have to avoid all database constraints - Jeffrey Kemp 2012-04-11 05:24
@JeffreyKemp, I agree that they are very useful. What i meant was that you shouldn't rely on Exceptions to implement loops like Mona Cheikhna was suggesting above... "insert rows with an incremented column value until the Unique Contraint Violation goes away. - Remko Jansen 2012-04-11 06:49
I didn't notice that comment. Agreed with you there Remko - Jeffrey Kemp 2012-04-11 07:58
Ads