[summary of the invention]
Based on this, be necessary to provide a kind of tables of data disposal route that can prevent the sequence number conflict.
A kind of tables of data disposal route may further comprise the steps:
Create sequence table, described sequence table comprises the current maximal value field of name field, the sequence of tables of data and major key field;
Obtain the user to the operation requests of tables of data, obtain the title of the tables of data of user's operation according to described operation requests;
In described sequence table, search the title of the tables of data of described user operation, the record of the title that comprises described tables of data in the described sequence table is locked;
Obtain the current maximal value of sequence in the record that locks in the described sequence table, and the current maximal value of described sequence is increased predetermined value, upgrade the current maximal value of described sequence;
Upgrade the tables of data of described user's operation according to the current maximal value of the sequence after the described renewal.
Preferably, in described sequence table, search the title of the tables of data of described user operation, the record of the title that comprises described tables of data in the described sequence table advanced line-locked step be specially:
In described sequence table, search the title of the tables of data of described user's operation, when in described sequence table, not finding, described sequence table is locked, in described sequence table, create the record of the title of the tables of data that comprises described user's operation, the current maximal value of sequence in the described record of initialization, then the record with the title that comprises described tables of data in the described sequence table locks.
Preferably, the current maximal value of the sequence in the record that in obtaining described sequence table, locks, and with the current maximal value increase of described sequence predetermined value, upgrade the current peaked step of described sequence and also comprise step afterwards:
Formation comprises the sequence of interval piece of a predetermined value sequence number;
The step of upgrading the tables of data of described user's operation according to the current maximal value of the sequence after the described renewal is: the tables of data of upgrading described user's operation according to described sequence of interval piece.
Preferably, described obtaining after the step of user to the operation requests of described tables of data also comprises:
Judge whether sequence number is finished in the described sequence of interval piece, if, then obtain new sequence of interval piece, if not, then directly from described sequence of interval piece, obtain sequence number, according to the tables of data of the described user's operation of the sequence number update that obtains, wherein, described sequence number is less than the current maximal value of described sequence.
Preferably, homing sequence number, maximum sequence number and current sequence number have been recorded in the described sequence of interval piece.
In addition, also be necessary to provide a kind of tables of data disposal system that can prevent the sequence number conflict.
A kind of tables of data disposal system comprises:
Creation module is used for creating sequence table, and described sequence table comprises the current maximal value field of name field, the sequence of tables of data and major key field;
Acquisition module is used for obtaining the user to the operation requests of tables of data, obtains the title of the tables of data of user's operation according to described operation requests;
Search module, be used for searching in described sequence table the title of the tables of data of described user's operation;
Processing module, be used for the record of the title that comprises described tables of data of described sequence table is locked, obtain the current maximal value of sequence in the record that locks in the described sequence table, and the current maximal value of described sequence is increased predetermined value, upgrade the current maximal value of described sequence;
Update module is used for upgrading the tables of data that described user operates according to the current maximal value of the sequence after the described renewal.
Preferably, in the described module of searching when in described sequence table, not finding the title of tables of data of described user operation, described processing module also is used for described sequence table is locked, in described sequence table, create the record of the title of the tables of data that comprises described user's operation, the current maximal value of the sequence of the described record of initialization, then the record with the title that comprises described tables of data in the described sequence table locks.
Preferably, described processing module also is used to form the sequence of interval piece that comprises a predetermined value sequence number; Described update module also is used for upgrading according to described sequence of interval piece the tables of data of described user's operation.
Preferably, also comprise judge module, described judge module is used for after described acquisition module obtains the operation requests of described user to described tables of data, judge whether sequence number is finished in the described sequence of interval piece, described acquisition module is when described judge module judges that sequence number has been finished in the described sequence of interval piece, described processing module also is used for obtaining new sequence of interval piece, described processing module also is used for when described judge module is judged described sequence of interval piece sequence number and is not finished, directly from described sequence blocks, obtain sequence number, described update module also is used for the tables of data according to the described user's operation of the sequence number update that obtains, wherein, described sequence number is less than the current maximal value of described sequence.
Preferably, homing sequence number, maximum sequence number and current sequence number have been recorded in the described sequence blocks.
Above-mentioned tables of data disposal route and system, create sequence table, obtain the user to the operation requests of tables of data and the title of tables of data, in sequence table, search the title of the tables of data of user's operation, the record that will comprise the title of this tables of data locks, obtain the current maximal value of sequence in the record of locking, after increasing predetermined value, the current maximal value of renewal sequence, upgrade again the tables of data of user's operation according to the current maximal value of the sequence of upgrading, owing to having locked the record of the title of the tables of data that comprises user's operation, in the time of can guaranteeing that certain user operates tables of data, other users are invalid to the operation of this tables of data, situation that so can occurrence sequence number conflict, and the data that can be applicable in the distributed environment are synchronous.
[embodiment]
Be described in detail below in conjunction with specific embodiment and the accompanying drawing technical scheme to sequence generating method and system.
As shown in Figure 1, in one embodiment, a kind of sequence generating method may further comprise the steps:
Step S110 creates sequence table, and this sequence table comprises the current maximal value field of name field, the sequence of tables of data and major key field.
Utilize the sql like language of standard to create a sequence table.The current maximal value field of name field, sequence and the major key field that comprise tables of data in this sequence table.For example, the structure of the sequence table of establishment is as follows:
create?table?My_IDSequence(
seqName?varchar(128),
seqNext?int,
primary?key(seqName)
);
Wherein, the name field of seqName varchar (128) expression tables of data, seqNext int represents the current maximal value field of sequence, primary key (seqName) represents major key field.The record that has many tables of data in this sequence table.The sequence number that will generate next time in the maximal value of existing sequence number or the tables of data in the value representation tables of data in the current maximal value field of sequence, maximal value such as already present sequence number in the tables of data is 20, and the current maximal value of the sequence of this tables of data that then records in the sequence table can be 20 or 21.In the present embodiment, the current maximal value of the sequence of the tables of data that records in this sequence table is the sequence number that tables of data will generate next time.This sequence table with the name field of tables of data as major key.
Step S120 obtains the user to the operation requests of tables of data, obtains the title of the tables of data of user's operation according to this operation requests.
Obtain the user to the operation requests of tables of data, because there being the record of many tables of data in the sequence table, need obtain the title of the tables of data of user operation, in order to follow-uply in sequence table, search.After obtaining the operation requests of user to tables of data, can be connected with Database, at this moment, need this connection to be set to non-automatic submission data, to guarantee follow-up lock operation.Submitting data to database is association database affairs.Generally speaking, db transaction is defaulted as automatic submission, namely as namely submitting data to after the operation, in the present embodiment, need should to connect maintenance a period of time, guarantee that operation in the whole affairs is finished after, just submit data to, transfer accounts 100 yuan such as user A in the account of user B, be divided into two steps: 1. button goes 100 yuan in the account of user A; 2. in the account of user B, increase by 100 yuan.When two steps are all finished, just submit data to.
Step S130 searches the title of the tables of data of user operation in this sequence table, the record of the title that comprises this tables of data in this sequence table is locked.
Search the title of the tables of data of user's operation in sequence table, if find, then direct record to the title that comprises this tables of data in the sequence table locks, and to prevent other users the record of the title of this tables of data is operated.
In one embodiment, if when in sequence table, not finding, then this sequence table is locked, in sequence table, create the record of the title of the tables of data that comprises this user's operation, the current maximal value of sequence of the record that initialization creates, the record with the title of the tables of data that comprises user's operation in this sequence table locks again.Sequence table is locked, and in the time of can preventing that other users from operating this tables of data simultaneously, sequence table also creates same record, thereby causes record to repeat, and occupies resource.After establishment comprises the record of title of tables of data of this user operation, the current maximal value of sequence to the tables of data that comprises this user operation is carried out initialization, can be 1 such as the current maximal value of this sequence, the sequence number that namely represents to generate in the tables of data of this user's operation is since 1.The record of the title of the tables of data that comprises user's operation that then will create locks, and can discharge the locking to sequence table, makes things convenient for other users that the record of the title that comprises other tables of data in the sequence table is operated.
Step S140 obtains the current maximal value of sequence in the record that locks in this sequence table, and the current maximal value of this sequence is increased predetermined value, upgrades the current maximal value of this sequence.
After the record of the title of the tables of data of the user that comprises in sequence table operation locked, get access to the current maximal value of sequence of this record, the current maximal value of this sequence increases predetermined value for the sequence number that this tables of data will generate next time with it, has upgraded the current maximal value of sequence.This predetermined value can be Arbitrary Digit, such as 10000.The current maximal value of sequence of the record of the title of the tables of data that comprises user operation of for example obtaining is 10001, then increase predetermined value 10000 after, the current maximal value of the sequence after the renewal is 20001.
In a preferred embodiment, after step S140, also comprise step: form the sequence of interval piece that comprises a predetermined value sequence number.Such as the sequence of interval piece [10001,20000] that comprises 10000 sequence numbers that forms, sequence number is the round values that comprises two end points in the sequence of interval piece.
Record homing sequence number, maximum sequence number and current sequence number in the sequence of interval piece, saved cache resources.Wherein, current sequence number refer to the serial number assignment in the sequence of interval piece to the user after, next with the sequence number that distributes.
After comprising the sequence of interval piece of a predetermined value sequence number, formation also comprises: submit data to, discharge the record of the title of the tables of data that comprises user's operation in the sequence table of locking.So that other users operate the record of the title of this tables of data.
Step S150 upgrades the tables of data that the user operates according to the current maximal value of sequence after upgrading.
Comprise the sequence of interval piece of a predetermined value sequence number in formation after, the tables of data of upgrading user's operation according to the current maximal value of sequence after upgrading is specially: the tables of data of upgrading user's operation according to this sequence of interval piece.This sequence of interval piece has comprised the sequence number between the current maximal value of sequence of upgrading after the front current maximal value of sequence arrives renewal, sequence number in this sequence of interval piece is all distributed to this user, when other users operate this tables of data, can not use the sequence number in this sequence of interval piece.
In one embodiment, after obtaining the operation requests of this user to this tables of data, also comprise: judge whether sequence number is finished in this sequence of interval piece, if then carry out and obtain new sequence of interval piece, if not, then directly from this sequence of interval piece, obtain sequence number, according to the tables of data of this user's operation of the sequence number update that obtains, wherein, described sequence number is less than the current maximal value of described sequence.
For example, when personnel system operation subscriber's meter, in subscriber's meter, insert user A, need to distribute a sequence number to user A, if the sequence generator of subscriber's meter obtains sequence of interval piece [1,10000], judge first whether the sequence number in this sequence of interval piece is finished, if do not have, then directly from the sequence of interval piece, obtain a serial number assignment to user A, if then need obtain new sequence of interval piece.
As shown in Figure 2, in one embodiment, a kind of tables of data disposal system comprises creation module 210, acquisition module 220, searches module 230, processing module 240 and update module 250.Wherein,
Creation module 210 is used for creating sequence table, and this sequence table comprises the current maximal value field of name field, the sequence of tables of data and major key field.Utilize the sql like language of standard to create a sequence table.The current maximal value field of name field, sequence and the major key field that comprise tables of data in this sequence table.The structure of sequence table can as describing in the above-mentioned tables of data disposal route, not repeat them here.
Acquisition module 220 is used for obtaining the user to the operation requests of tables of data, obtains the title of the tables of data of user's operation according to this operation requests.Acquisition module office 220 obtains the user to the operation requests of tables of data, because there being the record of many tables of data in the sequence table, need obtain the title of the tables of data of user operation, in order to follow-uply search in sequence table.
Search module 230 for the title of the tables of data of searching this user's operation in this sequence table.
Search module 230 and in sequence table, search the title of the tables of data of user's operation, if find, then processing module 240 directly locks the record of the title that comprises this tables of data in the sequence table, to prevent other users the record of the title of this tables of data is operated.
If search module 230 when in sequence table, not finding, then processing module 240 locks this sequence table, in sequence table, create the record of the title of the tables of data that comprises this user's operation, the current maximal value of sequence of the record that initialization creates, the record with the title of the tables of data that comprises user's operation in this sequence table locks again.Sequence table is locked, and in the time of can preventing that other users from operating this tables of data simultaneously, sequence table also creates same record, causes record to repeat, and occupies resource.After establishment comprises the record of title of tables of data of this user operation, the current maximal value of sequence to the tables of data that comprises this user operation is carried out initialization, can be 1 such as the current maximal value of this sequence, the sequence number that namely represents to generate in the tables of data of this user's operation is since 1.The record of the title of the tables of data that comprises user's operation that then will create locks, and can discharge the locking to sequence table, makes things convenient for other users that the record of the title that comprises other tables of data in the sequence table is operated.
Processing module 240 is used for the record of the title of the tables of data that comprises user's operation of this sequence table is locked, obtain the current maximal value of sequence in the record that locks in this sequence table, and with the current maximal value increase of this sequence predetermined value, upgrade the current maximal value of this sequence.
After the record of the title of the tables of data of the user's operation that comprises in 240 pairs of sequence tables of processing module locks, get access to the current maximal value of sequence of this record, the current maximal value of this sequence increases predetermined value for the sequence number that this tables of data will generate next time with it, has upgraded the current maximal value of sequence.This predetermined value can be Arbitrary Digit, such as 10000.The current maximal value of sequence of the record of the title of the tables of data that comprises user operation of for example obtaining is 10001, then increase predetermined value 10000 after, the current maximal value of the sequence after the renewal is 20001.
In the preferred embodiment, processing module 240 also is used to form the sequence of interval piece that comprises a predetermined value sequence number.Such as the sequence of interval piece [10001,20000] that comprises 10000 sequence numbers that forms, sequence number is the round values that comprises two end points in the sequence of interval piece.
Homing sequence number, maximum sequence number and current sequence number have been recorded in the sequence of interval piece.Saved cache resources.Wherein, current sequence number refer to the serial number assignment in the sequence of interval piece to the user after, next with the sequence number that distributes.
Processing module 240 also is used for submitting to data, discharges the record of the title of the tables of data that comprises user's operation in the sequence table that locks.So that other users operate the record of the title of this tables of data.
Update module 250 is used for upgrading according to the current maximal value of the sequence after this renewal the tables of data of this user's operation.
Update module 250 also is used for upgrading according to this sequence of interval piece the tables of data of this user's operation.This sequence of interval piece has comprised the sequence number between the current maximal value of sequence of upgrading after the front current maximal value of sequence arrives renewal, sequence number in this sequence of interval piece is all distributed to this user, when other users operate this tables of data, can not use the sequence number in this sequence of interval piece.
In one embodiment, as shown in Figure 3, above-mentioned tables of data disposal system, except comprise creation module 210, acquisition module 220, search module 230, processing module 240 and update module 250, also comprise judge module 260.Wherein,
Judge module 260 is used for after acquisition module 220 obtains the operation requests of this user to this tables of data, judge whether sequence number is finished in this sequence of interval piece, acquisition module 220 is when judge module 260 judges that sequence number has been finished in this sequence of interval piece, obtain the title of the tables of data of user's operation according to this operation requests, search module 230 and also be used for searching in this sequence table the title of the tables of data of this user's operation, processing module 240 also is used for the record of the title of the tables of data that comprises user's operation of this sequence table is locked, obtain the current maximal value of sequence in the record that locks in this sequence table, and with the current maximal value increase of this sequence predetermined value, upgrade the current maximal value of this sequence, form the new sequence of interval piece that comprises a predetermined value sequence number; Processing module 240 also is used for when judge module 260 is judged this sequence of interval piece sequence number and is not finished, directly from this sequence blocks, obtain sequence number, update module 250 also is used for the tables of data according to this user's operation of the sequence number update that obtains, wherein, this sequence number is less than the current maximal value of this sequence.
For example, when personnel system operation subscriber's meter, in subscriber's meter, insert user A, need to distribute a sequence number to user A, if subscriber's meter obtains sequence blocks [1,10000], judge module 260 judges whether the sequence number in this sequence blocks is finished, if do not have, then processing module 240 directly obtains a serial number assignment to user A from the sequence of interval piece, if then need obtain new sequence of interval piece.
For the course of work of clearer explanation tables of data disposal system, describe with concrete example, but the notebook data list processing system is not limited to be applied to this concrete example.
There is subscriber's meter T_user (id int, userName varchar (128)) in main data system; Organization table T_org (id int, orgName varchar (128)).Creation module 210 creates first sequence table My_IDSequence in database.If two application systems are arranged, the S1 of human resources system and purchasing system S2.S1 and S2 have the business that creates the user, need to generate user's sequence number.S1 system creation sequence generator SEQ1, S2 system creation sequence generator SEQ2, all sequence numbers generate since 1.
When the request that user A1 is inserted in operation to subscriber's meter T_user of S1 system, after then acquisition module 220 obtains this operation requests, SEQ1 is connected with Database, search module 230 and from the seqName of My_IDSequence table, search the record that whether has T_user, if do not exist, then processing module 240 locking My_IDSequence show, in the My_IDSequence table, create the record of the title that comprises this T_user, and then the record of the title that comprises this T_user locked, the current maximal value seqNext of the sequence of this T_user of initialization is 1, the current maximal value seqNext of sequence that inquiry obtains this T_user is 1, with 1 increase predetermined value 10000, then upgrading seqNext is 10001, and formation sequence extent block [1,10000], to be that SEQ1 is unique hold this sequence of interval piece.Update module 250 is upgraded this subscriber's meter T_user according to this sequence of interval piece, namely obtains a serial number assignment to user A1 from the sequence of interval piece.
Equally, the request that user a1 is inserted in operation to subscriber's meter T_user of S2 system, after then acquisition module 220 obtains operation requests, SEQ2 is connected with Database, search the T_user record among the seqName that module 230 finds My_IDSequence table, processing module 240 obtains the row level lock of this T_user, the current maximal value seqNext of sequence that inquiry obtains this T_user is 10001, increase predetermined value 10000 with 10001, then upgrading seqNext is 20001, and formation sequence extent block [10001,20000], to be that SEQ2 is unique hold this sequence of interval piece.Update module 250 is upgraded this subscriber's meter T_user according to this sequence of interval piece, namely obtains a serial number assignment to user a1 from this sequence of interval piece.
So the data in subscriber's meter T_user, inserted of SEQ1 for (1, A1), (2, A2) ..., (10000, A10000).
The data that SEQ2 inserts in subscriber's meter T_user for (10001, a1), (10002, a2) ..., (20000, a10000).
Above-mentioned tables of data disposal route and system, create sequence table, obtain the user to the operation requests of tables of data and the title of tables of data, in sequence table, search the title of the tables of data of user's operation, the record that will comprise the title of this tables of data locks, obtain the current maximal value of sequence in the record of locking, after increasing predetermined value, the current maximal value of renewal sequence, upgrade again the tables of data of user's operation according to the current maximal value of the sequence of upgrading, owing to having locked the record of the title of the tables of data that comprises user's operation, in the time of can guaranteeing that certain user operates tables of data, other users are invalid to the operation of this tables of data, so can occurrence sequence number conflict, can be applicable in the distributed environment data synchronous.
In addition, do not rely on concrete programming language, expanded its range of application, obtain a sequence of interval piece at every turn, reduced the access times of database, improved the sequence number formation efficiency; Only record start sequence number, maximum sequence number and current sequence number in the sequence blocks can be saved cache resources.
The above embodiment has only expressed several embodiment of the present invention, and it describes comparatively concrete and detailed, but can not therefore be interpreted as the restriction to claim of the present invention.Should be pointed out that for the person of ordinary skill of the art, without departing from the inventive concept of the premise, can also make some distortion and improvement, these all belong to protection scope of the present invention.Therefore, the protection domain of patent of the present invention should be as the criterion with claims.