Specific embodiment
In general, distributed transaction can execute parallel with other affairs.In a typical application scenarios, distributed thing
It is engaged in when carrying out write operation to the data in tables of data, other affairs concurrently can carry out read operation to the data.For example, point
Cloth affairs 1 carry out write operation to the data A in tables of data, and when data A is revised as data B, distributed transaction 2 can be with logarithm
Read operation is carried out according to B.
However, in practical applications, distributed transaction after, terminating state includes submitting and two kinds of rollback, works as knot
When pencil state is rollback, in order to guarantee the final consistency of distributed transaction, distributed transaction can be cancelled to the write operation of data,
There is asking for " dirty reading " in this way, will lead to the data invalid that other affairs are read in the data convert that will be modified before
Topic.
Specifically referring to Figure 1.In Fig. 1, t is time shaft, and distributed transaction 1 and distributed transaction 2 execute parallel.Specifically
Ground:
Distributed transaction 1 executes SQL1, is updated to a field of id=1 row in tables of data 1, and a field is updated by 10
It is 15.After the completion of update, distributed transaction 2 executes SQL2, a field of id=1 row in tables of data 1 is inquired, at this point, distributed
The data that affairs 2 inquire are 15.
Distributed transaction 1 leads to transaction rollback after execution for some reason, at this point, distributed transaction 1 will
To the update of a field of id=1 row in tables of data 1 between revocation, i.e., a word of id=1 row is reduced to 10 by 15 in tables of data 1.
At this point, the data inquired will be if distributed transaction 2 inquires a field of id=1 row in tables of data 1 again
10, with 15 inconsistent, the data invalids for causing 2 first time of distributed transaction to inquire inquired before, there is " dirty reading "
Problem.
When the problem of appearance " dirty reading ", although will not influence data final consistency (distributed transaction after rollback,
Can re-execute), still, it will lead to and read chaotic data, needle for a user, can bring certain interference to user.
To solve the above-mentioned problems, the embodiment of the present application provides a kind of method for reading data and device, this method comprises: connecing
The read operation instruction of the second affairs is received, the read operation instructs the aiming field for reading target record in target matrix,
The aiming field based on the first affairs write operation instruction modify, first affairs be distributed transaction, and with institute
The second affairs are stated to execute parallel;If first affairs are not carried out end, the aiming field is read from preset data table
Field value before modification, and the field value before the modification is returned into second affairs, the preset data table is used for
Before modifying according to the instruction of the write operation of first affairs to the aiming field, the field of the aiming field is stored
Value.
Technical solution provided by the embodiments of the present application executes write operation to the aiming field in tables of data in distributed transaction
Before, the field value of the aiming field is stored in preset data table, when receiving reading of other affairs to the aiming field
When operational order, if distributed transaction is not carried out end, the field value of the aiming field in preset data table can be returned
Other affairs are given, in this way, before distributed transaction is not carried out end distributed transaction can be being read just to avoid other affairs
In the data of modification, and then other affairs is avoided to read invalid data, avoids the occurrence of " dirty reading " problem.
In order to make those skilled in the art better understand the technical solutions in the application, below in conjunction with the application reality
The attached drawing in example is applied, the technical scheme in the embodiment of the application is clearly and completely described, it is clear that described implementation
Example is merely a part but not all of the embodiments of the present application.Based on the embodiment in the application, this field is common
The application protection all should belong in technical staff's every other embodiment obtained without creative efforts
Range.
Below in conjunction with attached drawing, the technical scheme provided by various embodiments of the present application will be described in detail.
Fig. 1 is the flow diagram of one embodiment method for reading data of the application.It the described method comprises the following steps.
S102: the read operation instruction of the second affairs is received, the read operation instruction is for reading target in target matrix
The aiming field of record, the aiming field are modified based on the write operation instruction of the first affairs, and first affairs are point
Cloth affairs, and executed parallel with second affairs.
In S102, receive the second affairs read operation instruction before, can with the following steps are included:
Firstly, receiving the write operation instruction of the first affairs;
Secondly, the field value of the aiming field of target record in target matrix is deposited before response write operation instruction
Storage is in preset data table.
The write operation instruction of first affairs can be used for carrying out writing behaviour to the aiming field of target record in target matrix
Make.After the write operation instruction for receiving the first affairs, before responding write operation instruction, it can be instructed based on the write operation
The field value of the aiming field of target record in target matrix is inquired, and the field value of the aiming field is stored in present count
According in table.Wherein, preset data tableau format can be identical as target data tableau format.
In one implementation, it is preferable that can be by the preset data table with the shape of cancel daily record (" undo log ")
Formula storage.
After the field value of aiming field is stored in preset data table, the write operation that can respond the first affairs refers to
It enables, i.e., is instructed based on the write operation, modify, be somebody's turn to do to the field value of the aiming field of target record in target matrix
The modified field value of aiming field.
After obtaining the modified field value of aiming field, it can receive the second affairs to target record in target matrix
Aiming field read operation instruction.
In the present embodiment, the second affairs can be distributed transaction, be also possible to local matter, not do specific limit here
It is fixed.
After the read operation instruction for receiving the second affairs, S104 can be executed.
S104: if first affairs are not carried out end, before reading the aiming field modification in preset data table
Field value, and the field value before the modification is returned into second affairs.
In S104, after the read operation instruction for receiving the second affairs, it can be determined that whether the first affairs, which execute, terminates.
If the first affairs are not carried out end, the aiming field can be read from the preset data table recorded in S102
Field value, and the field value is returned into the second affairs.Wherein, the field value read from preset data table is the first affairs pair
The aiming field modify before field value.
In this way, not allowing the second affairs to read the first affairs due to before the first affairs are not carried out end and modifying
Data, it can thus be avoided the second affairs read invalid data, and then avoid the problem that " dirty reading ".
If the execution of the first affairs terminates, the field value of aiming field can be read from target matrix, and will read
Field value return to the second affairs.Specifically:
If the end state of the first affairs is rollback, it can be said that bright, the first affairs fail to the modification of aiming field, this
When, it can be from the field value after the aiming field rollback for reading target record in target matrix, and by the field after the rollback
Value returns to the second affairs, wherein the field value after the rollback can be understood as before the first affairs modify to aiming field
Field value;
If the end state of the first affairs is to submit, it can be said that bright, the first affairs to the successfully modified of aiming field, this
When, the modified field value of aiming field of target record can be read from target matrix, and by the modified field
Value returns to the second affairs.
In this way, the second affairs can read effective data in the case where the execution of the first affairs terminates.
In the present embodiment, if the execution of the first affairs terminates, the aiming field stored in preset data table can also be emptied
Field value, to save memory space.
Technical solution provided by the embodiments of the present application executes write operation to the aiming field in tables of data in distributed transaction
Before, the field value of the aiming field is stored in preset data table, when receiving reading of other affairs to the aiming field
When operational order, if distributed transaction is not carried out end, the field value of the aiming field in preset data table can be returned
Other affairs are given, in this way, before distributed transaction is not carried out end distributed transaction can be being read just to avoid other affairs
In the data of modification, and then other affairs is avoided to read invalid data, avoids the occurrence of " dirty reading " problem.
Entire technical solution in order to facilitate understanding, may refer to Fig. 3.Fig. 3 is a kind of data of one embodiment of the application
The flow diagram of read method, embodiment shown in Fig. 3 the following steps are included:
S301: the write operation instruction of the first affairs is received, the write operation instruction is for remembering target in target matrix
The aiming field of record carries out write operation.
Wherein, the first affairs are distributed transaction.
S302: the field value of the aiming field is stored in preset data table.
In S302, after the write operation instruction for receiving the first affairs, the write operation instructions query target can be based on
The field value of the aiming field of target record in tables of data, and the field value inquired is stored in preset data table.Wherein,
Preset data tableau format can be identical as target data tableau format, it is preferable that preset data table can be with cancel daily record
Form stored.
S303: the write operation instruction is responded, modifies to the aiming field, obtains modified field value.
It, can writing based on the first affairs after the field value of aiming field is stored in preset data table in S303
Operational order modifies to the field value of aiming field.
S304: the read operation instruction of the second affairs is received, the read operation instruction is for reading in the target matrix
The aiming field of the target record.
Second affairs are the affairs executed parallel with first, specifically can be distributed transaction, are also possible to local matter,
Here it is not specifically limited.
S305: judging whether first affairs execute terminates.
If the first affairs are not carried out end, S306 can be executed;If the execution of the first affairs terminates, can execute
S307。
S306: the field value before reading the aiming field modification in the preset data table, and will be before the modification
Field value return to second affairs.
In this way, at the end of the first affairs are not carried out, due to not allowing the second affairs to read what the first affairs were being modified
Data it can thus be avoided the second affairs read invalid data, and then avoid the appearance of " dirty reading " problem.
After the field value before modifying aiming field returns to the second affairs, S309 can be executed.
S307: the field value of the aiming field is read from the target matrix, and the field value of reading is returned
To second affairs.
Specifically, if the end state of the first affairs is rollback, the field value for returning to the second affairs is the first affairs
Field value before modifying to aiming field;The end state of first affairs is to submit, then returns to the field of the second affairs
Value is the field value after the first affairs modify to aiming field.
After the field value of reading is returned to the second affairs, S308 can be executed.
S308: the field value of the aiming field stored in the preset data table is emptied.
S309: terminate.
Based on the content that above-mentioned S301 to S309 is recorded, carried out by the data before distributed transaction is executed write operation
It saves, before distributed transaction terminates, when other affairs read the data, returns to previously stored number to other affairs
According to the data that distributed transaction is being modified can be read to avoid other business, and then avoid the occurrence of " dirty reading " problem.
" dirty reading " problem how the embodiment of the present application solves in order to facilitate understanding, may refer to Fig. 4.Fig. 4 is the application
One embodiment method for reading data schematic diagram.It is that SQL is grasped with the write operation instruction and read operation instruction of affairs in Fig. 4
It is illustrated for vertical order.
In Fig. 4, t is time shaft, and distributed transaction 1 and distributed transaction 2 execute parallel.Specifically:
Distributed transaction 1 executes SQL1, and a field to id=1 row in tables of data 1 is needed to be updated.Before the update,
The field value 10 of a field of id=1 row in tables of data 1 can be stored in tables of data 2, tables of data 2 is with " undo log "
Form storage.
After being stored the field value 10 of a field, SQL1 can be responded, and the field value of a field is updated by 10
It is 15.
After executing SQL1, the distributed transaction 2 executed parallel with distributed transaction 1 executes SQL2, and SQL2 is used for
Inquire a field of id=1 row in tables of data 1.At this point, since distributed transaction 1 is also not carried out end, it can be from tables of data 2
The middle field value 10 read before the modification of a field, and return to distributed transaction 2, i.e., the data that distributed transaction 2 inquires are
10。
After distributed transaction 1 executes, it is assumed that there is rollback in distributed transaction 1 for some reason, then, and tables of data 1
The field value of a field of middle id=1 row will be rolled back to unmodified preceding field value, i.e. the field value of a field becomes 10 from 15.
At this point, if distributed transaction 2 executes SQL2 again, and inquire a field of id=1 row in tables of data 1, then, due to
Distributed transaction 1, which has executed, to be terminated, and therefore, the data for returning to distributed transaction 2 are 10, i.e. distributed transaction 2 inquires
Data are 10, consistent with the data inquired in 1 implementation procedure of distributed transaction.
Based on above-mentioned whole process it is recognized that while distributed transaction 1 the case where rollback occurs after execution, but
It is that distributed transaction 2 does not read invalid data.
It is above-mentioned that the application specific embodiment is described.Other embodiments are within the scope of the appended claims.
In some cases, the movement recorded in detail in the claims or step can be executed according to the sequence being different from embodiment
And desired result still may be implemented.In addition, process depicted in the drawing not necessarily require the particular order shown or
Person's consecutive order is just able to achieve desired result.In some embodiments, multitasking and parallel processing are also possible
Or it may be advantageous.
Fig. 5 is the structural schematic diagram of one embodiment electronic equipment of the application.Referring to FIG. 5, in hardware view, the electricity
Sub- equipment includes processor, optionally further comprising internal bus, network interface, memory.Wherein, memory may be comprising interior
It deposits, such as high-speed random access memory (Random-Access Memory, RAM), it is also possible to further include non-volatile memories
Device (non-volatile memory), for example, at least 1 magnetic disk storage etc..Certainly, which is also possible that other
Hardware required for business.
Processor, network interface and memory can be connected with each other by internal bus, which can be ISA
(Industry Standard Architecture, industry standard architecture) bus, PCI (Peripheral
Component Interconnect, Peripheral Component Interconnect standard) bus or EISA (Extended Industry Standard
Architecture, expanding the industrial standard structure) bus etc..The bus can be divided into address bus, data/address bus, control always
Line etc..Only to be indicated with a four-headed arrow in Fig. 5, it is not intended that an only bus or a type of convenient for indicating
Bus.
Memory, for storing program.Specifically, program may include program code, and said program code includes calculating
Machine operational order.Memory may include memory and nonvolatile memory, and provide instruction and data to processor.
Processor is from the then operation into memory of corresponding computer program is read in nonvolatile memory, in logical layer
Reading data device is formed on face.Processor executes the program that memory is stored, and is specifically used for executing following operation:
The read operation instruction of the second affairs is received, the read operation instruction is for reading target record in target matrix
Aiming field, the aiming field are modified based on the write operation instruction of the first affairs, and first affairs are distributed thing
Business, and executed parallel with second affairs;
Field if first affairs are not carried out end, before reading the aiming field modification in preset data table
Value, and the field value before the modification is returned into second affairs, the preset data table is used for according to described first
Before the write operation instruction of affairs modifies to the aiming field, the field value of the aiming field is stored.
The method that reading data device disclosed in the above-mentioned embodiment illustrated in fig. 5 such as the application executes can be applied to processor
In, or realized by processor.Processor may be a kind of IC chip, the processing capacity with signal.It was realizing
Each step of Cheng Zhong, the above method can be complete by the integrated logic circuit of the hardware in processor or the instruction of software form
At.Above-mentioned processor can be general processor, including central processing unit (Central Processing Unit, CPU),
Network processing unit (Network Processor, NP) etc.;It can also be digital signal processor (Digital Signal
Processor, DSP), it is specific integrated circuit (Application Specific Integrated Circuit, ASIC), existing
Field programmable gate array (Field-Programmable Gate Array, FPGA) or other programmable logic device are divided
Vertical door or transistor logic, discrete hardware components.It may be implemented or execute and is in the embodiment of the present application disclosed each
Method, step and logic diagram.General processor can be microprocessor or the processor is also possible to any conventional place
Manage device etc..The step of method in conjunction with disclosed in the embodiment of the present application, can be embodied directly in hardware decoding processor and execute
At, or in decoding processor hardware and software module combination execute completion.Software module can be located at random access memory,
This fields such as flash memory, read-only memory, programmable read only memory or electrically erasable programmable memory, register maturation
In storage medium.The storage medium is located at memory, and processor reads the information in memory, completes above-mentioned side in conjunction with its hardware
The step of method.
The method that the electronic equipment can also carry out Fig. 2 to Fig. 4, and realize the implementation shown in Fig. 2 to Fig. 4 of reading data device
Function in example, details are not described herein for the embodiment of the present application.
Certainly, other than software realization mode, other implementations are not precluded in the electronic equipment of the application, for example patrol
Collect device or the mode of software and hardware combining etc., that is to say, that the executing subject of following process flow is not limited to each patrol
Unit is collected, hardware or logical device are also possible to.
The embodiment of the present application also proposed a kind of computer readable storage medium, the computer-readable recording medium storage one
A or multiple programs, the one or more program include instruction, and the instruction is when by the portable electronic including multiple application programs
When equipment executes, the method that the portable electronic device can be made to execute Fig. 2 to embodiment illustrated in fig. 4, and be specifically used for executing
It operates below:
The read operation instruction of the second affairs is received, the read operation instruction is for reading target record in target matrix
Aiming field, the aiming field are modified based on the write operation instruction of the first affairs, and first affairs are distributed thing
Business, and executed parallel with second affairs;
Field if first affairs are not carried out end, before reading the aiming field modification in preset data table
Value, and the field value before the modification is returned into second affairs, the preset data table is used for according to described first
Before the write operation instruction of affairs modifies to the aiming field, the field value of the aiming field is stored.
Fig. 6 is the structural schematic diagram of one embodiment reading data device 60 of the application.Referring to FIG. 6, a kind of soft
In part embodiment, the reading data device 60 can include: receiving unit 61 and reading unit 62, in which:
Receiving unit 61 receives the read operation instruction of the second affairs, and the read operation instruction is for reading target matrix
The aiming field of middle target record, the aiming field are modified based on the write operation instruction of the first affairs, first thing
Business is distributed transaction, and is executed parallel with second affairs;
Reading unit 62 reads the aiming field if first affairs are not carried out end from preset data table
Field value before modification, and the field value before the modification is returned into second affairs, the preset data table is used for
Before modifying according to the instruction of the write operation of first affairs to the aiming field, the field of the aiming field is stored
Value.
Optionally, the reading data device 60 further includes processing unit 63, in which:
The receiving unit 61, before receiving the read operation instruction of the second affairs, receive first affairs writes behaviour
It instructs, the write operation instruction carries out writing behaviour for the aiming field to target record described in the target matrix
Make;
The field value of the aiming field is stored in by the processing unit 63 before responding the write operation instruction
In the preset data table.
Optionally, the preset data table is stored in the form of cancel daily record.
Optionally, the field value of the aiming field is being stored in the preset data table by the processing unit 63
Afterwards, the write operation requests are responded, are modified to the aiming field, modified field value is obtained.
Optionally, second affairs are distributed transaction or local matter.
Optionally, the reading unit 62, however, it is determined that the first affairs execution terminates, then:
If the end state of first affairs is rollback, the aiming field is read from the target matrix and is returned
Field value after rolling, and the field value after the rollback is returned into second affairs;
If the end state of first affairs is to submit, the aiming field is read from the target matrix and is repaired
Field value after changing, and the modified field value is returned into second affairs.
Optionally, the reading unit 62 is emptied and is deposited in the preset data table if first affairs execution terminates
The field value of the aiming field of storage.
The method that reading data device 60 provided by the embodiments of the present application can also carry out Fig. 2 to Fig. 4, and realize reading data
Device is in the function of Fig. 2 to embodiment illustrated in fig. 4, and details are not described herein for the embodiment of the present application.
In short, being not intended to limit the protection scope of the application the foregoing is merely the preferred embodiment of the application.
Within the spirit and principles of this application, any modification, equivalent replacement, improvement and so on should be included in the application's
Within protection scope.
System, device, module or the unit that above-described embodiment illustrates can specifically realize by computer chip or entity,
Or it is realized by the product with certain function.It is a kind of typically to realize that equipment is computer.Specifically, computer for example may be used
Think personal computer, laptop computer, cellular phone, camera phone, smart phone, personal digital assistant, media play
It is any in device, navigation equipment, electronic mail equipment, game console, tablet computer, wearable device or these equipment
The combination of equipment.
Computer-readable medium includes permanent and non-permanent, removable and non-removable media can be by any method
Or technology come realize information store.Information can be computer readable instructions, data structure, the module of program or other data.
The example of the storage medium of computer includes, but are not limited to phase change memory (PRAM), static random access memory (SRAM), moves
State random access memory (DRAM), other kinds of random access memory (RAM), read-only memory (ROM), electric erasable
Programmable read only memory (EEPROM), flash memory or other memory techniques, read-only disc read only memory (CD-ROM) (CD-ROM),
Digital versatile disc (DVD) or other optical storage, magnetic cassettes, tape magnetic disk storage or other magnetic storage devices
Or any other non-transmission medium, can be used for storage can be accessed by a computing device information.As defined in this article, it calculates
Machine readable medium does not include temporary computer readable media (transitory media), such as the data-signal and carrier wave of modulation.
It should also be noted that, the terms "include", "comprise" or its any other variant are intended to nonexcludability
It include so that the process, method, commodity or the equipment that include a series of elements not only include those elements, but also to wrap
Include other elements that are not explicitly listed, or further include for this process, method, commodity or equipment intrinsic want
Element.In the absence of more restrictions, the element limited by sentence "including a ...", it is not excluded that including described want
There is also other identical elements in the process, method of element, commodity or equipment.
Various embodiments are described in a progressive manner in the application, same and similar part between each embodiment
It may refer to each other, each embodiment focuses on the differences from other embodiments.Implement especially for system
For example, since it is substantially similar to the method embodiment, so being described relatively simple, related place is referring to embodiment of the method
Part illustrates.