Summary of the invention
The present invention aims to provide a kind of concurrency control method and device; Can solve other concurrent transactions that concurrency control method in the correlation technique makes all requests operate same data-base recording and all get into waiting list; After being finished to lock release by last lock-based transaction execution; Other concurrent transactions just are able to database operated again, and cause the concurrency of affairs control lower, the problem that system performance is relatively poor.
In an embodiment of the present invention, a kind of concurrency control method is provided, has may further comprise the steps: whether the operating position of treating of judging affairs according to the lock relation table has and treats the afoul lock of granted lock, locks the relation that relation table is used to preserve affairs and lock; To the affairs granted lock, and carry out affairs according to judged result; When affairs are carried out end, discharge the lock that affairs are held, and the lock that need not in the lock that will discharge to continue to wait for is authorized other concurrent transactions.
Preferably, in above-mentioned concurrency control method, the lock relation table specifically comprises: transaction list is used to the affairs of preserving concurrent execution and not submitting to; The lock Hash table is used to follow the tracks of the lock of waiting in line; The chain table is used for association affairs and lock.
Preferably, in above-mentioned concurrency control method,, and carry out affairs and specifically comprise the affairs granted lock according to judged result:,, and carry out affairs the affairs granted lock if the operating position of treating of affairs does not have lock.
Preferably, in above-mentioned concurrency control method, to the affairs granted lock, and carry out affairs and specifically comprise according to judged result: if affairs treat the existing lock of operating position, the existing lock of inspection with treat whether granted lock exists and conflict; To the affairs granted lock, and carry out affairs according to check result.
Preferably, in above-mentioned concurrency control method, to the affairs granted lock, and carry out affairs and specifically comprise based on check result: if existing lock with treat that granted lock does not exist and conflict, to the affairs granted lock, and the execution affairs.
Preferably, in above-mentioned concurrency control method, to the affairs granted lock, and carry out affairs and specifically comprise based on check result: if existing lock with treat that granted lock exists and conflict, transaction queue's wait is set, treat granted lock until regaining; Carry out affairs.
Preferably, in above-mentioned concurrency control method, carry out affairs comprise following one of at least: increase operation, carry out deletion action, the operation of making amendment, carry out read operation.
Preferably, in above-mentioned concurrency control method, affairs are carried out and finished to comprise: affairs are accomplished and are submitted to or affairs completion rollback.
Preferably, in above-mentioned concurrency control method, authorize other concurrent transactions with the lock that need not in the lock that discharges to continue to wait for and specifically comprise: each lock in the lock place chain table that discharges, detect whether the sign of wait is arranged; As waiting for sign; And when there is not the lock type comflict in other locks on the same operating position of lock to be detected and other concurrent transactions; Authorize other concurrent transactions with lock to be detected, wake other concurrent transactions up and continue to carry out, the lock type comprises shared lock and exclusive lock.
In an embodiment of the present invention, a kind of concurrent control device is provided also, has comprised: judge module is used for judging that according to the lock relation table whether the operating position of treating of affairs has and treat the afoul lock of granted lock, locks the relation that relation table is used to preserve affairs and lock; Execution module is used for according to judged result the affairs granted lock, and carries out affairs; Release module is used for when affairs are carried out end, discharge the lock that affairs are held, and the lock that need not in the lock that will discharge to continue to wait for being authorized other concurrent transactions.
Preferably, in above-mentioned concurrent control device, the lock relation table specifically comprises: transaction list is used to the affairs of preserving concurrent execution and not submitting to; The lock Hash table is used to follow the tracks of the lock of waiting in line; The chain table is used for association affairs and lock.
The foregoing description is because the relation that adopts the lock relation table to preserve its corresponding lock of affairs; So can lock the judgement that whether has conflict to pointing to the same difference of operating position of treating; And carry out differentiated treatment according to judged result; In a single day rather than treat the concurrency control method that in correlation technique operative position is equipped with lock and just all lists the corresponding affairs of every other lock in waiting list, and so it is lower to the concurrency of affairs control to have overcome the concurrency control method in the correlation technique, the problem that system performance is relatively poor; And then improved the concurrency of affairs controls, improved concurrent control effect.
Embodiment
Below with reference to accompanying drawing and combine embodiment, specify the present invention.
Fig. 1 shows the process flow diagram according to the concurrency control method of first embodiment of the invention, and this method may further comprise the steps:
Step S101 judges according to the lock relation table whether the operating position of treating of affairs has and treat the afoul lock of granted lock, locks the relation that relation table is used to preserve affairs and lock;
Step S102 to the affairs granted lock, and carries out affairs according to judged result;
Step S103 when affairs are carried out end, discharge the lock that affairs are held, and the lock that need not in the lock that will discharge to continue to wait for is authorized other concurrent transactions.
Present embodiment at first judges according to the lock relation table whether the operating position of treating of affairs has and treat the afoul lock of granted lock; Then according to judged result to the affairs granted lock, and carry out affairs, at last when affairs are carried out end; Discharge the lock that affairs are held; And the lock that need not in the lock that will discharge to continue to wait for authorizes other concurrent transactions, because the relation that has adopted the lock relation table to preserve its corresponding locks of affairs, so can lock the judgement that whether has conflict to pointing to the same difference of operating position of treating; And carry out differentiated treatment according to judged result; In a single day rather than treat the concurrency control method that in correlation technique operative position is equipped with lock and just all lists the corresponding affairs of every other lock in waiting list, and so it is lower to the concurrency of affairs control to have overcome the concurrency control method in the correlation technique, the problem that system performance is relatively poor; And then improved the concurrency of affairs controls, improved concurrent control effect.
Preferably, in above-mentioned concurrency control method, the lock relation table specifically comprises: transaction list is used to the affairs of preserving concurrent execution and not submitting to; The lock Hash table is used to follow the tracks of the lock of waiting in line; The chain table is used for association affairs and lock.
Lock relation table in the present embodiment specifically comprises transaction list, lock Hash table and chain table; The concurrency control method that is arranged so that present embodiment of above-mentioned table can effectively be managed affairs and lock; Comprise index location, traversal search etc. apace; And then realized locking the judgement that whether has conflict to pointing to the same difference of operating position of treating, for providing, above-mentioned concurrency control method provides powerful support for.
Transaction list is preserved the current affairs transaction information of other affairs of concurrent execution in the system when carrying out; The principle that is inserted into team's head according to the late comer is inserted, the pointer of node, the meter pointer of chain table etc. before and after transaction information specifically comprises in affairs ID, transaction types, the sensing transaction list.Affairs of each startup all can be inserted into these affairs in the transaction list, promptly can the transaction information of these affairs be saved in the transaction list.Comprise a thread structure information in the transaction structure, wherein comprised event object, when affairs because of waiting for the lock hanging time-out, carry out this event object of thread waits of affairs; When the affairs granted lock thereby when being waken up, the thread of granted lock is provided with this event object.
Fig. 2 shows the synoptic diagram of the lock Hash table that concurrency control method adopts among Fig. 1; As shown in Figure 2; Each list item of lock Hash table is corresponding to the gauge outfit of a chain table; Do like this, make that be that key calculates cryptographic hash with the file space of writing down in the lock construction number with page number, can navigate to corresponding chain table through corresponding lock Hash table list item.
The chain table is used for the affairs of association affairs tabulation record and locks the lock that writes down in the Hash table; As shown in Figure 2; Each chain table comprises a plurality of lock constructions; Each lock construction has write down information such as the file space number, page number, lock-bit figure, the shared bit of lock-bit figure (bit) number, lock type (shared lock, exclusive lock), lock sign (wait for, authorize), affiliated affairs of the data-base recording of the lock locking of waiting in line, and wherein the heap_no position of lock-bit figure is a sign of treating the operating position record.Each affairs is corresponding to a chain table, lock of the every application of affairs, and all chain is gone in corresponding chain table, and this chain table is preserved the lock that these affairs are held, and every pair of record is operated, and just can insert in the corresponding chain table of these affairs locking accordingly.A lock construction can be shared with other lock of the same type on one page; The lock that on other record, adds same type if desired; Only need in the chain table, to search to have or not similar lock; If find then, adopt single linked list promptly to solve hash collision like this, and saved memory headroom effectively with the corresponding heap_no bit of lock-bit figure.
Preferably, in above-mentioned concurrency control method, step S102 specifically comprises: if the operating position of treating that thing 0 is engaged in does not have lock, to the affairs granted lock, and carry out affairs.
If the operating position of treating of affairs does not have lock,, and carry out respective transaction in the present embodiment then to these affairs granted lock.Do like this, make data-base recording to be operated when not having other transaction operations, accept the operation of these affairs, under the prerequisite that guarantees concurrent control correctness, improved the concurrency of control.
Preferably, in above-mentioned concurrency control method, step S102 specifically comprises: if affairs treat the existing lock of operating position, the existing lock of inspection with treat whether granted lock exists and conflict; To the affairs granted lock, and carry out affairs according to check result.
In the present embodiment if affairs treat the existing lock of operating position, then need continue to check existing lock and treat whether granted lock exists and conflict that this conflict comprises: WR conflict, RW conflict and WW are outstanding, and then according to check result to the affairs granted lock, and execution affairs.Do like this, can and not exist two kinds of different situations of conflict to carry out differentiated treatment to the existence conflict, rather than after judgement obtains existing the lock, just every other affairs are hung up wait, improved the concurrency of control.
Preferably, in above-mentioned concurrency control method, to the affairs granted lock, and carry out affairs and specifically comprise based on check result: if existing lock with treat that granted lock does not exist and conflict, to the affairs granted lock, and the execution affairs.
Though in the present embodiment affairs treat the existing lock of operating position, check result for existing lock with treat that granted lock does not exist and conflict, at this moment, still to the affairs granted lock, and the execution affairs.Present embodiment has been caught the execution opportunity of concurrent transaction under situation about being independent of each other, and makes the available resources maximization, and the concurrency control method of comparing in the correlation technique has improved the concurrency of control effectively.
Preferably, in above-mentioned concurrency control method, to the affairs granted lock, and carry out affairs and specifically comprise based on check result: if existing lock with treat that granted lock exists and conflict, transaction queue's wait is set, treat granted lock until regaining; Carry out affairs.
In the present embodiment if affairs treat the existing lock of operating position, and existing lock with treat that granted lock exists and conflict, these affairs then are set hang up insertion waiting list and wait in line, treat granted lock until regaining, and the execution affairs.Do like this; Make affairs to satisfy simultaneously to treat existing lock of operating position and existing lock with treat condition that the granted lock existence conflicts under just can hang up wait; Be that affairs are only just waited for when other concurrent transactions influence definite can receiving; Under the prerequisite that guarantees concurrent control correctness, improved the concurrency of control.
Preferably, in above-mentioned concurrency control method, carry out affairs comprise following one of at least: increase that (insert is also referred to as insertion) operated, carried out deletion action, the operation of making amendment, carry out read operation.
Carrying out affairs in the present embodiment can be for increasing, deletes, revise, read, or its combination.The concurrency control method of present embodiment is not limited to a certain operation, but may extend to above-mentioned four kinds of basic operations, and perhaps range of application has been expanded in its combination.
Fig. 3 shows the process flow diagram according to the concurrency control method of second embodiment of the invention, is operating as the example explanation with insertion in the present embodiment, and this method may further comprise the steps:
Step S301, watcher thread receives SQL (Structured Query Language, SQL) statement query requests, and distributes a worker thread to handle query requests;
Step S302, worker thread is carried out SQL query analysis and SQL query optimization, generates executive plan;
Step S303, worker thread adds the purpose exclusive lock to the latching operation table, shows that soon his-and-hers watches are carried out write operation;
Step S304, worker thread navigates to the record that article one satisfies X<=tuple from being inserted into data recording structure index entry tuple (tuple), navigates to the page or leaf that is inserted into record fast in order to index of reference;
Step S305 judges the Field Count of whether having built unique index and vernier coupling on the table more than or equal to the unique key Field Count, if less than, forward step S314 to;
Step S306 from being inserted into data recording structure index entry tuple (tuple), navigates to the record that article one satisfies X>=tuple, and X is an index record;
Step S307, whether if maximum transaction ID is more than or equal to minimum affairs ID in the current active affairs on the page, then checking has the implicit expression lock, if having, forward step S309 in the current record;
Step S308, this inserts operation and waits in line;
Step S309 obtains the corresponding gathering index record of current index record, if this record is just to insert, the implicit expression lock is arranged on this record then, converts this implicit expression lock into explicit lock;
Step S310 judges whether X=tuple sets up, and X=tuple representes to exist the unique key conflict, and also the unique key constraint is violated in expression, if be false, forwards step S312 to;
Step S311, newspaper repeat key mistake forwards step S305 to;
Step S312, shared lock documentarily;
Step S313 is repositioned onto the record that article one satisfies X<=tuple, and X is an index record;
Whether step S314 exists lock on next bar record of inspection current record, is that key calculates cryptographic hash with file space, record place number with page number promptly; Navigate to certain list item on the lock Hash table; Traversal is the chain table of gauge outfit with this list item, checks among the lock-bit figure of each lock the whether set of heap_no position, if the heap_no position is not set among the lock-bit figure; Promptly there is not lock on this record, forwards step S316 to;
Step S315, if the heap_no position is set among the lock-bit figure, then should upward there be lock in expression by record, continued to judge whether to exist lock conflict, if there is lock conflict, forwarded step S308 to;
Step S316, application locks successfully, inserts data, has prevented this record of other transactions modify;
Step S317 has lock if insert on next bar record that writes down, and then inherits the lock on next bar record, simultaneously waiting status is changed to the state of authorizing;
Step S318, affairs are carried out and are finished, and promptly affairs are accomplished and are submitted to or affairs completion rollback, discharge the lock of holding, and the lock that no longer need wait for is authorized in the formation of scanning lock Hash table lock simultaneously.
Present embodiment is applied in the Database Systems, such as the Database Systems of IPTV electronic program list.Because the number of users that each electronic program system can hold is limited, therefore a cover IPTV system need dispose a lot of electronic program lists.A commercial data base is all moved on each electronic program list backstage in the correlation technique; Cause cost higher; And present embodiment adopts above-mentioned concurrency control method near the commercial data base performance, has reduced cost, has satisfied the demand of IPTV system preferably; Possess the standard database characteristic simultaneously, these characteristics comprise transactional integrity, transaction concurrency control, fault recovery etc.Present embodiment has been controlled the transaction concurrency execution effectively, has guaranteed system performance simultaneously to the full extent.
Preferably, in above-mentioned concurrency control method, affairs are carried out and finished to comprise: affairs are accomplished and are submitted to or affairs completion rollback.
Affairs execution end comprises that the affairs completion is submitted to or affairs are accomplished rollback in the present embodiment, promptly when affairs completion submission or affairs completion rollback, discharges the lock that these affairs are held.When lock is accomplished the locking task, be released at once in the present embodiment, be convenient to other concurrent transactions and obtain corresponding lock, the chance to obtain to be performed helps to improve the concurrency of control.
Preferably, in above-mentioned concurrency control method, the lockset body that the release affairs are held comprises each lock in the lock place chain table that discharges, and it is deleted from this chain table, from the lock Hash table of correspondence, deletes simultaneously.
Carry out end when affairs in the present embodiment, when promptly these affairs are accomplished submission or accomplished rollback, the lock that these affairs are held is deleted from the corresponding chain table of these affairs, the while also deletes from the lock Hash table of correspondence.Present embodiment adopts will lock from corresponding chain table deletes the release that realizes lock with locking the Hash table; Operation is simple; And make and after lock is accomplished the locking task to certain affairs, be released at once; Be convenient to other concurrent transactions and obtain corresponding lock, the chance to obtain to be performed helps to improve the concurrency of control.
Preferably, in above-mentioned concurrency control method, authorize other concurrent transactions with the lock that need not in the lock that discharges to continue to wait for and specifically comprise: each lock in the lock place chain table that discharges, detect whether the sign of wait is arranged; As waiting for sign; And when there is not the lock type comflict in other locks on the same operating position of lock to be detected and other concurrent transactions; Authorize other concurrent transactions with lock to be detected, wake other concurrent transactions up and continue to carry out, the lock type comprises shared lock and exclusive lock.
Present embodiment detects at first whether the sign of wait is arranged in the lock type that is released lock; If the sign of wait is arranged; Showing that then lock is in waiting status, is that key calculates cryptographic hash with the file space, record place of the lock locking of waiting status number with page number then, navigates to the gauge outfit of corresponding chain table on the lock Hash table; Scanning lock place chained list; Whether have other lock to exist with it to each lock inspection in the chained list and lock type comflict, if there is the lock type comflict, lock then to be detected need wait for that existing the lock of locking type comflict to be released with it just might obtain to authorize chance; If there is not the lock type comflict; Lock then to be detected need not to wait for, authorizes other concurrent transactions with lock to be detected, the wait sign of this lock of resetting; Be about to this lock and change the state of authorizing into, and wake other corresponding concurrent transactions of lock to be detected up and continue to carry out by waiting status.
Fig. 4 shows the structural drawing according to the concurrent control device of third embodiment of the invention, and this device comprises:
Judge module 10 is used for judging that according to the lock relation table whether the operating position of treating of affairs has and treat the afoul lock of granted lock, locks the relation that relation table is used to preserve affairs and lock;
Execution module 20 is used for according to judged result the affairs granted lock, and carries out affairs;
Release module 30 is used for when affairs are carried out end, discharge the lock that affairs are held, and the lock that need not in the lock that will discharge to continue to wait for being authorized other concurrent transactions.
Present embodiment at first adoptsjudge module 10 to judge according to the lock relation table whether the operating position of treating of affairs has and treat the afoul lock of granted lock; Adopt thenexecution module 20 according to judged result to the affairs granted lock, and carry out affairs, adoptrelease module 30 at last when affairs are carried out end; Discharge the lock that affairs are held; And the lock that need not in the lock that will discharge to continue to wait for authorizes other concurrent transactions, because the relation that has adopted the lock relation table to preserve its corresponding locks of affairs, so can lock the judgement that whether has conflict to pointing to the same difference of operating position of treating; And carry out differentiated treatment according to judged result; In a single day rather than treat the concurrency control method that in correlation technique operative position is equipped with lock and just all lists the corresponding affairs of every other lock in waiting list, and so it is lower to the concurrency of affairs control to have overcome the concurrency control method in the correlation technique, the problem that system performance is relatively poor; And then improved the concurrency of affairs controls, improved concurrent control effect.
Preferably, in above-mentioned concurrent control device, the lock relation table specifically comprises: transaction list is used to the affairs of preserving concurrent execution and not submitting to; The lock Hash table is used to follow the tracks of the lock of waiting in line; The chain table is used for association affairs and lock.
Lock relation table in the present embodiment specifically comprises transaction list, lock Hash table and chain table; The concurrency control method that is arranged so that present embodiment of above-mentioned table can effectively be managed affairs and lock; Comprise index location, traversal search etc. apace; And then realized locking the judgement that whether has conflict to pointing to the same difference of operating position of treating, for providing, above-mentioned concurrency control method provides powerful support for.
Transaction list is preserved the current affairs transaction information of other affairs of concurrent execution in the system when carrying out; The principle that is inserted into team's head according to the late comer is inserted, the pointer of node, the meter pointer of chain table etc. before and after transaction information specifically comprises in affairs ID, transaction types, the sensing transaction list.Affairs of each startup all can be inserted into these affairs in the transaction list, promptly can the transaction information of these affairs be saved in the transaction list.Comprise a thread structure information in the transaction structure, wherein comprised event object, when affairs because of waiting for the lock hanging time-out, carry out this event object of thread waits of affairs; When the affairs granted lock thereby when being waken up, the thread of granted lock is provided with this event object.
As shown in Figure 2, each list item of lock Hash table is done corresponding to the gauge outfit of a chain table like this, makes that be that key calculates cryptographic hash with the file space of writing down in the lock construction number with page number, can navigate to corresponding chain table through corresponding lock Hash table list item.
The chain table is used for the affairs of association affairs tabulation record and locks the lock that writes down in the Hash table; As shown in Figure 2; Each chain table comprises a plurality of lock constructions; Each lock construction has write down information such as the file space number, page number, lock-bit figure, the shared bit of lock-bit figure (bit) number, lock type (shared lock, exclusive lock), lock sign (wait for, authorize), affiliated affairs of the data-base recording of the lock locking of waiting in line, and wherein the heap_no position of lock-bit figure is a sign of treating the operating position record.Each affairs is corresponding to a chain table, lock of the every application of affairs, and all chain is gone in corresponding chain table, and this chain table is preserved the lock that these affairs are held, and every pair of record is operated, and just can insert in the corresponding chain table of these affairs locking accordingly.A lock construction can be shared with other lock of the same type on one page; The lock that on other record, adds same type if desired; Only need in the chain table, to search to have or not similar lock; If find then, adopt single linked list promptly to solve hash collision like this, and saved memory headroom effectively with the corresponding heap_no bit of lock-bit figure.
As can be seen from the above description, the above embodiments of the present invention have improved the concurrency of affairs controls, have improved concurrent control effect.
Obviously, it is apparent to those skilled in the art that above-mentioned each module of the present invention or each step can realize with the general calculation device; They can concentrate on the single calculation element; Perhaps be distributed on the network that a plurality of calculation element forms, alternatively, they can be realized with the executable program code of calculation element; Thereby; Can they be stored in the memory storage and carry out, perhaps they are made into each integrated circuit modules respectively, perhaps a plurality of modules in them or step are made into the single integrated circuit module and realize by calculation element.Like this, the present invention is not restricted to any specific hardware and software combination.
The above is merely the preferred embodiments of the present invention, is not limited to the present invention, and for a person skilled in the art, the present invention can have various changes and variation.All within spirit of the present invention and principle, any modification of being done, be equal to replacement, improvement etc., all should be included within protection scope of the present invention.