Disclosure of Invention
In view of the above problems in the prior art, embodiments of the present invention provide an initialization loading method and device based on flashback query in database synchronization.
In a first aspect, an embodiment of the present invention provides a method for initializing and loading based on flashback query in database synchronization, including: acquiring a first final SCN of a current log of a source database, inquiring a system table of the source database, and if an active transaction related to the system table exists in the current active transaction and the initial SCN of the related active transaction is smaller than the first final SCN, finishing submitting the related active transaction; acquiring a second final SCN of a current log of a source database, sending the second final SCN to a target end data synchronization service, associating the second final SCN with a table to be loaded by the target end data synchronization service, flashing back to inquire a data mirror image of the table to be loaded at the second final SCN according to the second final SCN, sending the acquired data mirror image to the target end data synchronization service, synchronizing the data mirror image, and sending an operation of capturing the table to be loaded in a log stream to the target end data synchronization service; and if the SCN of the operation of the table to be loaded, which is captured in the log stream, is greater than the second final SCN, synchronizing the table to be loaded.
Further, on the basis of the contents of the above method embodiments, in the method for initializing and loading based on flashback query in database synchronization provided in the embodiments of the present invention, the system table includes: v $ TRANSACTION and/or V $ LOCKED _ OBJECT.
Further, on the basis of the content of the foregoing method embodiment, the method for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes, after sending the acquired data mirror to the target-end data synchronization service and before synchronizing the data mirror: and starting log analysis of the source database.
Further, on the basis of the content of the above method embodiment, the method for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes: and if the SCN of the operation of the table to be loaded, which is captured in the log stream, is less than or equal to the second final SCN, discarding the operation of the table to be loaded.
In a second aspect, an embodiment of the present invention provides an initial loading apparatus based on flashback query in database synchronization, including:
a related activity transaction submitting module, configured to obtain a first final SCN of a current log of a source database, query a system table of the source database, and if an activity transaction related to the system table exists in the current activity transaction and an initial SCN of the related activity transaction is smaller than the first final SCN, complete the submission of the related activity transaction;
a to-be-loaded table data mirror image acquisition module, configured to acquire a second final SCN of a current log of a source database, send the second final SCN to a target end data synchronization service, where the target end data synchronization service associates the second final SCN with a to-be-loaded table, and according to the second final SCN, perform a flash query on a data mirror image of the to-be-loaded table at the second final SCN, send the acquired data mirror image to the target end data synchronization service, synchronize the data mirror image, and send an operation of capturing the to-be-loaded table in a log stream to the target end data synchronization service;
and the table to be loaded synchronizing module is used for synchronizing the table to be loaded if the SCN of the operation captured by the table to be loaded in the log stream is greater than the second final SCN.
In a third aspect, an embodiment of the present invention provides an electronic device, including:
at least one processor; and
at least one memory communicatively coupled to the processor, wherein:
the memory stores program instructions executable by the processor, the processor invoking the program instructions to perform a flashback-query-based initial loading method in database synchronization provided by any one of the various possible implementations of the first aspect.
In a fourth aspect, an embodiment of the present invention provides a non-transitory computer-readable storage medium storing computer instructions for causing a computer to execute a flashback query-based initial loading method in database synchronization provided in any one of various possible implementations of the first aspect.
According to the method and the device for initial loading based on the flashback query in the database synchronization, provided by the embodiment of the invention, the data mirror image of the database at a certain moment is obtained through a flashback query mechanism, and the transition from the initialization to the data synchronization is realized by combining the SCN in the log stream, so that the consistency of the source end database and the target end database and the application stability of the source end database can be ensured on the premise of avoiding an S lock on a table to be loaded.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention. In addition, technical features of various embodiments or individual embodiments provided by the invention can be arbitrarily combined with each other to form a feasible technical solution, but must be realized by a person skilled in the art, and when the technical solution combination is contradictory or cannot be realized, the technical solution combination is not considered to exist and is not within the protection scope of the present invention.
At present, a method for initializing data from a source database to a destination database without using an S-lock on a table to be loaded needs to be found, and the technical problem that the application of the source database is greatly affected when data is loaded in the prior art is solved. To achieve the object, an embodiment of the present invention provides an initial loading method based on flashback query in database synchronization, and referring to fig. 1, the method includes:
101. acquiring a first final SCN of a current log of a source database, inquiring a system table of the source database, and if an active transaction related to the system table exists in the current active transaction and the initial SCN of the related active transaction is smaller than the first final SCN, finishing submitting the related active transaction;
102. acquiring a second final SCN of a current log of a source database, sending the second final SCN to a target end data synchronization service, associating the second final SCN with a table to be loaded by the target end data synchronization service, flashing back to inquire a data mirror image of the table to be loaded at the second final SCN according to the second final SCN, sending the acquired data mirror image to the target end data synchronization service, synchronizing the data mirror image, and sending an operation of capturing the table to be loaded in a log stream to the target end data synchronization service; specifically, the Flashback Query (Flashback Query) provided in the embodiment of the present invention is one of four database Flashback functions provided by an ORACLE database (the four Flashback functions are: Flashback Query, Flashback deletion, Flashback archiving, and Flashback database respectively), the Flashback Query can be subdivided into a basic Flashback Query and a Flashback table, and the basic Flashback Query is mainly used for querying a database state in a past certain time period. The Oracle database will fetch the required revocation data (provided that revocation is available, i.e. the revocation data is not yet covered) for rollback, but this rollback is temporary and only visible for the current session.
The flashback table is used primarily to rollback a table to a point in time in the past. Similarly, the Oracle database queries the revocation segment first, extracts all changes after a certain time point passes, constructs an SQL statement for inverting the changes, and backs the SQL statement, and the flashback query is a single transaction. Specifically, enabling table flashback first supports row movement on the table, and flashback table operation may occur as follows: violating the database constraint, for example, if the user carelessly deletes the data in the child table, the user wants to perform rollback by using the flashback table technique, and just in the middle, the record corresponding to the data in the parent table is also deleted, in this case, the flashback table operation fails due to violating the foreign key constraint; revocation data fails, such as revocation data used to support a flashback operation is overwritten, in which case a flashback table operation will naturally fail; flashback cannot cross the DDL, i.e., there is an excessive change in the table structure between the flashback point and the current point, in which case the flashback operation will also fail.
103. And if the SCN of the operation of the table to be loaded, which is captured in the log stream, is greater than the second final SCN, synchronizing the table to be loaded.
Based on the content of the foregoing method embodiment, as an optional embodiment, in the method for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention, the system table includes: v $ TRANSACTION and/or V $ LOCKED _ OBJECT.
Based on the content of the foregoing method embodiment, as an optional embodiment, the method for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes, after sending the acquired data mirror to the target-end data synchronization service and before synchronizing the data mirror: and starting log analysis of the source database.
Based on the content of the foregoing method embodiment, as an optional embodiment, the method for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes: and if the SCN of the operation of the table to be loaded, which is captured in the log stream, is less than or equal to the second final SCN, discarding the operation of the table to be loaded.
According to the flash query-based initialization loading method in database synchronization provided by the embodiment of the invention, the data mirror image of the database at a certain moment is obtained through a flash query mechanism, and the transition from initialization to data synchronization is realized by combining the SCN in the log stream, so that the consistency of a source end database and a target end database and the application stability of the source end database can be ensured on the premise of avoiding an S lock on a table to be loaded.
In order to more clearly illustrate the essence of the technical solution of the present invention, on the basis of the above-mentioned embodiments, a detailed embodiment is proposed, which shows the overall view of the technical solution of the present invention. It should be noted that the detailed embodiment is only for embodying the technical essence of the present invention, and is not intended to limit the scope of the present invention, and those skilled in the art can obtain any combination type technical solution meeting the essence of the technical solution of the present invention by combining technical features based on the various embodiments of the present invention, and as long as the combination type technical solution can be practically implemented, the combination type technical solution is within the scope of the present patent.
The detailed embodiment of the invention provides an initial loading method based on flashback inquiry in database synchronization, taking a loading T table as an example, and comprising the following steps:
s1, acquiring the final SCN of the current log of the source database: get _ SYSTEM _ CHANGE _ NUMBER FROM DUAL;
s2, by querying the system tables V $ TRANSACTION and V $ LOCKED _ OBJECT of the source database, judging whether the starting SCN of the active TRANSACTION related to the tables in the current active TRANSACTION has a smaller TRANSACTION than the SCN obtained in S1, if yes, waiting for the TRANSACTIONs to be submitted and then continuing;
the main purpose of the above steps is to ensure that the transaction log related to the T table in the current database system can be read from this moment, because after the log synchronization is started, the log file position where the current SCN value is located is set as the starting point of the synchronization reading and analysis, and if not, a certain transaction related to the T table starts before the current SCN, then its modification operation cannot be analyzed, and the modification snapback function of the transaction cannot be obtained, resulting in data loss.
S3, the maximum SCN of the current log of the source database is obtained again, the SCN is sent to the target end data synchronization service, and the target end data synchronization service associates the SCN with the table to be loaded.
This step is effective in that, after waiting for a long transaction to be committed, the SCN obtained in S1 may be a long-past value, and if a flashback query is performed using the SCN, the probability that the rollback section is cleaned during the flashback query is greatly increased, and the flashback query is a data image obtained from the database at a certain time based on the historical data in the rollback section, and the rollback section is cleaned up to cause a flashback query to report an error. Therefore, retrieving the latest SCN value in the current source database helps to reduce the probability of error reporting of the flashback query.
And S4, utilizing the SCN value obtained in S3 to carry out flash-back query on the data mirror image of the table to be loaded in the SCN, and sending the obtained data to the target end for synchronous data storage. Query SQL used SELECT FROM AS SCN (;
and S5, after the initialization is completed, starting the log analysis of the source end to perform data synchronization, and sending the operation acquired by the table in the log stream to the target end to perform data synchronization.
S6, after the data synchronization of the target end receives the commit operation of the transaction, whether the SCN in the commit operation of the transaction is larger than the flashback SCN related to the table in S3 needs to be judged, if so, the operation of the table is synchronized; otherwise, the operation of the table in the current transaction is discarded, the transition from loading to synchronization is realized, and the initialization work of data synchronization is ensured to be completed under the condition that the application of the source database is not influenced.
The principle of this step is that the flashback query can obtain all the data that has been committed before the table flashback SCN, while the uncommitted transactions are invisible to the flashback query, so all transactions in the log stream that commit an SCN greater than flashback SCN need to be synchronized.
The detailed embodiment provided by the invention can not load any lock (such as an S lock or an X lock) on a table to be loaded, reduces the influence on the application of the source-end database during the initial loading of data to the minimum, then utilizes a flash mechanism of the ORACLE database to obtain a data mirror image of the database at a certain moment, and combines an SCN value in a log stream to realize the transition from the data initialization to the data synchronization, thereby ensuring the consistency of the source-end database and the target-end database and ensuring the stable operation of the application of the source-end database.
The implementation basis of the various embodiments of the present invention is realized by programmed processing performed by a device having a processor function. Therefore, in engineering practice, the technical solutions and functions thereof of the embodiments of the present invention can be packaged into various modules. Based on this practical situation, on the basis of the foregoing embodiments, an embodiment of the present invention provides an apparatus for initializing and loading based on flashback inquiries in database synchronization, where the apparatus is used to execute a method for initializing and loading based on flashback inquiries in database synchronization in the foregoing method embodiments. Referring to fig. 2, the apparatus includes:
a related activity transaction submitting module 201, configured to obtain a first final SCN of a current log of a source database, query a system table of the source database, and if an activity transaction related to the system table exists in the current activity transaction and an initial SCN of the related activity transaction is smaller than the first final SCN, complete the submission of the related activity transaction;
a to-be-loaded table data mirror image obtaining module 202, configured to obtain a second final SCN of a current log of a source database, send the second final SCN to a target end data synchronization service, where the target end data synchronization service associates the second final SCN with a to-be-loaded table, according to the second final SCN, perform a flash query on a data mirror image of the to-be-loaded table at the second final SCN, send the obtained data mirror image to the target end data synchronization service, perform synchronization on the data mirror image, and send an operation of capturing the to-be-loaded table in a log stream to the target end data synchronization service;
a table to be loaded synchronizing module 203, configured to synchronize the table to be loaded if the SCN of the operation captured by the table to be loaded in the log stream is greater than the second final SCN.
According to the initialization loading device based on the flashback query in the database synchronization, provided by the embodiment of the invention, the related active transaction submitting module, the data mirror image acquisition module of the table to be loaded and the synchronization module of the table to be loaded are adopted, the data mirror image of the database at a certain moment is acquired through a flashback query mechanism, and the transition from initialization to data synchronization is realized by combining with the SCN in the log stream, so that the consistency of a source end database and a target end database and the application stability of the source end database can be ensured on the premise of avoiding an S lock on the table to be loaded.
It should be noted that, the apparatus in the apparatus embodiment provided by the present invention may be used for implementing methods in other method embodiments provided by the present invention, except for implementing methods in the above method embodiments, and the difference is only that a corresponding function module is provided, and the principle of the apparatus embodiment is basically the same as that of the above apparatus embodiment provided by the present invention. For example:
based on the content of the foregoing device embodiment, as an optional embodiment, the device for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes: and the log analysis starting module is used for starting the log analysis of the source database.
Based on the content of the foregoing device embodiment, as an optional embodiment, the device for initializing and loading based on flashback query in database synchronization provided in the embodiment of the present invention further includes: and the operation module for discarding the table to be loaded is used for discarding the operation of the table to be loaded if the SCN of the operation captured by the table to be loaded in the log stream is less than or equal to the second final SCN.
The method of the embodiment of the invention is realized by depending on the electronic equipment, so that the related electronic equipment is necessarily introduced. To this end, an embodiment of the present invention provides an electronic apparatus, as shown in fig. 3, including: at least one processor (processor)301, a communication Interface (Communications Interface)304, at least one memory (memory)302 and a communication bus 303, wherein the at least one processor 301, the communication Interface 304 and the at least one memory 302 are configured to communicate with each other via the communication bus 303. The at least one processor 301 may call logic instructions in the at least one memory 302 to perform the following method: acquiring a first final SCN of a current log of a source database, inquiring a system table of the source database, and if an active transaction related to the system table exists in the current active transaction and the initial SCN of the related active transaction is smaller than the first final SCN, finishing submitting the related active transaction; acquiring a second final SCN of a current log of a source database, sending the second final SCN to a target end data synchronization service, associating the second final SCN with a table to be loaded by the target end data synchronization service, flashing back to inquire a data mirror image of the table to be loaded at the second final SCN according to the second final SCN, sending the acquired data mirror image to the target end data synchronization service, synchronizing the data mirror image, and sending an operation of capturing the table to be loaded in a log stream to the target end data synchronization service; and if the SCN of the operation of the table to be loaded, which is captured in the log stream, is greater than the second final SCN, synchronizing the table to be loaded.
Furthermore, the logic instructions in the at least one memory 302 may be implemented in software functional units and stored in a computer readable storage medium when sold or used as a stand-alone product. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. Examples include: acquiring a first final SCN of a current log of a source database, inquiring a system table of the source database, and if an active transaction related to the system table exists in the current active transaction and the initial SCN of the related active transaction is smaller than the first final SCN, finishing submitting the related active transaction; acquiring a second final SCN of a current log of a source database, sending the second final SCN to a target end data synchronization service, associating the second final SCN with a table to be loaded by the target end data synchronization service, flashing back to inquire a data mirror image of the table to be loaded at the second final SCN according to the second final SCN, sending the acquired data mirror image to the target end data synchronization service, synchronizing the data mirror image, and sending an operation of capturing the table to be loaded in a log stream to the target end data synchronization service; and if the SCN of the operation of the table to be loaded, which is captured in the log stream, is greater than the second final SCN, synchronizing the table to be loaded. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. Based on this recognition, each block in the flowchart or block diagrams may represent a module, a program segment, or a portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
In this patent, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.