CN107066522B - Database access method and device - Google Patents

Database access method and device Download PDF

Info

Publication number
CN107066522B
CN107066522B CN201710090434.9A CN201710090434A CN107066522B CN 107066522 B CN107066522 B CN 107066522B CN 201710090434 A CN201710090434 A CN 201710090434A CN 107066522 B CN107066522 B CN 107066522B
Authority
CN
China
Prior art keywords
database
index
instance
type
accessed
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201710090434.9A
Other languages
Chinese (zh)
Other versions
CN107066522A (en
Inventor
杨玉奇
杨从安
张宇平
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Shuzilianmeng Network Science & Technology Co Ltd
Original Assignee
Beijing Shuzilianmeng Network Science & Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Shuzilianmeng Network Science & Technology Co Ltd filed Critical Beijing Shuzilianmeng Network Science & Technology Co Ltd
Priority to CN201710090434.9A priority Critical patent/CN107066522B/en
Publication of CN107066522A publication Critical patent/CN107066522A/en
Application granted granted Critical
Publication of CN107066522B publication Critical patent/CN107066522B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2228Indexing structures
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5083Techniques for rebalancing the load in a distributed system

Abstract

The invention provides a database access method and device. The database access method comprises the following steps: acquiring the type of a database to be accessed, wherein the database to be accessed is divided into a plurality of types in advance according to the service type, and each type of database is provided with respective indexes; calling an index corresponding to the type of the database to be accessed; determining the connection state of the database instance pointed by the index; in the case where the connection state is connecting, the database instance to which the index points is accessed. According to the scheme, the databases providing data services are classified according to the service types, and each type of database is independently provided with the index, so that balanced scheduling is performed in the corresponding database according to the service types, on one hand, the configuration of database instances can be performed according to the service types, on the other hand, the access load balance of the same type of database can be ensured, and the efficiency of accessing the database is improved.

Description

Database access method and device
Technical Field
The present invention relates to databases, and more particularly, to a method and apparatus for accessing a database.
Background
Database (Database) is a collection of data organized according to a certain rule for achieving a certain purpose, and with the rapid development of computer technology, the read-write speed, data availability, security and expansibility of the Database face higher requirements.
Particularly for a large-scale high-concurrency background service architecture system, the database is generally in a distributed arrangement, namely a database cluster is adopted. The database cluster needs to ensure load balance among the databases when in use. Coordination of access to database clusters is therefore an important technique in the field of databases.
The existing access method of the database cluster generally has two modes, one mode is a database server connected with a fixed address, the mode cannot carry out balanced scheduling among all servers, the access pressure of some servers is high, and the access quantity of other servers is small; the other method is to use PROXY (PROXY) technology to send access requests to each server in a balanced manner, which realizes balanced scheduling among the servers, but all databases are required to have the capability of processing the same service, and if the connection of a certain server is abnormal, a data loss situation occurs.
Disclosure of Invention
It is an object of the invention to improve the efficiency of database access.
In particular, the present invention provides a method for accessing a database, the method comprising: acquiring the type of a database to be accessed, wherein the database to be accessed is divided into a plurality of types in advance according to the service type, and each type of database is provided with respective indexes; calling an index corresponding to the type of the database to be accessed; determining the connection state of the database instance pointed by the index; in the case where the connection state is connecting, the database instance to which the index points is accessed.
Optionally, after accessing the database instance pointed to by the index, the method further includes: and modifying the index into database instances pointing to the next sequence for the next access, wherein the database instances in each type of database are arranged in advance according to the set sequence.
Optionally, after the step of determining the connection status of the database instance pointed to by the index, the method further includes: in the case that the access connection state is disconnected, checking the latest connection time of the database pointed by the index; and in the case that the interval from the latest connection time is shorter than a preset threshold value, trying to reconnect the database pointed by the index, and after the reconnection is successful, accessing the database instance pointed by the index and modifying the index to point to the next sequential database instance.
Optionally, when the interval between the latest connection times is longer than a threshold, the database instances of the types of the databases to be accessed are queried one by one according to a set sequence until the database instance in the connection state is queried, and the queried database instance is accessed.
Optionally, after querying the database instances of the types of the databases to be accessed one by one according to the set sequence, if the database instance in the connection state is not queried, returning the database connection failure information.
Optionally, after adding and/or deleting the database instance to the database of one type, the method further includes: the database instances in the class database are reordered.
According to another aspect of the present invention, there is also provided an access apparatus for a database, the apparatus including: the type acquisition module is configured to acquire the type of the database to be accessed, wherein the database to be accessed is divided into a plurality of types in advance according to the service type, and each type of database is provided with respective indexes; the index calling module is configured to call an index corresponding to the type of the database to be accessed; a state query module configured to determine a connection state of the database instance to which the index points; and the instance access module is configured to access the database instance pointed by the index under the condition that the connection state is the connection state.
Optionally, the apparatus further comprises: and the index modification module is configured to modify the index into database instances pointing to the next sequence for the next access after the instance access module accesses the database instances pointed by the index, wherein the database instances in each class of databases are arranged in advance according to a set sequence.
Optionally, the instance access module is further configured to: in the case that the access connection state is disconnected, checking the latest connection time of the database pointed by the index; in the event that the interval from the most recent connection time is less than a preset threshold, attempting to reconnect the database to which the index points and, upon successful reconnection, accessing the database instance to which the index points, and the index modification module being further configured to modify the index to point to the next sequential database instance after the instance access module accesses the database instance to which the index points.
Optionally, the instance access module is further configured to: under the condition that the interval between the latest connection time and the latest connection time is longer than a threshold value, inquiring the database instances of the types of the databases to be accessed one by one according to a set sequence until the database instance in the connection state is inquired, and accessing the inquired database instance; and the access device of the database further comprises: a prompt module configured to: after database instances of the types of the databases to be accessed are queried one by one according to a set sequence, if the database instances in the connection state are not queried, returning database connection failure information; and the ordering module is configured to: after database instance addition and/or deletion to a class of databases, the method further comprises the following steps: the database instances in the class database are reordered.
According to the method and the device for accessing the database, the databases providing data services are classified according to the service types, and each type of database is independently provided with the index, so that balanced scheduling is performed in the corresponding database according to the service types, on one hand, configuration of database instances can be performed according to the service types, on the other hand, access load balance of the same type of database can be guaranteed, and the efficiency of accessing the database is improved.
Further, the database access method and the database access device manage multiple types of database instances of different types at the same time, pre-arrange the database instances of different types, and determine the database instances needing to be accessed by using the index. For the condition of abnormal database connection, the reconnection is limited in time, so that the abnormal database connection is prevented from being frequently tried in a short time, the waste of time is reduced, and the efficiency is improved.
Furthermore, the database access method and the database access device can perform operations of adding and deleting database instances for each type of database respectively by performing classification management on the database instances, thereby improving the flexibility of the database.
The above and other objects, advantages and features of the present invention will become more apparent to those skilled in the art from the following detailed description of specific embodiments thereof, taken in conjunction with the accompanying drawings.
Drawings
Some specific embodiments of the invention will be described in detail hereinafter, by way of illustration and not limitation, with reference to the accompanying drawings. The same reference numbers in the drawings identify the same or similar elements or components. Those skilled in the art will appreciate that the drawings are not necessarily drawn to scale. In the drawings:
FIG. 1 is a schematic block diagram of an access device for a database according to one embodiment of the present invention;
FIG. 2 is a schematic block diagram of an access device for a database according to another embodiment of the present invention;
FIG. 3 is a schematic diagram of a method of accessing a database according to one embodiment of the invention; and
fig. 4 is a flowchart illustrating a database access method according to another embodiment of the present invention.
Detailed Description
Fig. 1 is a schematic block diagram of an access device 100 for a database according to one embodiment of the present invention. The database access device 100 may generally include: a type obtaining module 110, an index calling module 120, a state query module 130, and an instance accessing module 140.
In this embodiment, the databases to be accessed are divided into multiple classes according to the service types in advance, and each class of database is provided with its own index. The number of specific partitions may be determined according to the function business of the database, each class of database may perform a relatively independent data service, and each class of database may include a plurality of database instances (instances). The database instance is a combination of a set of background processes and a memory structure, and can operate on the database. The type obtaining module 110 is configured to obtain the type of the database that needs to be accessed. And an index calling module 120 configured to call an index corresponding to the type of the database to be accessed. The index is used to indicate the database instance that should be accessed this time. The state query module 130 is configured to determine the connection state of the database instance to which the index points; the instance access module 140 is configured to access the database instance pointed to by the index if the connection state is connecting.
The access device 100 of the database classifies the database, and performs balanced scheduling on each class of database, so that the database is convenient to call, and the access efficiency of the database is improved.
Fig. 2 is a schematic block diagram of an access device 100 for a database according to another embodiment of the present invention, where the access device 100 for a database may optionally add one or more of the following modules on the basis of the above embodiment: index modification module 150, hint module 160, and sort module 170.
After the instance access module 140 accesses the database instance pointed by the index, the index modification module 150 modifies the index into the database instance pointed to the next sequence, so that the next time the index access modification module 150 modifies the index into the database instance pointed to the next sequence after one database instance is accessed, repeated access to the same database instance can be avoided, the database instances normally connected in the index can be ensured to be accessed, and therefore balanced scheduling is achieved. The database instances in each type of database are arranged in advance according to a set sequence, and after database instance operation (addition and/or deletion) is performed on one type of database, the method further comprises the following steps: the ranking module 170 may reorder the database instances in the class of database to ensure that all database instances in the class of database have an opportunity to be accessed.
In order to avoid repeated connection of the same database instance for a long time under the condition that all database instances are not in normal connection, the instance access module 140 may further check the latest connection time of the database pointed by the index under the condition that the access connection state is disconnected; in the case that the interval from the most recent connection time is shorter than a preset threshold, an attempt is made to reconnect the database pointed to by the index, and after the reconnection is successful, the database instance pointed to by the index is accessed. The threshold value can be set according to the connection condition of the database instances, so that time limitation is carried out on the attempt of reconnection, frequent attempts of connection of the database instances which cannot be connected are prevented in a short time, waste of time is reduced, and efficiency is improved.
The instance accessing module 140 may query the database instances of the type of the database that needs to be accessed one by one according to a set sequence until the database instance in the connection state is queried and the queried database instance is accessed when finding that the interval between the database instances pointed by the current index and the latest connection time is longer than the threshold.
The index modification module 150 modifies the index to point to the next sequential database instance of the currently accessed database instance after the instance access module 140 accesses the database instance, whether the currently accessed database instance is a reconnect or a query by query.
The prompt module 160 may perform a one-by-one query on the database instances of the type of the database to be accessed according to the set sequence, and return the database connection failure information if the database instance in the connection state is not queried, that is, perform an alarm to prompt the maintenance staff to intervene when the database instances in the current database type are not normally connected.
The embodiment of the present invention further provides a database access method, which can be executed by the database access device 100 according to any of the above embodiments, and improves the database access efficiency by classifying the databases and performing load balancing between database instances of the same type of databases using indexes. Fig. 3 is a schematic diagram of an access method of a database according to an embodiment of the present invention, the access method of the database including:
step S302, acquiring the type of the database to be accessed;
step S304, invoking an index corresponding to the type of the database to be accessed;
step S306, determining the connection state of the database instance pointed by the index;
in step S308, if the connection status is connection, the database instance pointed to by the index is accessed.
In this embodiment, the databases to be accessed are divided into multiple classes according to the service types in advance, database instances in each class of databases are arranged according to a set order in advance, and are respectively provided with respective indexes. Each type of database can reset the database instances, and after database instance operation (addition and/or deletion) is performed on one type of database, the database instances in the type of database can be reordered, so that all the database instances in the type of database are ensured to be accessed.
After step S306, if the access connection status of the database instance pointed to by the index is disconnected, checking the latest connection time of the database pointed to by the index; in the case that the interval from the most recent connection time is shorter than a preset threshold, an attempt is made to reconnect the database pointed to by the index and, after the reconnection is successful, the database instance pointed to by the index is accessed. And under the condition that the interval between the latest connection time and the latest connection time is longer than a threshold value, inquiring the database instances of the types of the databases needing to be accessed one by one according to a set sequence until the database instance in the connection state is inquired, and accessing the inquired database instance.
After the database instances of the types of the databases needing to be accessed are queried one by one according to the set sequence, if the database instances in the connection state are not queried, returning the failure information of database connection.
The threshold value can be set according to the connection condition of the database instances, so that time limitation is carried out on the attempt of reconnection, frequent attempts of connection of the database instances which cannot be connected are prevented in a short time, waste of time is reduced, and efficiency is improved.
After accessing a certain database instance (whether the database instance pointed by the index is normally connected, or the database instance with the disconnection time shorter than the threshold value and successfully reconnected, or the database instance in the connection state obtained by polling according to the set sequence), the pointed direction of the index is modified to the database instance next to the currently accessed database instance. Therefore, all normally connected databases can be accessed, and the uniform scheduling is realized.
Fig. 4 is a schematic flow chart of a database access method according to another embodiment of the present invention, which introduces a completion process of accessing a certain type of database, where i represents a pointer of an index, and d is an instance of the database pointed to by the index i, and the flow chart may include:
step S402, obtaining an access request of a database;
step S404, determining the type of the database needing to be accessed;
step S406, judging whether the database instance exists, if not, executing step S428, confirming that the database connection fails, and outputting prompt information;
step S408, acquiring the index of the database of the type and determining a current index pointer i;
step S410, acquiring a database instance pointed to by the index i: d;
step S412, judging whether the database instance d is connected normally;
step S414, when the database instance d is not in the connection state, judging whether the time interval from the last d normal connection exceeds a threshold value;
step S416, if the time interval does not exceed the threshold, attempting reconnection;
step S418, if the reconnection is failed or the time interval exceeds the threshold value, modifying the index i to point to the next database instance;
step S420, acquiring a database instance pointed by the modified index i;
step S422, judging whether the database instances are normally connected;
step S424, when the database instance is not in the connection state, determining whether all the query of the class database instances is completed, if yes, executing step S428, determining that the database connection fails, and outputting a prompt message;
step S426, under the condition that the database instances are normally connected, modifying the index i and returning the database instances to the request source;
and step S430, under the condition that the database instance d pointed by the index i is connected normally or reconnected successfully, modifying the index i, pointing to the next database instance, and returning d to the request source.
The above flow gives a complete process of determining a database instance in response to an access request, and it should be noted that the flow is merely an example, and in other embodiments, the order and execution parameters of some steps of the above flow may be modified to meet specific use requirements, where the number of instances of various types of databases, the types of databases, and the time interval threshold may be modified in a case that the use requirements are met.
Taking an application environment for providing software application verification as an example, a device state library and an application state library can be respectively arranged in different servers, indexes can be respectively set for the two databases and balanced scheduling is carried out, when the device state library needs to be operated, a preferred device state library example can be determined by using the indexes of the device state library, if the example is in a connection state, the example is directly returned, the indexes are modified into device state library examples pointing to the next sequence, if the disconnection time interval of the example is short, connection is retried, the example is directly returned after connection is successful, and the indexes are modified into device state library examples pointing to the next sequence. If the time interval of disconnection of the instance is longer, determining the connection state of the next sequential equipment state library instances, returning to the normally connected equipment state library instances, and if the subsequent equipment state library instances are also disconnected, sequentially inquiring until the normally connected equipment state library instances are found or all the equipment state library instances are determined to be failed to be connected.
In the method for accessing a database of this embodiment, databases providing data services are classified according to service types, and each type of database is separately provided with an index, so that balanced scheduling is performed in the corresponding database according to the service types. For the condition of abnormal database connection, the reconnection is limited in time, so that the abnormal database connection is prevented from being frequently tried in a short time, the waste of time is reduced, and the efficiency is improved.
Thus, it should be appreciated by those skilled in the art that while a number of exemplary embodiments of the invention have been illustrated and described in detail herein, many other variations or modifications consistent with the principles of the invention may be directly determined or derived from the disclosure of the present invention without departing from the spirit and scope of the invention. Accordingly, the scope of the invention should be understood and interpreted to cover all such other variations or modifications.

Claims (5)

1. A method for accessing a database, comprising:
obtaining the type of a database to be accessed, wherein the database to be accessed is divided into a plurality of types in advance according to the service type, and each type of database is provided with respective indexes;
calling an index corresponding to the type of the database needing to be accessed;
determining a connection state of a database instance pointed to by the index;
under the condition that the connection state is in connection, accessing the database instances pointed by the index, and modifying the index into the database instances pointed to the next sequence for the next access, wherein the database instances in each type of database are arranged in advance according to a set sequence;
in the case that the access to the connection state is disconnected, checking the latest connection time of the database pointed by the index;
in the case that the interval from the latest connection time is shorter than a preset threshold value, trying to reconnect the database pointed by the index, and after the reconnection is successful, accessing the database instance pointed by the index and modifying the index to point to the database instance in the next sequence;
and under the condition that the interval between the latest connection time and the latest connection time is longer than the threshold value, inquiring the database instances of the types of the databases needing to be accessed one by one according to the set sequence until the database instance in the connection state is inquired, and accessing the inquired database instance.
2. The method of claim 1,
and after the database instances of the types of the databases needing to be accessed are inquired one by one according to the set sequence, if the database instances in the connection state are not inquired, returning the database connection failure information.
3. The method of claim 1,
after database instance addition and/or deletion to one type of the database, the method further comprises the following steps: the database instances in the class database are reordered.
4. An apparatus for accessing a database, comprising:
the type acquisition module is configured to acquire the type of the database to be accessed, wherein the database to be accessed is divided into a plurality of types in advance according to the service type, and each type of database is provided with respective indexes;
the index calling module is configured to call an index corresponding to the type of the database to be accessed;
a state query module configured to determine a connection state of a database instance to which the index points;
an instance access module configured to access a database instance pointed to by the index if the connection state is connecting; in the case that the access to the connection state is disconnected, checking the latest connection time of the database pointed by the index; in the case that the interval from the latest connection time is shorter than a preset threshold value, attempting to reconnect the database pointed by the index, and accessing the database instance pointed by the index after the reconnection is successful;
the index modification module is configured to modify the index into database instances pointing to the next sequence for the next access after the instance access module accesses the database instances pointed by the index, wherein the database instances in each type of the database are arranged in advance according to a set sequence; the instance access module is further configured to:
and under the condition that the interval between the latest connection time and the latest connection time is longer than the threshold value, inquiring the database instances of the types of the databases needing to be accessed one by one according to the set sequence until the database instance in the connection state is inquired, and accessing the inquired database instance.
5. The apparatus of claim 4, wherein the apparatus is a portable electronic device
The access device of the database further comprises:
a prompt module configured to: after the database instances of the types of the databases to be accessed are queried one by one according to the set sequence, if the database instances in the connection state are not queried, returning database connection failure information; and
the sorting module is configured to: after database instance addition and/or deletion to one type of the database, the method further comprises the following steps: the database instances in the class database are reordered.
CN201710090434.9A 2017-02-20 2017-02-20 Database access method and device Active CN107066522B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201710090434.9A CN107066522B (en) 2017-02-20 2017-02-20 Database access method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201710090434.9A CN107066522B (en) 2017-02-20 2017-02-20 Database access method and device

Publications (2)

Publication Number Publication Date
CN107066522A CN107066522A (en) 2017-08-18
CN107066522B true CN107066522B (en) 2020-03-17

Family

ID=59621464

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201710090434.9A Active CN107066522B (en) 2017-02-20 2017-02-20 Database access method and device

Country Status (1)

Country Link
CN (1) CN107066522B (en)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109947765B (en) * 2017-09-28 2021-05-14 北京国双科技有限公司 Data source detection method and device
CN108093094B (en) * 2017-12-08 2021-05-28 腾讯科技(深圳)有限公司 Database instance access method, device, system, storage medium and equipment
CN109271438B (en) * 2018-10-10 2022-01-04 上海艾融软件股份有限公司 Database access method and system
CN113672665A (en) * 2021-08-18 2021-11-19 Oppo广东移动通信有限公司 Data processing method, data acquisition system, electronic device and storage medium

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9275095B2 (en) * 2013-07-31 2016-03-01 International Business Machines Corporation Compressing a multi-version database
CN105956138B (en) * 2016-05-11 2019-07-12 北京百度网讯科技有限公司 The control method and device of database connection
CN106339475A (en) * 2016-08-29 2017-01-18 联众智慧科技股份有限公司 Distributed storage system for mass data

Also Published As

Publication number Publication date
CN107066522A (en) 2017-08-18

Similar Documents

Publication Publication Date Title
CN107066522B (en) Database access method and device
CN111818112B (en) Kafka system-based message sending method and device
US7685322B2 (en) Port number emulation for wireless USB connections
CN107515878B (en) Data index management method and device
US20140365827A1 (en) Architecture for end-to-end testing of long-running, multi-stage asynchronous data processing services
CN111506559A (en) Data storage method and device, electronic equipment and storage medium
CN105512283A (en) Data quality management and control method and device
CN110688305B (en) Test environment synchronization method, device, medium and electronic equipment
CN114640707A (en) Message asynchronous processing method and device, electronic equipment and storage medium
CN110874365B (en) Information query method and related equipment thereof
CN111338888B (en) Data statistics method and device, electronic equipment and storage medium
CN112069175A (en) Data query method and device and electronic equipment
JP2017511515A (en) Method and apparatus for scanning a file
CN110737458A (en) code updating method and related device
CN115481026A (en) Test case generation method and device, computer equipment and storage medium
CN112699648B (en) Data processing method and device
CN113849520A (en) Intelligent identification method and device of abnormal SQL (structured query language), electronic equipment and storage medium
CN110493326B (en) Zookeeper-based cluster configuration file management system and method
CN112035471A (en) Transaction processing method and computer equipment
CN111538672A (en) Test case layered test method, computer device and computer-readable storage medium
CN106528577B (en) Method and device for setting file to be cleaned
CN113656411B (en) Method and device for storing graph data
CN112084827B (en) Data processing method and device
CN111966993B (en) Equipment identification code identification and generation algorithm test method, device, equipment and medium
CN111078718B (en) Frequency control method, device, equipment and computer storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant