Concurrent operations on serial number generation in a sequential account table

Source: Internet
Author: User

Concurrent operations on serial number generation in a sequential account table
1. The sequence numbers of some flow tables do not have certain rules,
For example, four-digit year + two-digit week number + seven-digit serial number
2007130000002.
Assume that the table is defined as follows:

Create Table T
(ID char (16) not null primary key,
Others varchar2 (100)
)

In this case, sequence usage is not advisable. At this time, you may think of two methods.
1. Take the data of the table, and add one in the sequence number of the latest record.
Select max (ID) from T where Substr (T. ID,) = to_char (sysdate, 'yyyyww ')
In this case,If two applications insert a sequential account at the same time, they may get the same sequential account number, causing one operation to fail.
In addition, if the data size of this table is large, it takes a certain amount of time to obtain the maximum value.
2. Use another table to record the maximum value. Assume that the table is defined as follows:

Create Table SEQ_T
(Week char (6 ),
ID number (10, 0)
)
/
Insert into SEQ_T values ('20170101', 0)
/

But there will also be a problem with the first solution:
If two applications insert a sequential account at the same time, they may get the same sequential account number, causing one operation to fail.


How can I solve the problem of obtaining the same serial number for this concurrent query.
The second method is improved.
Step 1 lock this serial number table.
Lock Table SEQ_X;
Then execute the second method.
Remember to use commit; otherwise, it cannot be unlocked.

Some may say that other Oracle sessions may still use the second method. The first method is used to obtain the serial number, which you cannot control.
However, you must ensure that your application only has this unique path to insert the journal table.
The following is a program for testing concurrency. You can start multiple clients to execute it at the same time.

Declare
V_ID Number (10 );
V_week varchar2 (10 );
V_week2 varchar2 (10 );
Num number (10 );
N number (5 );
Begin
FOR num IN 1 .. 100000 LOOP
Lock TABLE SEQ_T in exclusive mode;
Select week into v_week from SEQ_T where rownum = 1;
Select ID into v_id from SEQ_T where rownum = 1;
V_week2: = to_char (sysdate, 'yyyyww ');
If v_week = v_week2 then
V_id: = v_id + 1;
Else
V_week: = v_week2;
V_id: = 1;
End if;

Update seq_T
Set id = v_id,
Week = v_week;
Insert into T values (v_week | to_char (v_id), 'test ');
Commit;
End loop;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.