The method of a kind of distributed data base internal adjustment connection and device
Technical field
The present invention relates to database field, particularly to method and the device of the connection of a kind of distributed data base internal adjustment.
Background technology
It is the resource of a kind of key, limited and costliness that data base connects, and this point embodies especially prominent in the Web page application program of multi-user.The management that data base is connected can have influence on retractility and the vigorousness of whole application program, has influence on the performance indications of program.Database connection pool puts forward for this problem just.Database connection pool is responsible for distribution, management and release data base and is connected, and its permission application program is reused an existing data base and connected, and is or else re-establish one;The releasing idling time exceedes data base's connection of maximum idle time and avoids the data base caused because not discharging data base's connection to connect omission.This technology can significantly improve the performance to database manipulation.
Connection pool basic idea is when system initialization, data base is connected and is stored in internal memory as object, when user needs to access data base, not sets up a new connection, but takes out an idle connecting object set up from connection pool.After use, user is also not by connection closed, but connection is put back in connection pool, accesses for next one request and uses.And establishment of connection, disconnection are all managed by connection pool self.At the same time it can also be control connection pool initially connects number, the bound number of connection and the maximum access times of each connection, maximum idle time etc. by arranging the parameter of connection pool.Quantity, the service condition that data base connects can also be monitored by the administrative mechanism of himself.
At present, database connection pool substantially can accomplish some content following:
1. having a configuration file, configuration file generally has the parameter confession configurations such as Smallest connection number, maximum number of connections, maximum idle time.
2. according to the Smallest connection number in configuration file, create the connection of this quantity when system is initial, be placed in internal memory for user.
3. when user needs to connect, available connection is searched from connection pool, if can find, then be directly returned to user, if can not find, first look at whether the connection quantity in now connection pool reaches the upper limit of the maximum number of connections of configuration in configuration file, if it has not, so can continue to create new connection, and this connection is returned to user, if having reached the upper limit, then user can only enter waiting list and waits that existing connection is released.
4. make to be finished connection as user, return to connection pool, if now without waiting for user in connection pool, being then directly added into connection queue, if there being wait user, being then distributed directly to user.
5. when the connection quantity in connection pool exceedes the Smallest connection number of configuration, then the maximum idle time according to configuration, after exceeding maximum idle time, the unnecessary connection of auto-destruct.
But, in some above-mentioned content, data base is when creating new connection, and according to program needs, it is possible to can initialize some data, if initialization data amount is relatively larger, initialization time is longer, when height is concurrent, network congestion will certainly be caused serious.For this kind of situation, inventors suggest that the service condition of a kind of intelligent predicting network, create the connection of pre-quantitation in advance, to avoid when without going again when connecting available to create new connection, alleviate the phenomenon of network congestion.
Summary of the invention
The technical problem to be solved in the present invention is to provide method and the device that a kind of distributed data base internal adjustment connects, connection number in described method record unit time, connection quantity in prediction a period of time, when next time arrives this time, automatically create the connection of pre-quantitation, to avoid when without going again when connecting available to create new connection.
In order to solve the problems referred to above, the invention provides a kind of method that distributed data base internal adjustment connects, including:
The connection quantity of the connection created in data base in record first time period;
The connection quantity in described first time period according to record, adjusts the connection quantity connected in data base described in the second time period.
Wherein, the connection quantity of the connection created in data base in described record first time period, particularly as follows:
Described first time period is divided into multiple sub-time period;
Recorded in the metadata table being pre-created by the connections quantity of connection created in data base in each described sub-time period, in described metadata table, at least record has: the end time of the mark of time period, connection quantity, the initial time of connection and connection.
Wherein, the described connection quantity by the connection created in data base in each described sub-time period recorded in the metadata table being pre-created, and specifically includes:
If the described sub-time period does not have record in described metadata table, then automatically generate the time period mark of described sub-time period, and by the connection quantity of connection created in the data base in the described sub-time period, the initial time of described sub-time period, the end time of described sub-time period and described time period identification record to described metadata table;
If there has been the record of correspondence the described sub-time period in described metadata table, then take out the sub-time period recorded described in described metadata table, calculate the meansigma methods of the connection quantity in the described sub-time period and the connection quantity in the recorded sub-time period, and the connection quantity in the sub-time period recorded in described metadata table is replaced with the meansigma methods of calculated connection quantity.
Wherein, described according to the connection quantity in the described first time period of record, adjust the connection quantity connected in described data base within the second time period, specifically include:
The connection quantity in described first time period according to record, obtains and needs the estimation of the connection created to connect quantity in described second time period in described data base;
Estimate that connecting quantity adjusts the connection quantity connected in described data base in described second time period according to described.
Wherein, described according to the described connection quantity estimated and connect in described data base in connection quantity adjustment described second time period, specifically include:
If the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and the connections quantity connected in described data base in described second time period is connected the difference of quantity less than or equal to default difference with described estimation, then do not adjust in described second time period connection quantity of connection in described data base;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and with described, the connections quantity connected in described data base in described second time period estimates that the difference being connected quantity is more than default difference, then close in described second time period connection quantity of connection in described data base and exceed the connection of described preset difference value part;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity less than described, then continue establishment and connect until the connection quantity connected in described data base in described second time period connects quantity equal to described estimation.
Additionally, present invention also offers the device that a kind of distributed data base internal adjustment connects, including:
Logging modle, for recording the connection quantity of the connection created in data base in first time period;
Adjusting module, for the connection quantity in the described first time period according to record, adjusts the connection quantity connected in data base described in the second time period.
Wherein, described logging modle is used for:
Described first time period is divided into multiple sub-time period;
Recorded in the metadata table being pre-created by the connections quantity of connection created in data base in each described sub-time period, in described metadata table, at least record has: the end time of the mark of time period, connection quantity, the initial time of connection and connection.
Wherein, described logging modle specifically for:
If the described sub-time period does not have record in described metadata table, then automatically generate the time period mark of described sub-time period, and by the connection quantity of connection created in the data base in the described sub-time period, the initial time of described sub-time period, the end time of described sub-time period and described time period identification record to described metadata table;
If there has been the record of correspondence the described sub-time period in described metadata table, then take out the sub-time period recorded described in described metadata table, calculate the meansigma methods of the connection quantity in the described sub-time period and the connection quantity in the recorded sub-time period, and the connection quantity in the sub-time period recorded in described metadata table is replaced with the meansigma methods of calculated connection quantity.
Wherein, described adjusting module is used for:
The connection quantity in described first time period according to record, obtains and needs the estimation of the connection created to connect quantity in described second time period in described data base;Estimate that connecting quantity adjusts the connection quantity connected in described data base in described second time period according to described.
Wherein, described adjusting module specifically for:
If the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and the connections quantity connected in described data base in described second time period is connected the difference of quantity less than or equal to default difference with described estimation, then do not adjust in described second time period connection quantity of connection in described data base;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and with described, the connections quantity connected in described data base in described second time period estimates that the difference being connected quantity is more than default difference, then close in described second time period connection quantity of connection in described data base and exceed the connection of described preset difference value part;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity less than described, then continue establishment and connect until the connection quantity connected in described data base in described second time period connects quantity equal to described estimation.
Having the beneficial effect that of the such scheme of the present invention: the connection quantity of the connection created in data base in record certain time period, on this basis, connection quantity in prediction subsequent time period, when next time, this time arrived, connection quantity according to prediction, data base adjusts the connection quantity that subsequent time period connects, to avoid when without going when connecting available to create new connection again.
Accompanying drawing explanation
Fig. 1 represents the step schematic diagram of distributed data base internal adjustment method of attachment in the embodiment of the present invention;
Fig. 2 represents the method schematic diagram of the connection quantity recording the connection created in data base in first time period in the embodiment of the present invention;
Fig. 3 represents the method schematic diagram adjusting the connection quantity connected in data base in the second time period in the embodiment of the present invention;
Fig. 4 represents the device block diagram that in the embodiment of the present invention, distributed data base internal adjustment connects.
Detailed description of the invention
For making the technical problem to be solved in the present invention, technical scheme and advantage clearly, it is described in detail below in conjunction with the accompanying drawings and the specific embodiments.
The present invention is directed to current data base when creating new connection, according to program needs, some data may be initialized, if initialization data quantity is bigger, and initialization time is longer, when height is concurrent, the problem that network heavy congestion will certainly be caused, it is provided that the method for a kind of distributed data base internal adjustment connection and device.
As it is shown in figure 1, embodiments provide a kind of method that distributed data base internal adjustment connects, including:
Step S11: the connection quantity m of the connection created in data base in record first time period;
In an embodiment of the present invention, the unit of first time period can be: sky, week, the moon, season etc., correspondingly, namely records the connection quantity of the connection created in some day, a certain week, certain January or scheme data base's first quarter.Of course, it should be understood that in order to improve adjust connect accuracy, the unit of first time period be preferably " my god ".
Step S12: according to the connection quantity m in the described first time period of record, adjust the connection quantity connected in data base described in the second time period.
In an embodiment of the present invention, the unit of the second time period and the unit of first time period adapt, if namely the unit of first time period is " my god ", the unit of the second time period be also " my god ".
In the above embodiment of the present invention, the connection quantity of the connection created in data base in record certain time period, on this basis, connection quantity in prediction subsequent time period, when next time, this time arrived, connection quantity according to prediction, data base adjusts the connection quantity that subsequent time period connects, to avoid when without going when connecting available to create new connection again.
Specifically, step S11 records the connection quantity m of the connection created in data base in first time period, as in figure 2 it is shown, include:
Step S111: described first time period is divided into multiple sub-time period;
Step S112: recorded in the metadata table being pre-created by the connections quantity of connection created in data base in each described sub-time period, in described metadata table, at least record has: the end time of the mark ID of time period, connection quantity, the initial time of connection and connection.
In the present embodiment, by hour in units of described first time period (such as in units of sky) is divided into multiple sub-time period (such as by hour in units of), data base is pre-created a metadata table, wherein table includes four field: ID, connects quantity, the initial time of connection and end time.When within one hour, terminating, get this hour interior connection quantity, if record not corresponding with this hour in metadata table, then automatically generating a record has ID in this hour, connects the record of the starting and ending time of quantity, connection, is inserted directly in metadata table;If there has been the record of correspondence this hour in metadata table, then taking out the record in metadata table, the connection quantity interior with this hour is added averages, and replaces the corresponding record in metadata table by this meansigma methods.Specifically method is as follows:
If the described sub-time period does not have record in described metadata table, then automatically generate the time period mark of described sub-time period, and by the connection quantity of connection created in the data base in the described sub-time period, the initial time of described sub-time period, the end time of described sub-time period and described time period identification record to described metadata table;
If there has been the record of correspondence the described sub-time period in described metadata table, then take out the sub-time period recorded described in described metadata table, calculate the meansigma methods of the connection quantity in the described sub-time period and the connection quantity in the recorded sub-time period, and the connection quantity in the sub-time period recorded in described metadata table is replaced with the meansigma methods of calculated connection quantity.
Specifically, according to the connection quantity m in the described first time period recorded described in step S12, the connection quantity connected in described data base within the second time period is adjusted, as it is shown on figure 3, include:
Step S121: according to the connection quantity m in the described first time period of record, obtains and needs the estimation of the connection created to connect quantity n in described second time period in described data base;
Step S122: estimate that connecting quantity n adjusts the connection quantity connected in described data base in described second time period according to described, specifically,
First determine whether whether the connection quantity connected in described data base in described second time period is estimated to connect quantity n more than described, if the connection quantity being judged as in the second time period is estimated to connect quantity n more than described, and the connection quantity in described second time period with the described difference estimating to be connected quantity n less than or equal to default difference K, then do not adjust the connection quantity connected in described data base in described second time period, contrary, connection quantity in described second time period is connected the difference of quantity n more than default difference K with described estimation, then close the connection quantity connected in described data base in described second time period and exceed the connection of described preset difference value K part;
If the connection quantity being judged as in described second time period is estimated to connect quantity n less than described, then continue to create connection until the connection quantity connected in described data base in described second time period estimates connection quantity n equal to described.
It should be pointed out that, about the connection quantity m of record, can be updated along with the change of every day, enable a system to adjust dynamically when one relatively average.
For the clearer above-mentioned purpose that realizes, the present embodiment provides a kind of computational methods obtaining and needing the estimation of the connection created to connect quantity n in the second time period in described data base:
The connection quantity m in described first time period according to record, is designated as λ by m, according to Poisson distribution herein(prove according to experiment with x=λ ± k, when K=10 is more effective interval selection) within number as the parameter of Poisson distribution, determine that according to Poisson distribution the probability connecting quantity X generation is maximum, then connect quantity n by connecting the quantity X estimation being designated as in the present embodiment.
Connection quantity in second time period has the connection more than n, and both differences are less than or equal to K=10, then without adjusting the connection quantity in the second time period, if both differences are more than 10, then close in the second time period and connect beyond the part of 10;If the connection quantity in the second time period is less than n, then continues to create new connection and reach the value of n until the connection quantity in the second time period.
Additionally, as shown in Figure 4, present invention also offers the device that a kind of distributed data base internal adjustment connects, including:
Logging modle 31, for recording the connection quantity of the connection created in data base in first time period;
Specifically, described logging modle 31 is used for:
Described first time period is divided into multiple sub-time period;
Recorded in the metadata table being pre-created by the connections quantity of connection created in data base in each described sub-time period, in described metadata table, at least record has: the end time of the mark of time period, connection quantity, the initial time of connection and connection.
Should be noted that, if the described sub-time period does not have record in described metadata table, then automatically generate the time period mark of described sub-time period, and by the connection quantity of connection created in the data base in the described sub-time period, the initial time of described sub-time period, the end time of described sub-time period and described time period identification record to described metadata table;
If there has been the record of correspondence the described sub-time period in described metadata table, then take out the sub-time period recorded described in described metadata table, calculate the meansigma methods of the connection quantity in the described sub-time period and the connection quantity in the recorded sub-time period, and the connection quantity in the sub-time period recorded in described metadata table is replaced with the meansigma methods of calculated connection quantity.
Adjusting module 32, for the connection quantity in the described first time period according to record, adjusts the connection quantity connected in data base described in the second time period.
Specifically, described adjusting module 32 is used for:
The connection quantity in described first time period according to record, obtains and needs the estimation of the connection created to connect quantity in described second time period in described data base;Estimate that connecting quantity adjusts the connection quantity connected in described data base in described second time period according to described.
Should be noted that, if the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and the connections quantity connected in described data base in described second time period is connected the difference of quantity less than or equal to default difference with described estimation, then do not adjust in described second time period connection quantity of connection in described data base;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity more than described, and with described, the connections quantity connected in described data base in described second time period estimates that the difference being connected quantity is more than default difference, then close in described second time period connection quantity of connection in described data base and exceed the connection of described preset difference value part;
If the connection quantity connected in described data base in described second time period is estimated to connect quantity less than described, then continue establishment and connect until the connection quantity connected in described data base in described second time period connects quantity equal to described estimation.
The above embodiment of the present invention, the service condition of intelligent predicting network, data base creates connection in advance, effectively alleviates the phenomenon of network congestion.
The method of the distributed data base internal adjustment connection of the embodiment of the present invention and device, the connection quantity of the connection created in data base in record certain time period, on this basis, connection quantity in prediction subsequent time period, when next time, this time arrived, connection quantity according to prediction, data base adjusts the connection quantity connected in subsequent time period, to avoid when without going when connecting available to create new connection again.
The above is the preferred embodiment of the present invention; it should be pointed out that, for those skilled in the art, under the premise without departing from principle of the present invention; can also making some improvements and modifications, these improvements and modifications also should be regarded as protection scope of the present invention.