Specific embodiment
In the term that the embodiment of the present application uses merely for the sake of for the purpose of describing particular embodiments, rather than limit this Shen
Please.The "an" of singular used in the application and claims, " described " and "the" are also intended to including most shapes
Formula, unless context clearly shows that other meanings.It is also understood that term "and/or" used herein refers to comprising one
A or multiple associated any or all of project listed may combine.
It will be appreciated that though various letters may be described using term first, second, third, etc. in the embodiment of the present application
Breath, but these information should not necessarily be limited by these terms.These terms are only used to for same type of information being distinguished from each other out.For example,
In the case where not departing from the application range, the first information can also be referred to as the second information, and similarly, the second information can also be with
The referred to as first information.Depending on context, in addition, used word " if " can be construed to " ... when " or
" when ... " or " in response to determination ".
It is proposed a kind of database switching method in the embodiment of the present application, this method can be applied to include application server,
The system of middleware and database, middleware are the relay devices between application server and database, are referred to as
Middleware equipment, middleware can be separately connected application server and database.In one example, application server, centre
Part and database can be deployed in the same equipment.In another example, application server, middleware and database can also be with
It is deployed in different equipment, as application server, middleware and database are deployed in independent equipment.It is shown in Figure 1, be
The application scenarios schematic diagram of the embodiment of the present application, the quantity of application server can be one or more, the quantity of middleware
It can be one, the quantity of database can be two or more, with no restrictions to this quantity.
In one example, middleware is the relay device between application server and database, middleware encapsulation
There is processing method relevant to the operating system of application server, communication protocol etc., so as to provide unification for database
Interface, so that application server haves no need to change database after the configuration of the bottoms such as replacement operation system and communication protocol
Program code, and enhance the portability of database.
Middleware can also manage the connection of database, mitigate the burden of database.Specifically, if not using middleware,
It when application server accesses database every time, is required to connect with Database one, when extensive application server is visited parallel
It when asking database, needs largely to connect with Database simultaneously, is the behaviour for expending time and system resource due to establishing connection
Make, and the parallel processing capability of database is limited, therefore, application server substantially reduces the access efficiency of database.It is intermediate
Part can be connect with Database multiple free time in advance, and these connections are kept and managed, when application server is logical
Cross Middleware Request access database when, the application server first with middleware establishes connection, then, middleware from database
Idle connection is selected in the connection of foundation, and application server is distributed into free time connection and is used, and application servers is waited to use
After, middleware recycles the connection, to improve application server to the access efficiency of database.
Wherein, above-mentioned database includes but is not limited to MySQL (My Structured Query Language, i.e. relationship
Type data base management system) database, the connection between above-mentioned middleware and database can include but is not limited to JDBC
(connection of Java Database Connectivity, java database).Certainly, above-mentioned MySQL and JDBC is the application
One example, with no restrictions to this database, connection.
In one example, the database before switching can be known as first database, the database after switching is known as
Second database.Before database switching, middleware asks operation after the operation requests for receiving application server transmission
It asks and is sent to first database, first database executes the operation requests, returns to the operation to application server by middleware
The implementing result of request.After database switching, middleware will be grasped after the operation requests for receiving application server transmission
Make request and be sent to the second database, the second database executes the operation requests, and being returned by middleware to application server should
The implementing result of operation requests.
Wherein, for the process flow before database switching, then: when application server needs to access database, answering
It needs to establish with middleware communication connection (subsequent to be known as connection 1) with server, and middleware needs and database (the i.e. first number
According to library) establish communication connection (subsequent to be known as connection 2).In application server using operation requests to the data in first database
When being operated, then the operation requests are sent to middleware by connection 1 by application server, and middleware is receiving the operation
After request, which can be sent to by first database by connection 2.First database is asked receiving the operation
After asking, the operation requests are executed, and return to the implementing result of the operation requests to the application server by middleware.
Wherein, for the process flow after database switching, then: being established and led to middleware due to application server
Letter connection (connect 1) therefore can not resettle connection between application server and middleware, middleware also need and
Second Database communication connection (subsequent to be known as connection 3).In application server using operation requests in the second database
Data when being operated, then the operation requests are sent to middleware by connection 1 by application server, and middleware is receiving
After the operation requests, which is sent to by the second database by connection 3.Second database is receiving the operation
After request, the operation requests are executed, and return to the implementing result of the operation requests to the application server by middleware.
In order to realize above-mentioned database handoff procedure, a kind of database switching method, the party are proposed in the embodiment of the present application
Method can be applied to middleware, shown in Figure 2, for the flow chart of the database switching method.
Step 201, the switching request that first database is sent is received.
In one example, first database is executing operation requests, during returning to implementing result, if this first number
Database service is no longer provided according to library, and (such as first database causes no longer to provide database clothes restarting, due to exception
Business), then first database can send switching request to middleware, which indicates that this first database no longer provides number
It is serviced according to library, middleware is needed to carry out database switching.
Wherein, middleware is after the switching request for receiving first database transmission, so that it may the after determining switching
Two databases, and connect with the second Database.And it is directed to that " middleware determines the second database after switching, and with second
The process of Database connection ", can include but is not limited to such as under type:
The information of the second database after switching can be carried in mode one, above-mentioned switching request is (such as the second database
Mark, IP address etc.), middleware can parse the information of the second database from the switching request, and in middleware and
Connection is established between two databases.Mode two, middleware are that first database selects the second database after switching (such as from first
In other databases except database randomly choose a database, with no restrictions to this selection mode), and middleware with
Connection is established between second database.
Wherein, middleware can also judge first database after the switching request for receiving first database transmission
In whether there is untreated completion operation requests;If it is, continuing to keep the connection between middleware and first database;
If it is not, then the connection between middleware and first database can be disconnected.
For example, within a certain period of time, application server sends operation requests 1- operation requests 3, middleware to middleware
After receiving these operation requests, these operation requests are sent to first database.First database is receiving operation
After requesting 1- operation requests 3, operation requests 1 are first carried out, after the completion of the execution of operation requests 1, are then taken by middleware to application
The implementing result for device return operation requests 1 of being engaged in.Then, first database executes operation requests 2, executes completion to operation requests 2
Afterwards, then the implementing result of operation requests 2 is returned to application server by middleware.Then, first database executes operation and asks
3 are asked, after the completion of the execution of operation requests 3, then returns to the implementing result of operation requests 3 to application server by middleware.
Wherein, above-mentioned " operation requests of untreated completion " refer to: the operation requests that have not carried out have been executed but have not been returned
The operation requests of implementing result.For example, if first database is currently executing operation requests 2, but do not returned to middleware also
The implementing result of operation requests 2 is returned, then there are the operation requests of untreated completion, i.e. aforesaid operations request 2 in first database
It is the operation requests of untreated completion with operation requests 3.
In order to realize the above process, middleware can also count the operation requests quantity sent to first database, with
And the implementing result quantity that first database returns.If operation requests quantity is identical as implementing result quantity, illustrate the first number
According to the operation requests that untreated completion is not present in library;If operation requests quantity is greater than implementing result quantity, illustrate the first number
According in library there are the operation requests of untreated completion, and the difference between operation requests quantity and implementing result quantity, that is,
The quantity of the operation requests of untreated completion.
In conclusion when, there are when the operation requests of untreated completion, keeping middleware by continuing in first database
Connection between first database enables first database that the implementing result of operation requests is sent to middleware, keeps away
The implementing result for exempting from operation requests is lost.Moreover, when the operation requests of untreated completion being not present in first database,
It just will disconnect the connection between middleware and first database.
Step 202, connection state information is got from first database, and the connection state information of first database is sent out
The second database is given, so that connection state information described in the second database purchase.
In one example, before disconnecting the connection between middleware and first database, middleware can be from first
Database gets connection state information.For the process of " getting connection state information from first database ", may include
But be not limited to such as under type: middleware sends connection status request message to first database;Then, middleware can receive
The response message for the connection status request message that one database returns, and can be parsed from the response message
The connection state information of first database.
Wherein, middleware and first database can arrange the format of connection status request message, response message, disappear to this
Format is ceased with no restrictions, as long as connection status request message is for requesting connection state information, response message for carrying connection
Status information.Based on this, middleware can send connection status request message to first database, and first database exists
After receiving this connection status request message, know this connection status request message for requesting the connection of first database
Therefore the connection state information of first database is added in response message by status information, and response message is sent to
Between part.Middleware is after receiving this response message, so that it may the connection of first database is parsed from the response message
Status information.
In one example, above-mentioned connection state information may include: that connection status variable (is referred to as ambient condition
Variable, link variable, state variable etc.), the assignment of connection status variable.Wherein, this connection state information is application service
The information that device and first database are negotiated, first database can execute the behaviour of application server according to this connection state information
It requests, and returns to implementing result to middleware.
In practical applications, variable may include global variable and local variable, global variable using two symbols as
Beginning, user can only access, can not assignment, and local variable is defined by user oneself, and life cycle is only in a batch processing
Interior effective, local variable, can when to local variable assignments by user oneself definition and assignment using a symbol as beginning
To use select or set.Above-mentioned connection status variable can be local variable, naturally it is also possible to and it is global variable, it is right
The type of this connection status variable is with no restrictions.
Wherein, global variable can include but is not limited to :@@IDENTITY, indicate that return is finally inserted capable identity column
Train value.@@ERROR indicates the error code for returning to the Transact-SQL sentence finally executed, is then zero without mistake.@@
ROWCOUNT, indicates to return the line number that is influenced by a upper sentence, any not return to capable sentence and set 0 for this variable.@@
DBTS indicates the timestamp value for returning to current database.@@SERVERNAME indicates the title for returning to local server.@@
LANGID indicates to return to currently used Language ID value.And so on, with no restrictions to this global variable.
Wherein, local variable (i.e. connection status variable) is by user oneself definition and assignment (the i.e. tax of connection status variable
Value), select or set can be used when to local variable assignments, above-mentioned local variable and assignment also just constitute connection
Status information.For example, set@name=Zhang San, indicates that local variable is " name ", the assignment of local variable is " Zhang San ".Example again
Such as, set@abc=123 indicates that local variable is " abc ", and the assignment of local variable is " 123 ".In another example set@names=
Utf8 indicates that local variable is " names ", and the assignment of local variable is " utf8 ".In another example set@autocommit=1, table
Show that local variable is " autocommit ", the assignment of local variable is " 1 ".And so on, with no restrictions to this local variable.
In one example, middleware is after getting connection state information from first database, so that it may pass through centre
The connection state information of first database is sent to the second database by the connection between part and the second database.Second data
Library can store the connection state information of first database after receiving the connection state information of first database, and rear
During continuous, the connection state information of first database is used.
Step 203, operation requests corresponding with first database are sent to the second database, so that the second database root
The operation requests are handled according to the connection state information of the first database.
In one example, for " operation requests corresponding with first database are sent to the second database by middleware "
Process, can include but is not limited to such as under type: mode one, middleware receiving application server transmission with first number
After the corresponding operation requests in library, the operation requests received are sent to the second database.Mode two, middleware are among this
Part inquires operation requests that are corresponding with first database but being also not issued to first database, and the operation that will be inquired
Request is sent to the second database.
For mode one, after middleware is connect with the second Database, when application server uses operation requests pair
When data in database are operated, which can be sent to middleware by application server, and middleware is connecing
After receiving the operation requests, the operation requests are no longer sent to first database, but pass through middleware and the second database
Between connection, which is sent to the second database.
For mode two, within a certain period of time, application server sends operation requests 1- operation requests 5 to middleware,
Middleware first sends out each operation requests in these operation requests of local cache, and successively after receiving these operation requests
Give first database.For example, operation requests 1 are first sent to first database by middleware, then operation requests 2 are sent to
Then operation requests 3 are sent to first database by first database, operation requests 4 are then sent to first database, so
Operation requests 5 are sent to first database afterwards.But, it is assumed that after operation requests 3 are sent to first database by middleware,
Before operation requests 4 are sent to first database by middleware, middleware receives the switching request of first database transmission,
In this way, operation requests 4 and operation requests 5 are just no longer sent to first database by middleware.But in middleware and the second number
After establishing connection according to library, middleware can inquire corresponding with first database from this middleware but also be not issued to first
The operation requests 4 and operation requests 5 of database, and the operation requests 4 inquired and operation requests 5 can be sent to the second number
According to library, without being destined to first database.
In one example, the second database can go out the first data after receiving operation requests from local search
The connection state information in library, and the operation requests are handled according to the connection state information of first database, and can lead to
Cross the implementing result that middleware returns to operation requests to the application server.
In conclusion in the embodiment of the present application, by the way that the connection state information of first database is sent to the second data
Both library handles the second database to operation requests according to the connection state information of first database, i.e.,
Connection state information is identical, avoids mistake occur to the implementing result of operation requests.
For example, middleware by the connection state information of first database (set@name=Zhang San, set@abc=123,
Set@names=utf8, set@autocommit=1) be sent to the second database after, the second database can use these companies
Status information is connect to handle operation requests.For example, being directed to " get abc " this operation requests, the second database can benefit
The operation requests are handled with connection state information " set@abc=123 ", and implementing result is " 123 ", therefore passed through
Between part to application server return implementing result " 123 ", still, if the second database does not store connection state information
" set@abc=123 ", it is NULL that will lead to, which can not handle " get abc " this operation requests or implementing result, is then led
Implementing result is caused mistake occur.
In another example for " get autocommit " this operation requests, the second database can use the connection shape
State information " set@autocommit=1 " handles the operation requests, and implementing result is " 1 ", therefore, the second database
Implementing result " 1 " is returned to application server by middleware, still, if the second database does not store connection state information
" set@autocommit=1 " will lead to that " get autocommit " this operation requests or implementing result can not be handled
For NULL, then implementing result is caused mistake occur.
In the above-described embodiments, operation requests can include but is not limited to SQL statement.Wherein, the SQL statement can be
Data query language sentence, data manipulation language sentence, Data Definition Language sentence, data control language sentence, issued transaction
Language statement, pointer control language statement etc., it is without limitation.
Based on the above-mentioned technical proposal, in the embodiment of the present application, when needing to be switched to the second database from first database,
Middleware can get connection state information from first database, and connection state information is sent to the second database, with
Make the second database purchase connection state information, and operation requests are handled according to the connection state information.In this way, can
To keep connection state information constant in database handoff procedure, i.e. the connection state information and first that the second database uses
The connection state information that database uses is identical, so that the success rate of database switching is promoted, it being capable of significantly more efficient realization number
It is connected according to library and keeps function.
Based on similarly applying conceiving with the above method, the embodiment of the present application also provides a kind of database switching device, should
Database switching device can be applied to middleware (being referred to as middleware equipment), as shown in figure 3, proposed for the application
The structure chart of database switching device, the database switching device include:
Receiving module 301, for receiving the switching request of first database transmission;
Module 302 is obtained, for getting connection state information from the first database;
Sending module 303, for the connection state information to be sent to the second database, so that the second database purchase
The connection state information;Operation requests corresponding with the first database are sent to second database, so that the
Two database roots are handled the operation requests according to the connection state information.
In one example, the database switching device further includes (not regarding out in figure):
Connection management module, for after getting connection state information, judging whether deposit in the first database
In the operation requests of untreated completion;If it is, keeping the connection between the middleware and the first database;If
It is no, then disconnect the connection between the middleware and the first database;
The connection management module, is also used to before the connection state information is sent to the second database, from institute
The information of the second database after parsing switching in switching request is stated, and between the middleware and second database
Establish connection;Alternatively, for the second database after first database selection switching, and in the middleware and described second
Connection is established between database.
In one example, the acquisition module 302, specifically for getting connection status from the first database
During information, connection status request message can be sent to the first database;Then, the first database is received
The response message for the connection status request message returned;Then, described first is parsed from the response message
The connection state information of database.
The sending module 303, specifically for will operation requests corresponding with the first database be sent to it is described
During second database, after the operation requests corresponding with the first database for receiving application server transmission,
The operation requests received are sent to second database;And/or
It inquires corresponding with the first database from the middleware but is also not issued to the first database
The operation requests inquired are sent to second database by operation requests.
In one example, the operation requests include: structured query language SQL statement;
The connection state information includes: the assignment of connection status variable, the connection status variable.
Based on similarly applying conceiving with the above method, the embodiment of the present application also provides a kind of middleware equipment, it is described in
Between part equipment include: receiver, processor and transmitter;Wherein, receiver, the switching for receiving first database transmission are asked
It asks;Processor, for getting connection state information from the first database;Transmitter, for believing the connection status
Breath is sent to the second database, so that connection state information described in the second database purchase;It will be corresponding with the first database
Operation requests be sent to second database so that second database root is according to the connection state information to the behaviour
Make request to be handled.
Based on similarly applying conceiving with the above method, a kind of machine readable storage Jie is also provided in the embodiment of the present application
Matter, the machine readable storage medium can be applied to middleware equipment, several meters are stored on the machine readable storage medium
The instruction of calculation machine, the computer instruction, which is performed, to be handled as follows:
Receive the switching request that first database is sent;Connection state information is got from the first database;And
The connection state information is sent to the second database, so that connection state information described in the second database purchase;Will with institute
It states the corresponding operation requests of first database and is sent to second database, so that second database root is according to the connection
Status information handles the operation requests.
Based on similarly applying conceiving with the above method, the embodiment of the present application also provides a kind of database switching system, institute
The system of stating includes initiating the first database of switching request, middleware, the second database being switched;
The first database, for sending switching request to the middleware;
The middleware, the switching request sent for receiving the first database, obtains from the first database
The second database is sent to connection state information, and by the connection state information;
Second database, for storing the connection state information received;
The middleware is also used to the corresponding operation requests of the first database being sent to the second database;
Second database is also used to handle the operation requests according to the connection state information.
In one example, during the middleware gets connection state information from the first database,
The middleware is also used to send connection status request message to the first database;The first database is received to return
The response message for the connection status request message;The company of the first database is parsed from the response message
Connect status information.
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.A kind of typically to realize that equipment is computer, the concrete form of computer can
To be personal computer, laptop computer, cellular phone, camera phone, smart phone, personal digital assistant, media play
In device, navigation equipment, E-mail receiver/send equipment, game console, tablet computer, wearable device or these equipment
The combination of any several equipment.
For convenience of description, it is divided into various units when description apparatus above with function to describe respectively.Certainly, implementing this
The function of each unit can be realized in the same or multiple software and or hardware when application.
It should be understood by those skilled in the art that, embodiments herein can provide as method, system or computer program
Product.Therefore, complete hardware embodiment, complete software embodiment or reality combining software and hardware aspects can be used in the application
Apply the form of example.Moreover, it wherein includes computer usable program code that the embodiment of the present application, which can be used in one or more,
The computer implemented in computer-usable storage medium (including but not limited to magnetic disk storage, CD-ROM, optical memory etc.)
The form of program product.
The application is referring to method, the process of equipment (system) and computer program product according to the embodiment of the present application
Figure and/or block diagram describe.It is generally understood that being realized by computer program instructions each in flowchart and/or the block diagram
The combination of process and/or box in process and/or box and flowchart and/or the block diagram.It can provide these computer journeys
Sequence instruct to general purpose computer, special purpose computer, Embedded Processor or other programmable data processing devices processor with
A machine is generated, so that the instruction generation executed by computer or the processor of other programmable data processing devices is used for
Realize the dress for the function of specifying in one or more flows of the flowchart and/or one or more blocks of the block diagram
It sets.
Moreover, these computer program instructions also can store be able to guide computer or other programmable datas processing set
In standby computer-readable memory operate in a specific manner, so that instruction stored in the computer readable memory generates
Manufacture including command device, the command device are realized in one process of flow chart or multiple processes and/or block diagram one
The function of being specified in a box or multiple boxes.
These computer program instructions can also be loaded into computer or other programmable data processing devices, so that counting
Series of operation steps are executed on calculation machine or other programmable devices to generate computer implemented processing, thus in computer
Or the instruction executed on other programmable devices is provided for realizing in one or more flows of the flowchart and/or block diagram
The step of function of being specified in one box or multiple boxes.
The above description is only an example of the present application, is not intended to limit this application.For those skilled in the art
For, various changes and changes are possible in this application.All any modifications made within the spirit and principles of the present application are equal
Replacement, improvement etc., should be included within the scope of the claims of this application.