Background technology
Various characters coding (character code: in order to use the various letters of binary representation, word, symbol etc. is also supported in the data storage of MySQL database, people have formulated various character code modes, by the good binary string of various predefineds, represent each character.Reading with writing of information must be used identical coded system, just can not cause mess code, as Chinese character of two byte representations for GB2312, and Chinese character of three byte representations for UTF-8, if write a Chinese character of encoding with GB2312, after reading, with UTF-8 coding, go to resolve again, just can only obtain wrong result), as: GBK, UTF-8 etc.MySQL provides specially character-coded statement has been set for this reason: SET NAMES, as SET NAMES GBK, just current character code has been arranged to GBK, and read-write afterwards is all carried out according to GBK coding.
Character code state connects and binds with database, that is to say, the different coding arranging in a plurality of connections is independent of each other.For example: two clients, be connected respectively to same MySQL database server, then carry out respectively SET NAMES GBK and SET NAMES UTF8, latter two client with GBK and UTF-8, encode to read and write data respectively, do not conflict mutually.
There is no connection pool, (data access request all must experience building database connection, open database, access data and close the steps such as database connection each time, and connect and open database, be the both time-consuming work of consumption of natural resource, if frequently there is this database manipulation, the performance of system will inevitably sharply decline.Database Connection-Pool Technology is after access data has operated, not closing database connects, but stored, next database request just can directly be used this connection, by the multiplexing database of having set up, connect, greatly save system resource and time) time, each client is set up one to the connection of MySQL and is monopolized this connection, and client one is carried out the encoding state in the connection that SET NAMES statement can not have influence on client two in the connection of oneself.And after introducing connection pool, problem some complexity that becomes, because client is no longer monopolized a connection now, but often execute a SQL statement, connection pool is just put back in used connection, a client will be carried out next SQL statement, and connection pool takes out a connection establishing again and uses to it.In other words, the connection that many SQL statement of same client executing are used is not necessarily same, but often executes a statement, connects and is just replaced.Doing like this object is can, by effectively multiplexing, promote the whole utilization ratio connecting for the connection in connection pool.
But due to character-coded state with connect and bind, therefore the client two that is likely that client one is used when carrying out certain SQL statement was carried out the connection of SET NAMES statement, will cause like this client one to use mistakenly UTF-8 coding to go to read and write GBK coded data, consequence is exactly mess code.
In prior art, a kind of solution that prevents mess code be partial cancellation connect multiplexing, a client is monopolized a connection at run duration, until after this client exits, the connection that it takies is just put back to connection pool.Because comprise that at this client run duration all SQL statement of SET NAMES all carry out in same connection, therefore can guarantee character-coded unification, avoid occurring the situation of mess code.
The shortcoming of obvious above-mentioned solution is that the reusing degree connecting reduces greatly.Each client also can be processed other logics at run duration except carrying out SQL statement, that is to say to carry out between two SQL statement and have certain time interval, if the connection pool of complete multiplexing connection type, the connection that this client takies in this interval will be put back to connection pool for other clients, and in the framework of above-mentioned solution, this connection is not used, and can only be taken and not put by this client.Only after this client exits, its connection taking could be used by other clients.
Summary of the invention
In view of the above problems, the present invention has been proposed to a kind of character code update the system and method that overcomes the problems referred to above or address the above problem is at least in part provided.
According to one aspect of the present invention, a kind of character code update the system is provided, comprising:
Database connects retrieval unit, is suitable for receiving the SQL statement of client, takes out database and connect from connection pool;
Database access unit, is suitable for when the current character encoding state that described database is connected is identical with the described client current character encoding state of preserving in advance, and described client is according to described database connected reference database;
Database Connection Release unit, is suitable for discharging described database after executing described SQL statement and is connected to described connection pool.
Alternatively, described database access unit further comprises:
Character code state set unit, is suitable for comprising when character-coded statement is set in described SQL statement, and it is the character code in described SQL statement that the current character encoding state that described database connects is set;
Character code state updating unit, being suitable for upgrading the described client current character encoding state of preserving is in advance the character code in described SQL statement;
SQL statement transmitting element, is suitable for described SQL statement to be connected and sent to described database by described database, so that database described in described client-access.
Alternatively, described database access unit further comprises:
SQL statement transmitting element, be suitable for not comprising character-coded statement being set in described SQL statement, and when the current character encoding state that the described database taking out connects is identical with the described client current character encoding state of preserving in advance, described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Alternatively, described database access unit further comprises:
SQL statement plug-in unit, be suitable for not comprising character-coded statement being set in described SQL statement, and when the current character encoding state that the described database taking out connects is not identical with the described client current character encoding state of preserving in advance, before described SQL statement, insert character-coded statement is set, to form new SQL statement;
SQL statement transmitting element, is suitable for described new SQL statement to be connected and sent to described database by described database, so that database described in described client-access.
Alternatively, by relation mapping table, preserve described client and described client current character encoding state.
According to a further aspect in the invention, provide a kind of character code modification method, comprised step:
Receive the SQL statement of client, from connection pool, take out database and connect;
When the current character encoding state that described database is connected is identical with the described client current character encoding state of preserving in advance, described client is according to described database connected reference database;
After executing described SQL statement, discharge described database and be connected to described connection pool.
Alternatively, when the described current character encoding state described database is connected is identical with the described client current character encoding state of preserving in advance, described client further comprises according to described database connected reference database:
In described SQL statement, comprise when character-coded statement is set, it is the character code in described SQL statement that the current character encoding state that described database connects is set;
Upgrading the described client current character encoding state of preserving is in advance the character code in described SQL statement;
Described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Alternatively, when the described current character encoding state described database is connected is identical with the described client current character encoding state of preserving in advance, described client further comprises according to described database connected reference database:
In described SQL statement, do not comprise character-coded statement is set, and when the current character encoding state that the described database taking out connects is identical with the described client current character encoding state of preserving in advance, described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Alternatively, when the described current character encoding state described database is connected is identical with the described client current character encoding state of preserving in advance, described client further comprises according to described database connected reference database:
In described SQL statement, do not comprise character-coded statement is set, and when the current character encoding state that the described database taking out connects is not identical with the described client current character encoding state of preserving in advance, before described SQL statement, insert character-coded statement is set, to form new SQL statement;
Described new SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Alternatively, by relation mapping table, preserve described client and described client current character encoding state.
According to character code update the system of the present invention and method, accessing database when the character code state that can make client connect at its character code state and database is identical, and after executing current SQL statement, discharge immediately this database and connect, in the prior art having solved thus for fear of occurring mess code, and the problem that the reusing degree that database is connected reduces greatly, obtained and can avoid the inconsistent mess code causing of character code, can guarantee again the effective multiplexing beneficial effect of all connections in connection pool.
Above-mentioned explanation is only the general introduction of technical solution of the present invention, in order to better understand technological means of the present invention, and can be implemented according to the content of instructions, and for above and other objects of the present invention, feature and advantage can be become apparent, below especially exemplified by the specific embodiment of the present invention.
Embodiment
Exemplary embodiment of the present disclosure is described below with reference to accompanying drawings in more detail.Although shown exemplary embodiment of the present disclosure in accompanying drawing, yet should be appreciated that and can realize the disclosure and the embodiment that should do not set forth limits here with various forms.On the contrary, it is in order more thoroughly to understand the disclosure that these embodiment are provided, and can by the scope of the present disclosure complete convey to those skilled in the art.
For fear of there is mess code, can make again database connect by effectively multiplexing, the character code modification method flow process of the present embodiment as shown in Figure 1, comprising:
Step S110, the SQL statement of reception client is taken out database and is connected from connection pool.Owing to being that idle database of random taking-up connects from connection pool, therefore, the character code state that the database of taking-up connects may be identical with the character code state of this client, also may be different.
Step S120, when the current character encoding state that described database is connected is identical with the described client current character encoding state of preserving in advance, described client is according to described database connected reference database.
In this step, the current character encoding state that make described database connect is identical with the described client current character encoding state of preserving in advance, specifically comprises following three kinds of situations:
One, client is when sending SQL statement to connection pool, in possible this SQL statement, just comprise character-coded statement is set, be SET NAMES code(as SET NAMES GBK or SET NAMES UTF8), represent that client wants display update character code state.Therefore, in described SQL statement, comprise when character-coded statement is set, it is the character code in described SQL statement that the current character encoding state that described database connects is set; And the described client current character encoding state that upgrades preservation is in advance the character code in described SQL statement.Afterwards described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Two, in described SQL statement, do not comprise character-coded statement is set, and when the current character encoding state that the described database taking out connects is identical with the described client current character encoding state of preserving in advance, described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Three, in described SQL statement, do not comprise character-coded statement is set, and when the current character encoding state that the described database taking out connects is not identical with the described client current character encoding state of preserving in advance, before described SQL statement, insert character-coded statement is set, insert SET NAMES cli_code, to form new SQL statement.Again described new SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Wherein, can preserve described client and described client current character encoding state by relation mapping table, and when each client display update character code state, upgrade client current character encoding state.
Step S130 discharges described database and is connected to described connection pool after executing described SQL statement.Execute after a SQL statement, client can enter other logical process stages.For example: execute a SELECT statement, database returns after the data that inquire, and client can process the data that inquire.After handling, may obtain new data, again by new data data inserting storehouse, at this moment to database connection pool, send INSERT statement again, from connection pool, take out a database connection (may be connected difference with the database that SELECT statement is used before, now need to perform step S120) and come connection data storehouse.And in client, data are processed during this period of time, the database connection before taking is released in connection pool, can be used by other client.
When client is passed through connection pool accessing database (MySQL database), above-mentioned flow process can as shown in Figure 2, comprise:
Step S210, judges whether connection pool receives the SQL statement that client is sent, if execution step S220, otherwise the SQL statement of client is waited in continuation.
Step S220 takes out a database and connects from connection pool, normally takes out at random an idle database and connects, and unappropriated database connects.
Step S230, judges in the SQL statement that client sends whether comprise SET NAMES code statement, if so, performs step S240, otherwise execution step S250;
Step S240, expression client is wanted display update character code state, and therefore, the current character encoding state that the database connection of taking-up is set is the character code in described SQL statement, and putting con_code is code; And to upgrade the described client current character encoding state of preserving be in advance the character code in described SQL statement, putting cli_code is code.Then redirect performs step S270.
Step S250, judges that whether cli_code is identical with con_code, if so, performs step S270, otherwise execution step S260;
Step S260 inserts character-coded statement is set before the SQL statement receiving, and inserts SET NAMES cli_code, to form new SQL statement.
Step S270, sends to MySQL by SQL statement (being likely the SQL statement that client arrives, is likely also the SQL statement after the SET NAMES code statement of an insertion).
Step S280, executes after the SQL statement receiving, and database Connection Release, to connection pool, is waited for and being used next time.
As shown in Figure 3, take two clients (client 1 and client 2 312) below describes as example by connection pool 320 access MySQL databases 330.
Client 1 and client 2 312 use be respectively connected one be connected two and carried out SET NAMES GBK statement and SET NAMES UTF8 statement, and to preserve the current character code state of client 1 be GBK, the current character code state of client 2 312 is UTF-8, connecting a current character code state is GBK, and connecting two current character code states is UTF-8.
Execute after SET NAMES code statement, connection pool has been put back in the used connection one of client 1 and the used connection two of client 2 312.Client 1 will be carried out the SQL statement of a read data now, takes out a connection and use to it from connection pool.If what take out is to connect one, connection pool checks client 1 and is connected a current character code, finds that the two is all GBK, directly in connection one, carries out SQL statement.If what take out is to connect two, check client 1 and be connected two current character codes, find that the former is GBK, the latter is UTF-8, and the two is inconsistent, can not directly carry out the SQL statement of read data, need to before the SQL statement of read data, insert a SET NAMES GBK statement, the character code that connects two is modified to GBK, and then carries out the SQL statement of read data, coding just can be not chaotic like this.
Character code update the system and the method for the present embodiment, can be so that the character code state that client connects at its character code state and database accessing database when identical, and after executing current SQL statement, discharge immediately this database and connect, in the prior art having solved thus for fear of occurring mess code, and the problem that the reusing degree that database is connected reduces greatly, obtained and can avoid the inconsistent mess code causing of character code, can guarantee again the effective multiplexing beneficial effect of all connections in connection pool.
The present invention also provides a kind of character code update the system 400, as shown in Figure 4, comprising:
Database connects retrieval unit 410, is suitable for receiving the SQL statement of client, takes out database and connect from connection pool.
Database access unit 420, is suitable for when the current character encoding state that described database is connected is identical with the described client current character encoding state of preserving in advance, and described client is according to described database connected reference database.Wherein, can preserve described client and described client current character encoding state by relation mapping table.
Database Connection Release unit 430, is suitable for discharging described database after executing described SQL statement and is connected to described connection pool.
Described database access unit 420 may further include:
Character code state set unit, is suitable for comprising when character-coded statement is set in described SQL statement, and it is the character code in described SQL statement that the current character encoding state that described database connects is set;
Character code state updating unit, being suitable for upgrading the described client current character encoding state of preserving is in advance the character code in described SQL statement;
SQL statement transmitting element, is suitable for described SQL statement to be connected and sent to described database by described database, so that database described in described client-access.
In described database access unit 420, SQL statement transmitting element is further adapted in described SQL statement and does not comprise character-coded statement is set, and when the current character encoding state that the described database taking out connects is identical with the described client current character encoding state of preserving in advance, described SQL statement is connected and sent to described database by described database, so that database described in described client-access.
Described database access unit 420 can further include:
SQL statement plug-in unit, be suitable for not comprising character-coded statement being set in described SQL statement, and when the current character encoding state that the described database taking out connects is not identical with the described client current character encoding state of preserving in advance, before described SQL statement, insert character-coded statement is set, to form new SQL statement;
Described SQL statement transmitting element is further adapted for described new SQL statement is connected and sent to described database by described database, so that database described in described client-access.
The algorithm providing at this is intrinsic not relevant to any certain computer, virtual system or miscellaneous equipment with demonstration.Various general-purpose systems also can with based on using together with this teaching.According to description above, it is apparent constructing the desired structure of this type systematic.In addition, the present invention is not also for any certain programmed language.It should be understood that and can utilize various programming languages to realize content of the present invention described here, and the description of above language-specific being done is in order to disclose preferred forms of the present invention.
In the instructions that provided herein, a large amount of details have been described.Yet, can understand, embodiments of the invention can not put into practice in the situation that there is no these details.In some instances, be not shown specifically known method, structure and technology, so that not fuzzy understanding of this description.
Similarly, be to be understood that, in order to simplify the disclosure and to help to understand one or more in each inventive aspect, in the above in the description of exemplary embodiment of the present invention, each feature of the present invention is grouped together into single embodiment, figure or sometimes in its description.Yet, the method for the disclosure should be construed to the following intention of reflection: the present invention for required protection requires than the more feature of feature of clearly recording in each claim.Or rather, as reflected in claims below, inventive aspect is to be less than all features of disclosed single embodiment above.Therefore, claims of following embodiment are incorporated to this embodiment thus clearly, and wherein each claim itself is as independent embodiment of the present invention.
Those skilled in the art are appreciated that and can the module in the equipment in embodiment are adaptively changed and they are arranged in one or more equipment different from this embodiment.Module in embodiment or unit or assembly can be combined into a module or unit or assembly, and can put them into a plurality of submodules or subelement or sub-component in addition.At least some in such feature and/or process or unit are mutually repelling, and can adopt any combination to combine all processes or the unit of disclosed all features in this instructions (comprising claim, summary and the accompanying drawing followed) and disclosed any method like this or equipment.Unless clearly statement in addition, in this instructions (comprising claim, summary and the accompanying drawing followed) disclosed each feature can be by providing identical, be equal to or the alternative features of similar object replaces.
In addition, those skilled in the art can understand, although embodiment more described herein comprise some feature rather than further feature included in other embodiment, the combination of the feature of different embodiment means within scope of the present invention and forms different embodiment.For example, in the following claims, the one of any of embodiment required for protection can be used with array mode arbitrarily.
All parts embodiment of the present invention can realize with hardware, or realizes with the software module moved on one or more processor, or realizes with their combination.It will be understood by those of skill in the art that and can use in practice microprocessor or digital signal processor (DSP) to realize the some or all functions that send the some or all parts in server according to the application coupling message of the embodiment of the present invention.The present invention for example can also be embodied as, for carrying out part or all equipment or device program (, computer program and computer program) of method as described herein.Realizing program of the present invention and can be stored on computer-readable medium like this, or can there is the form of one or more signal.Such signal can be downloaded and obtain from internet website, or provides on carrier signal, or provides with any other form.
It should be noted above-described embodiment the present invention will be described rather than limit the invention, and those skilled in the art can design alternative embodiment in the situation that do not depart from the scope of claims.In the claims, any reference symbol between bracket should be configured to limitations on claims.Word " comprises " not to be got rid of existence and is not listed as element or step in the claims.Being positioned at word " " before element or " one " does not get rid of and has a plurality of such elements.The present invention can be by means of including the hardware of some different elements and realizing by means of the computing machine of suitably programming.In having enumerated the unit claim of some devices, several in these devices can be to carry out imbody by same hardware branch.The use of word first, second and C grade does not represent any order.Can be title by these word explanations.