Detailed Description
In general, distributed transactions may be executed in parallel with other transactions. In a typical application scenario, when a distributed transaction writes data in a data table, other transactions may read the data in parallel. For example, distributed transaction 1 writes data a in the data table and when modifying data a to data B, distributed transaction 2 may read data B.
However, in practical applications, after the distributed transaction is finished, the finishing state includes two states of commit and rollback, and when the finishing state is rollback, in order to ensure the final consistency of the distributed transaction, the write operation of the distributed transaction on the data is cancelled, that is, the data modified before is restored, so that the data read by other transactions is invalid, and the problem of dirty reading occurs.
Please refer to fig. 1 in detail. In fig. 1, t is a time axis, and distributed transaction 1 and distributed transaction 2 execute in parallel. Specifically:
distributed transaction 1 executes SQL1, updates the a field of id=1 row in data table 1, and updates the a field from 10 to 15. After the update is completed, distributed transaction 2 executes SQL2, and queries the a field of row id=1 in data table 1, at which point the data queried by distributed transaction 2 is 15.
After the end of the execution, the distributed transaction 1 causes the transaction to rollback for some reason, at this time, the distributed transaction 1 will cancel the update of the a field of the id=1 row in the data table 1, that is, the a word of the id=1 row in the data table 1 is restored from 15 to 10.
At this time, if the distributed transaction 2 again queries the a field of the id=1 row in the data table 1, the queried data will be 10, which is inconsistent with the previously queried data 15, so that the data queried by the distributed transaction 2 for the first time is invalid, and the problem of dirty reading occurs.
When the problem of dirty reading occurs, although the final consistency of the data is not affected (the distributed transaction is re-executed after rollback), chaotic data is read, and certain interference is brought to the user.
In order to solve the above problems, an embodiment of the present application provides a data reading method and apparatus, where the method includes: receiving a read operation instruction of a second transaction, wherein the read operation instruction is used for reading a target field of a target record in a target data table, the target field is modified based on a write operation instruction of a first transaction, and the first transaction is a distributed transaction and is executed in parallel with the second transaction; and if the first transaction is not executed, reading a field value before the modification of the target field from a preset data table, and returning the field value before the modification to the second transaction, wherein the preset data table is used for storing the field value of the target field before the modification of the target field according to a write operation instruction of the first transaction.
According to the technical scheme provided by the embodiment of the application, before the distributed transaction executes the write operation on the target field in the data table, the field value of the target field is stored in the preset data table, when the read operation instruction of other transactions on the target field is received, if the distributed transaction is not executed to be ended, the field value of the target field in the preset data table can be returned to the other transactions, so that the other transactions can be prevented from reading the data which is being modified by the distributed transaction before the distributed transaction is not executed to be ended, the other transactions can be prevented from reading invalid data, and the dirty reading problem is avoided.
In order to make the technical solution of the present application better understood by those skilled in the art, the technical solution of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application, and it is apparent that the described embodiments are only some embodiments of the present application, not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, shall fall within the scope of the application.
The following describes in detail the technical solutions provided by the embodiments of the present application with reference to the accompanying drawings.
FIG. 1 is a flow chart of a data reading method according to an embodiment of the application. The method comprises the following steps.
S102: and receiving a read operation instruction of a second transaction, wherein the read operation instruction is used for reading a target field of a target record in a target data table, the target field is modified based on a write operation instruction of a first transaction, and the first transaction is a distributed transaction and is executed in parallel with the second transaction.
In S102, before receiving the read operation instruction of the second transaction, the method may further include the steps of:
firstly, receiving a write operation instruction of a first transaction;
second, a field value of a target field of a target record in a target data table is stored in a preset data table before responding to a write operation instruction.
The write operation instruction of the first transaction may be used to write to a target field of a target record in a target data table. After receiving the write operation instruction of the first transaction, before responding to the write operation instruction, the field value of the target field of the target record in the target data table can be queried based on the write operation instruction, and the field value of the target field is stored in the preset data table. The format of the preset data table may be the same as that of the target data table.
In one implementation, the preset data table may preferably be stored in the form of an undo log ("undo log").
After the field value of the target field is stored in the preset data table, the field value of the target field of the target record in the target data table can be modified in response to the write operation instruction of the first transaction, namely based on the write operation instruction, so as to obtain the field value after the modification of the target field.
After obtaining the modified field value of the target field, a read operation instruction of the second transaction on the target field of the target record in the target data table may be received.
In this embodiment, the second transaction may be a distributed transaction or a local transaction, which is not specifically limited herein.
After receiving the read operation instruction of the second transaction, S104 may be performed.
S104: and if the first transaction is not executed, reading the field value before modification of the target field from a preset data table, and returning the field value before modification to the second transaction.
In S104, after receiving the read operation instruction of the second transaction, it may be determined whether the execution of the first transaction is ended.
If the first transaction is not executed, the field value of the target field may be read from the preset data table recorded in S102, and the field value may be returned to the second transaction. The field value read from the preset data table is the field value before the first transaction modifies the target field.
In this way, since the second transaction is not allowed to read the data being modified by the first transaction until the first transaction is not executed is finished, the second transaction can be prevented from reading invalid data, and thus the problem of dirty reading is avoided.
If the execution of the first transaction is finished, the field value of the target field may be read from the target data table, and the read field value may be returned to the second transaction. Specifically:
if the ending state of the first transaction is rollback, it may be stated that the modification of the target field by the first transaction fails, at this time, a field value after rollback of the target field of the target record may be read from the target data table, and the rolled-back field value is returned to the second transaction, where the rolled-back field value may be understood as a field value before the modification of the target field by the first transaction;
if the ending status of the first transaction is commit, it may be stated that the modification of the target field by the first transaction is successful, at this time, the modified field value of the target field of the target record may be read from the target data table, and the modified field value may be returned to the second transaction.
In this way, the second transaction can read valid data in the event that execution of the first transaction is completed.
In this embodiment, if the execution of the first transaction is ended, the field value of the target field stored in the preset data table may be cleared, so as to save the storage space.
According to the technical scheme provided by the embodiment of the application, before the distributed transaction executes the write operation on the target field in the data table, the field value of the target field is stored in the preset data table, when the read operation instruction of other transactions on the target field is received, if the distributed transaction is not executed to be ended, the field value of the target field in the preset data table can be returned to the other transactions, so that the other transactions can be prevented from reading the data which is being modified by the distributed transaction before the distributed transaction is not executed to be ended, the other transactions can be prevented from reading invalid data, and the dirty reading problem is avoided.
For ease of understanding the entire solution, reference may be made to fig. 3. Fig. 3 is a flow chart of a data reading method according to an embodiment of the application, and the embodiment shown in fig. 3 includes the following steps:
s301: and receiving a write operation instruction of the first transaction, wherein the write operation instruction is used for performing write operation on a target field of a target record in a target data table.
Wherein the first transaction is a distributed transaction.
S302: and storing the field value of the target field in a preset data table.
In S302, after receiving the write operation instruction of the first transaction, a field value of a target field of the target record in the target data table may be queried based on the write operation instruction, and the queried field value may be stored in the preset data table. The format of the preset data table may be the same as that of the target data table, and preferably, the preset data table may be stored in the form of a revocation log.
S303: and responding to the write operation instruction, and modifying the target field to obtain a modified field value.
In S303, after storing the field value of the target field in the preset data table, the field value of the target field may be modified based on the write operation instruction of the first transaction.
S304: and receiving a read operation instruction of a second transaction, wherein the read operation instruction is used for reading the target field of the target record in the target data table.
The second transaction is a transaction executed in parallel with the first transaction, and may be a distributed transaction or a local transaction, which is not particularly limited herein.
S305: and judging whether the execution of the first transaction is ended.
If the first transaction is not completed, S306 may be executed; if the first transaction execution ends, S307 may be executed.
S306: and reading the field value before modification of the target field from the preset data table, and returning the field value before modification to the second transaction.
Thus, when the first transaction is not executed and ends, the second transaction is not allowed to read the data which is being modified by the first transaction, so that the second transaction is prevented from reading invalid data, and the dirty reading problem is prevented.
After returning the field value before the target field modification to the second transaction, S309 may be performed.
S307: and reading the field value of the target field from the target data table, and returning the read field value to the second transaction.
Specifically, if the ending state of the first transaction is rollback, the field value returned to the second transaction is the field value before the first transaction modifies the target field; and if the ending state of the first transaction is commit, the field value returned to the second transaction is the field value after the first transaction modifies the target field.
After returning the read field value to the second transaction, S308 may be performed.
S308: and clearing the field value of the target field stored in the preset data table.
S309: and (5) ending.
Based on the contents described in S301 to S309, by storing the data before the distributed transaction performs the write operation, when the data is read by another transaction before the distributed transaction ends, the data stored before is returned to the other transaction, so that the data that is being modified by the distributed transaction can be prevented from being read by the other transaction, and the problem of dirty reading can be avoided.
To facilitate an understanding of the "dirty read" problem that embodiments of the present application address, reference may be made to FIG. 4. FIG. 4 is a schematic diagram of a data reading method according to an embodiment of the application. In fig. 4, a write operation instruction and a read operation instruction of a transaction are exemplified as SQL manipulation commands.
In fig. 4, t is a time axis, and distributed transaction 1 and distributed transaction 2 execute in parallel. Specifically:
distributed transaction 1 executes SQL1, requiring an update to the a field of row id=1 in data table 1. Before updating, the field value 10 of the a field of the id=1 row in the data table 1 may be stored in the data table 2, and the data table 2 is stored in the form of "undolog".
After storing the field value of the a field 10, SQL1 may be responded to and the field value of the a field updated from 10 to 15.
After execution of SQL1 ends, distributed transaction 2, which is executed in parallel with distributed transaction 1, executes SQL2, SQL2 being used to query the a field of row id=1 in data table 1. At this time, since the distributed transaction 1 has not been executed, the field value 10 before the modification of the a field is read from the data table 2, and returned to the distributed transaction 2, that is, the data queried by the distributed transaction 2 is 10.
After the distributed transaction 1 is executed, assuming that the distributed transaction 1 rolls back for some reason, the field value of the a field of the id=1 row in the data table 1 will roll back to the field value before being unmodified, that is, the field value of the a field is changed from 15 to 10.
At this time, if the distributed transaction 2 executes SQL2 again and queries the a field of the row id=1 in the data table 1, the data returned to the distributed transaction 2 is 10 because the execution of the distributed transaction 1 is completed, that is, the data queried by the distributed transaction 2 is 10, which is consistent with the data queried during the execution of the distributed transaction 1.
Based on the above overall process, although the distributed transaction 1 has rolled back after the execution ends, the distributed transaction 2 does not read invalid data.
The foregoing describes certain embodiments of the present application. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims can be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
Fig. 5 is a schematic structural view of an electronic device according to an embodiment of the present application. Referring to fig. 5, at the hardware level, the electronic device includes a processor, and optionally an internal bus, a network interface, and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory (non-volatile Memory), such as at least 1 disk Memory. Of course, the electronic device may also include hardware required for other services.
The processor, network interface, and memory may be interconnected by an internal bus, which may be an ISA (Industry Standard Architecture ) bus, a PCI (Peripheral Component Interconnect, peripheral component interconnect standard) bus, or EISA (Extended Industry Standard Architecture ) bus, among others. The buses may be classified as address buses, data buses, control buses, etc. For ease of illustration, only one bi-directional arrow is shown in FIG. 5, but not only one bus or type of bus.
And the memory is used for storing programs. In particular, the program may include program code including computer-operating instructions. The memory may include memory and non-volatile storage and provide instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory into the memory and then runs, and forms a data reading device on a logic level. The processor is used for executing the programs stored in the memory and is specifically used for executing the following operations:
receiving a read operation instruction of a second transaction, wherein the read operation instruction is used for reading a target field of a target record in a target data table, the target field is modified based on a write operation instruction of a first transaction, and the first transaction is a distributed transaction and is executed in parallel with the second transaction;
and if the first transaction is not executed, reading a field value before the modification of the target field from a preset data table, and returning the field value before the modification to the second transaction, wherein the preset data table is used for storing the field value of the target field before the modification of the target field according to a write operation instruction of the first transaction.
The method performed by the data reading apparatus disclosed in the embodiment of fig. 5 of the present application may be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or by instructions in the form of software. The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but also digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), field programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. The disclosed methods, steps, and logic blocks in the embodiments of the present application may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present application may be embodied directly in the execution of a hardware decoding processor, or in the execution of a combination of hardware and software modules in a decoding processor. The software modules may be located in a random access memory, flash memory, read only memory, programmable read only memory, or electrically erasable programmable memory, registers, etc. as well known in the art. The storage medium is located in a memory, and the processor reads the information in the memory and, in combination with its hardware, performs the steps of the above method.
The electronic device may also execute the methods of fig. 2 to 4 and implement the functions of the data reading apparatus in the embodiments shown in fig. 2 to 4, which are not described herein.
Of course, other implementations, such as a logic device or a combination of hardware and software, are not excluded from the electronic device of the present application, that is, the execution subject of the following processing flows is not limited to each logic unit, but may be hardware or a logic device.
The embodiments of the present application also provide a computer-readable storage medium storing one or more programs, the one or more programs comprising instructions, which when executed by a portable electronic device comprising a plurality of application programs, enable the portable electronic device to perform the method of the embodiments shown in fig. 2-4, and in particular to perform the operations of:
receiving a read operation instruction of a second transaction, wherein the read operation instruction is used for reading a target field of a target record in a target data table, the target field is modified based on a write operation instruction of a first transaction, and the first transaction is a distributed transaction and is executed in parallel with the second transaction;
and if the first transaction is not executed, reading a field value before the modification of the target field from a preset data table, and returning the field value before the modification to the second transaction, wherein the preset data table is used for storing the field value of the target field before the modification of the target field according to a write operation instruction of the first transaction.
Fig. 6 is a schematic diagram of a data reading device 60 according to an embodiment of the present application. Referring to fig. 6, in a software implementation, the data reading device 60 may include: a receiving unit 61 and a reading unit 62, wherein:
a receiving unit 61, configured to receive a read operation instruction of a second transaction, where the read operation instruction is configured to read a target field of a target record in a target data table, where the target field is modified based on a write operation instruction of a first transaction, where the first transaction is a distributed transaction and is executed in parallel with the second transaction;
and a reading unit 62, configured to read, if the first transaction is not executed, a field value before modification of the target field from a preset data table, and return the field value before modification to the second transaction, where the preset data table is used to store the field value of the target field before modification of the target field according to a write operation instruction of the first transaction.
Optionally, the data reading device 60 further comprises a processing unit 63, wherein:
the receiving unit 61 receives a write operation instruction of the first transaction before receiving a read operation instruction of the second transaction, where the write operation instruction is used to perform a write operation on the target field of the target record in the target data table;
the processing unit 63 stores the field value of the target field in the preset data table before responding to the write operation instruction.
Optionally, the preset data table is stored in the form of a revocation log.
Optionally, after storing the field value of the target field in the preset data table, the processing unit 63 modifies the target field in response to the write operation request to obtain a modified field value.
Optionally, the second transaction is a distributed transaction or a local transaction.
Optionally, the reading unit 62, if determining that the first transaction execution ends,:
if the ending state of the first transaction is rollback, reading the field value of the rolled back target field from the target data table, and returning the field value of the rolled back to the second transaction;
and if the ending state of the first transaction is commit, reading the modified field value of the target field from the target data table, and returning the modified field value to the second transaction.
Optionally, the reading unit 62 clears the field value of the target field stored in the preset data table if the execution of the first transaction is ended.
The data reading device 60 provided in the embodiment of the present application may also execute the methods of fig. 2 to 4, and implement the functions of the data reading device in the embodiment shown in fig. 2 to 4, which are not described herein again.
In summary, the foregoing description is only of the preferred embodiments of the present application, and is not intended to limit the scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the protection scope of the present application.
The system, apparatus, module or unit set forth in the above embodiments may be implemented in particular by a computer chip or entity, or by a product having a certain function. One typical implementation is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
Computer readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of storage media for a computer include, but are not limited to, phase change memory (PRAM), static Random Access Memory (SRAM), dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), read Only Memory (ROM), electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium, which can be used to store information that can be accessed by a computing device. Computer-readable media, as defined herein, does not include transitory computer-readable media (transmission media), such as modulated data signals and carrier waves.
It should also be noted that 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 one … …" does not exclude the presence of other like elements in a process, method, article or apparatus that comprises the element.
The embodiments of the present application are described in a progressive manner, and the same and similar parts of the embodiments are all referred to each other, and each embodiment is mainly described in the differences from the other embodiments. In particular, for system embodiments, since they are substantially similar to method embodiments, the description is relatively simple, as relevant to see a section of the description of method embodiments.