Database access method and device
By generating a customized driver package and loading the customized driver package of the target database based on basic information, the problem of database driver package conflicts and management difficulty in the existing technology is solved, and support and flexible access to multiple types and versions of databases are achieved.
Patent Information
- Application Number
- CN202410911924.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-08
- Publication Date
- 2024-10-22
AI Technical Summary
In the existing technology, the same program cannot support different types and versions of databases at the same time because database driver packages may conflict, and a large number of database driver packages will affect the performance of the main program, making management and maintenance more difficult.
By determining the public driver package, a customized driver package is generated, including the function implementation of the database driver and the public driver package, and the customized driver package of the target database is loaded according to the basic information to achieve database access and avoid driver package conflicts and coupling.
It enables the same program to simultaneously support different types and versions of databases, reducing the complexity of database management and maintenance, and improving the flexibility and isolation of database access.
Smart Images

Figure CN118820344A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a database access method and device. Background Art
[0002] There are various types of databases, each with different versions. When a program needs to use certain types of databases as data sources, it is necessary to introduce the specified version of the driver provided by the database manufacturer to establish a correct connection. However, the same program cannot support all types and versions of databases at the same time. This is because there may be conflicts between driver packages of the same or different databases, and the same program cannot support conflicting databases at the same time. In addition, when a program needs to introduce many database driver packages, a large number of database driver packages may affect the main program. Summary of the invention
[0003] The present disclosure provides a database access method and device to at least solve one of the technical problems in the related art to a certain extent. The technical solution of the present disclosure is as follows:
[0004] According to a first aspect of an embodiment of the present disclosure, a database access method is provided, comprising: determining a common driver package according to a business application scenario of at least one version of a database driver package of at least one reference database; wherein the common driver package is used to indicate common business functions that each of the database driver packages needs to implement; generating a customized driver package corresponding to each of the database driver packages according to each of the database driver packages and the common driver package; wherein the customized driver package contains the functional implementation of the corresponding database driver and the functional implementation of the common driver package; acquiring basic information for loading the customized driver package corresponding to the target database; and loading the customized driver package corresponding to the target database according to the basic information to access the target database.
[0005] According to a second aspect of an embodiment of the present disclosure, a database access device is provided, comprising: a determination module, used to determine a common driver package according to a business application scenario of at least one version of a database driver package of at least one reference database; wherein the common driver package is used to indicate a common business function that each of the database driver packages needs to implement; a generation module, used to generate a customized driver package corresponding to each of the database driver packages according to each of the database driver packages and the common driver package; wherein the customized driver package contains the functional implementation of the corresponding database driver and the functional implementation of the common driver package; a first acquisition module, used to acquire basic information for loading a customized driver package corresponding to a target database; an access module, used to load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0006] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the database access method as described in the embodiment of the first aspect of the present disclosure.
[0007] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the database access method as described in the embodiment of the first aspect of the present disclosure.
[0008] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, comprising: a computer program, wherein when the computer program is executed by a processor, the database access method as described in the embodiment of the first aspect of the present disclosure is implemented.
[0009] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0010] In the present technical scheme, a common driver package is determined according to the business application scenario of at least one version of a database driver package of at least one reference database, and a customized driver package corresponding to each database driver package is generated according to each database driver package and the common driver package, and then, basic information for loading the customized driver package corresponding to the target database is obtained, and based on the basic information, the customized driver package corresponding to the target database is loaded to access the target database. Thus, before accessing the database, a common driver package is determined according to the business application scenario of each database driver package, and then, based on the common business functions indicated by the common driver package, a customized driver package is generated for each version of each database, which includes both the functional implementation of the corresponding database driver and the functional implementation of the common driver package, and then, based on the basic information, the customized driver package is loaded for database access. This not only enables the same program to support different types and versions of databases at the same time, but also decouples the program from the database driver package to avoid a large number of database driver packages affecting the main program. ; Among them, when generating a customized driver package, by creating a sub-module for realizing the public business functions indicated by the public driver package based on the dependence on the corresponding database driver package, each version of each type of database is made independent into a module, and the actual business functions are realized inside the module based on the dependence on the corresponding database driver package, so that there is no dependency between the customized driver packages and there will be no conflict with each other. The same program can support different types and versions of databases at the same time, and because each customized driver package can be managed and maintained separately, the simplicity of database management and maintenance is effectively improved; in addition, when loading the customized driver package, it is loaded according to the basic information, so that the program and the database driver package are decoupled, and a large number of database driver packages are prevented from affecting the main program. In addition, the customized driver package is loaded in the form of a custom class loader, which not only ensures the isolation between different customized driver packages, but also can dynamically load data sources according to actual needs, thereby improving the flexibility of database access.
[0011] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0013] Figure 1 is a flowchart of a database access method shown in the first embodiment of the present disclosure;
[0014] Figure 2 is a flowchart of a database access method shown in the second embodiment of the present disclosure;
[0015] Figure 3 is a flowchart of a database access method shown in the third embodiment of the present disclosure;
[0016] Figure 4 is a flowchart of a database access method shown in the fourth embodiment of the present disclosure;
[0017] Figure 5 is a flowchart of a database access method shown in the fifth embodiment of the present disclosure;
[0018] Figure 6 is a flowchart of a database access method shown in the sixth embodiment of the present disclosure;
[0019] Figure 7 A schematic diagram of the principle of database access provided by an embodiment of the present disclosure;
[0020] Figure 8 is a schematic diagram of the structure of a database access device shown in the seventh embodiment of the present disclosure;
[0021] Fig. 9 It is a schematic structural diagram of an electronic device shown in an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0022] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0023] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0024] It should be noted that in the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, and are in compliance with relevant laws and regulations and do not violate public order and good morals.
[0025] At present, in the related technology, the method of managing databases through branches or versions has the following problems:
[0026] 1. The same program cannot support all types and versions of databases at the same time. This is because the driver packages of the same or different databases may conflict, and the same program cannot support conflicting databases at the same time.
[0027] 2. When many database driver packages need to be introduced into the program, a large number of database driver packages may affect the main program.
[0028] 3. The types and versions of databases used in different scenarios are different and may be combined at will, resulting in a large number of branches and versions that are difficult to manage and maintain. For example, if a large number of databases are already supported, it will be very difficult to change the code if a new or modified database is needed.
[0029] Therefore, in view of the above problems, the present disclosure proposes a database access method and device.
[0030] The following describes the database access method and device according to the embodiments of the present disclosure with reference to the accompanying drawings.
[0031] Figure 1 It is a flowchart of the database access method shown in the first embodiment of the present disclosure.
[0032] like Figure 1 As shown, the database access method includes the following steps:
[0033] Step 101 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0034] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0035] As an example, the public business function is any business function. For example, the public business function is business functions such as testing connection, querying database version number, and executing SQL (Structured Query Language) statements; the reference database is a database of any version from any manufacturer. For example, the reference database is MySQL 8.0 (MySQL with database version number 8.0), HBase 2.2 (HBase (Hadoop Database) with database version number 2.2), etc.
[0036] In order to enable the same program to simultaneously support different types and versions of databases, as a possible implementation method, it is first necessary to determine what the database is used for, and then determine a common driver package for indicating the common business functions that each database driver package needs to implement based on the business application scenario of at least one version of a database driver package of at least one reference database. It should be noted that the number of common business functions that each database driver package indicated by the common driver package needs to implement can be one or more.
[0037] In addition, it should be noted that the determined common driver package is only used to indicate the common business functions that each database driver package needs to implement, and does not involve the specific implementation of the common business functions.
[0038] Step 102: Generate a customized driver package corresponding to each database driver package according to each database driver package and the common driver package.
[0039] Among them, the customized driver package includes the functional implementation of the corresponding database driver and the functional implementation of the public driver package.
[0040] In order to avoid conflicts between different database driver packages, as a possible implementation method, for each version of the database driver package of each database, a customized driver package corresponding to the database driver package can be generated based on the database driver package and the public driver package.
[0041] As an example, according to the common business functions that each database driver package needs to implement as indicated by the common driver package, the corresponding database driver package can be used to complete the development of a customized driver package, so that the customized driver package contains both the function implementation of the corresponding database driver and the function implementation of the common driver package.
[0042] Step 103: Obtain basic information for loading the customized driver package corresponding to the target database.
[0043] In order to decouple the program from the database driver package and prevent a large number of database driver packages from affecting the main program, as a possible implementation method, basic information for loading the customized driver package corresponding to the target database can be obtained each time the database is connected, rather than hard-coding it directly in the code.
[0044] As an example, the basic information for loading the customized driver package corresponding to the target database can be obtained through the front-end page and / or the program. Among them, obtaining basic information through the front-end page means obtaining basic information by obtaining the user's input on the front-end page, and obtaining basic information through the program means obtaining basic information according to program logic. For example, assuming that the basic information such as the storage address has been determined based on the set rules in the program, then when the basic information is obtained later, it can be obtained in reverse based on the logic of the previous determination of the basic information.
[0045] Step 104: load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0046] In order to improve the flexibility of database access, as a possible implementation method, based on the basic information obtained for loading the customized driver package corresponding to the target database, the customized driver package corresponding to the target database is loaded, and then a connection is established with the target database to access the target database.
[0047] It is important to understand that, since the database is accessed by loading the customized driver package corresponding to the database, different types and versions of databases have different corresponding customized driver packages, and there is no dependency between the customized driver packages, and there will be no conflict with each other, so that the same program can support different types and versions of databases at the same time. Moreover, since the customized driver package corresponding to the database is loaded based on the basic information obtained when accessing the database, rather than hard-coding it directly in the code, the program can be decoupled from the database driver package, avoiding a large number of database driver packages from affecting the main program.
[0048] In summary, a common driver package is determined according to the business application scenario of at least one version of a database driver package of at least one reference database; wherein the common driver package is used to indicate the common business functions that each database driver package needs to implement; a customized driver package corresponding to each database driver package is generated according to each database driver package and the common driver package; wherein the customized driver package includes the functional implementation of the corresponding database driver and the functional implementation of the common driver package; basic information for loading the customized driver package corresponding to the target database is obtained; according to the basic information, the customized driver package corresponding to the target database is loaded to access the target database, thereby, based on the common driver package, a customized driver package is generated for each version of each database, which includes both the functional implementation of the corresponding database driver and the functional implementation of the common driver package, and according to the basic information, the customized driver package corresponding to the target database is loaded, which not only enables the same program to support different types and versions of databases at the same time, but also decouples the program from the database driver package to avoid a large number of database driver packages affecting the main program.
[0049] In order to clearly explain how to generate customized driver packages corresponding to each database driver package based on each database driver package and the common driver package in the above embodiment, the present disclosure proposes another database access method.
[0050] Figure 2 It is a flowchart of the database access method shown in the second embodiment of the present disclosure.
[0051] like Figure 2 As shown, the database access method includes the following steps:
[0052] Step 201 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0053] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0054] Step 202: Obtain at least one common business function that each database driver package contained in the common driver package needs to implement.
[0055] To facilitate the creation of subsequent sub-modules, as an example, the common business functions that need to be implemented by each database driver package contained in the common driver package are obtained in turn, and then these common business functions are implemented in the sub-modules in turn.
[0056] Step 203: Create a submodule corresponding to each database driver package according to at least one common business function.
[0057] Among them, the submodule is used to implement at least one public business function based on relying on the corresponding database driver package.
[0058] In order to make the customized driver packages free of dependencies and conflicts, as a possible implementation method, for each version of the database driver package of each database, a submodule corresponding to the database driver package is created according to at least one common business function that each database driver package contained in the public driver package needs to implement. In this way, each version of each type of database can be made into an independent module, and the actual business functions are implemented in the module based on the corresponding database driver package, avoiding conflicts between different types and versions of database driver packages.
[0059] Step 204 : Generate a customized driver package corresponding to each database driver package according to each database driver package and the sub-module corresponding to each database driver package.
[0060] As an example, each database driver package is traversed in turn, and then, according to each database driver package and the sub-module corresponding to each database driver package, a customized driver package corresponding to each database driver package is generated.
[0061] It is important to understand that since each version of the database driver package for each database corresponds to a customized driver package, each customized driver package can be managed and maintained separately, without generating a large number of branches and versions, making management and maintenance easier. Moreover, even if a large number of databases are already supported, if a new or modified database is needed, only the corresponding customized driver package needs to be loaded or modified, which effectively improves the simplicity of database management and maintenance.
[0062] Step 205: Obtain basic information for loading the customized driver package corresponding to the target database.
[0063] Step 206: Load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0064] It should be noted that the execution process of step 201 and steps 205 to 206 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0065] In summary, at least one common business function that each database driver package contained in the common driver package needs to implement is obtained; according to at least one common business function, a sub-module corresponding to each database driver package is created; wherein the sub-module is used to implement at least one common business function based on the corresponding database driver package; according to each database driver package and the sub-module corresponding to each database driver package, a customized driver package corresponding to each database driver package is generated. Thus, when generating a customized driver package, by creating a sub-module for implementing the common business function indicated by the common driver package based on the corresponding database driver package, each version of each type of database is made independent into a module, and the actual business function is implemented inside the module based on the corresponding database driver package, so that there is no dependency between the customized driver packages and there will be no conflict with each other. The same program can support different types and versions of databases at the same time, and since each customized driver package can be managed and maintained separately, the simplicity of database management and maintenance is effectively improved.
[0066] In order to clearly explain how to create sub-modules corresponding to each database driver package according to at least one common business function in the above embodiment, the present disclosure proposes another database access method.
[0067] Figure 3 It is a flowchart of a database access method shown in the third embodiment of the present disclosure.
[0068] like Figure 3 As shown, the database access method includes the following steps:
[0069] Step 301 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0070] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0071] Step 302: Obtain at least one common business function that each database driver package contained in the common driver package needs to implement.
[0072] Step 303: for any database driver package, add the dependency of the database driver package in the to-be-created submodule corresponding to the database driver package, and create a target class corresponding to the database driver package.
[0073] In order to enable the sub-module to implement at least one common business function that needs to be implemented by each database driver package contained in the common driver package on the basis of relying on the corresponding database driver package, in the embodiment of the present disclosure, for any database driver package, it is necessary to add the dependency of the database driver package in the sub-module to be created corresponding to the database driver package, and create the target class corresponding to the database driver package, so that the sub-module can implement the common business function in the target class based on relying on the corresponding database driver package.
[0074] For example, for any database driver package, you can add the dependency of the database driver package in the pom.xml file of the submodule to be created corresponding to the database driver package. Specifically, you can add <dependencies>Tags and in <dependencies>Through the tag <dependency>The tag is used to define or declare the dependency (the database driver package). <dependency>Add in tag <groupid>(used to specify the group ID of the dependency), <artifactid>(used to specify the artifact ID of the dependency, where the artifact ID is the unique identifier of the dependency under the group ID), <version>(used to specify the version number of the dependency) and other sub-tags to indicate the relevant information of the dependency.
[0075] Step 304: for any business function in the at least one common business function, determine a function implementation method of the business function in the target class.
[0076] In order to ensure that the customized driver package contains the complete functional implementation of the public driver package, in the embodiment of the present disclosure, for each business function of at least one public business function, a functional implementation method of the business function is determined in the target class in turn.
[0077] In order to improve the efficiency of determining the function implementation method, in the embodiment of the present disclosure, first, for any business function in at least one common business function, it is determined whether the database driver package contains the implementation method corresponding to the business function, and then, if the database driver package contains the function implementation method of the business function, the function implementation method of the business function contained in the database driver package is referenced in the target class, or, if the database driver package does not contain the function implementation method of the business function, the function implementation method of the business function is created in the target class.
[0078] That is to say, in the embodiment of the present disclosure, for any business function in at least one common business function, if the database driver package already has a function implementation method for the business function, the function implementation method for the business function contained in the database driver package can be directly referenced in the target class. If the database driver package does not have a function implementation method for the business function, it is necessary to create a function implementation method for the business function in the target class.
[0079] Step 305: Generate a submodule corresponding to the database driver package according to the function implementation method of at least one business function in the dependency and target classes.
[0080] Furthermore, based on the created dependency and the function implementation method of at least one public business function in the created target class, a submodule corresponding to the database driver package is generated.
[0081] Step 306: Generate a customized driver package corresponding to each database driver package according to each database driver package and the sub-module corresponding to each database driver package.
[0082] Step 307: Obtain basic information for loading the customized driver package corresponding to the target database.
[0083] Step 308: Load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0084] It should be noted that the execution process of steps 301 to 302 and steps 306 to 308 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0085] In summary, for any database driver package, the dependency of the database driver package is added in the sub-module to be created corresponding to the database driver package, and the target class corresponding to the database driver package is created; for any business function in at least one public business function, the function implementation method of the business function is determined in the target class; according to the dependency and the function implementation method of at least one business function in the target class, the sub-module corresponding to the database driver package is generated, thereby, the sub-module corresponding to each database driver package for realizing at least one public business function based on the dependency on the corresponding database driver package can be generated, so that based on each database driver package and the sub-module corresponding to each database driver package, the customized driver package corresponding to each database driver package is generated, ensuring that there is no dependency between different customized driver packages and no conflict with each other.
[0086] In order to clearly explain how to generate a customized driver package corresponding to each database driver package according to each database driver package and the sub-modules corresponding to each database driver package in the above embodiment, the present disclosure proposes another database access method.
[0087] Figure 4 It is a flowchart of a database access method shown in the fourth embodiment of the present disclosure.
[0088] like Figure 4 As shown, the database access method includes the following steps:
[0089] Step 401 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0090] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0091] Step 402: Obtain at least one common business function that each database driver package contained in the common driver package needs to implement.
[0092] Step 403: Create a submodule corresponding to each database driver package according to at least one common business function.
[0093] Among them, the submodule is used to implement at least one public business function based on relying on the corresponding database driver package.
[0094] Step 404: for any database driver package, the database driver package and the sub-modules corresponding to the database driver package are packaged to generate a target database driver package.
[0095] In order to ensure that the customized driver package contains both the functional implementation of the corresponding database driver and the functional implementation of the common driver package, as an example, each database driver package and the sub-module corresponding to the database driver package are packaged into the target database driver package.
[0096] In the embodiment of the present disclosure, for any database driver package, a packaging tool is used to package the database driver package and the sub-modules corresponding to the database driver package into a target database driver package; or, for any database driver package, the database driver package and the sub-modules corresponding to the database driver package are stored in the target database driver.
[0097] Step 405: Use the target database driver package as the customized driver package corresponding to the database driver package.
[0098] Step 406, obtaining basic information for loading the customized driver package corresponding to the target database.
[0099] Step 407: Load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0100] It should be noted that the execution process of steps 401 to 403 and steps 406 to 407 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0101] In summary, by packaging the database driver package and the sub-modules corresponding to the database driver package for any database driver package, a target database driver package is generated; the target database driver package is used as a customized driver package corresponding to the database driver package, thereby, each database driver package and the sub-modules corresponding to each database driver package can be packaged into a customized driver package corresponding to each database driver package, ensuring that the customized driver package can simultaneously include the functional implementation of the corresponding database driver and the functional implementation of the common driver package, and then when loading the database, only the corresponding customized driver package needs to be loaded.
[0102] In order to clearly explain how to obtain basic information for loading a customized driver package corresponding to a target database in the above embodiment, the present disclosure proposes another database access method.
[0103] Figure 5 It is a flowchart of the database access method shown in the fifth embodiment of the present disclosure.
[0104] like Figure 5 As shown, the database access method includes the following steps:
[0105] Step 501 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0106] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0107] Step 502: Generate customized driver packages corresponding to the database driver packages according to the database driver packages and the common driver package.
[0108] Among them, the customized driver package includes the functional implementation of the corresponding database driver and the functional implementation of the public driver package.
[0109] Step 503: Obtain database information of the target database.
[0110] The database information includes connection information, database type and database version. The connection information is used to connect to the database. For example, the connection information includes a connection address, a connection user name, a connection password and the like.
[0111] As an example, the database information of the target database input by the user is obtained, or the database information of the target database is obtained from a specified file.
[0112] Step 504, obtaining the storage path of the customized driver package of the target database.
[0113] The storage path of the customized driver package is determined according to the specified storage path and the database type and database version indicated by the database information.
[0114] In the disclosed embodiment, after generating the customized driver packages corresponding to each database driver package, the customized driver packages corresponding to each database driver package can be stored. As a possible implementation method, the designated storage path of the customized driver package corresponding to each database driver package is obtained; for any database driver package, the designated storage path is spliced with the database type and database version of the database driver package to determine the customized driver package storage path of the database driver package; the customized driver package corresponding to the database driver package is stored in the customized driver package storage path of the database driver package.
[0115] As an example, assuming that the designated storage path of the customized driver package corresponding to each database driver package is predetermined to be C: / dataSource, then for the MySQL 8.0 database driver, the designated storage path is concatenated with the database type and database version of the database driver package, and it can be determined that the storage path of the customized driver package is C: / dataSource / mysql / 8.0, and then the customized driver package corresponding to the MySQL 8.0 database driver can be stored in C: / dataSource / mysql / 8.0.
[0116] Furthermore, when obtaining the storage path of the customized driver package, the storage path of the customized driver package can also be determined according to the specified storage path, and the database type and database version indicated by the database information.
[0117] Step 505: Generate basic information according to the database information and the storage path of the customized driver package.
[0118] As an example, the basic information is composed of database information and a storage path of a customized driver package. Thus, the basic information can be generated based on the database information and the storage path of the customized driver package.
[0119] Step 506: Load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0120] It should be noted that the execution process of steps 501 to 502 and step 506 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0121] In summary, by obtaining the database information of the target database; obtaining the storage path of the customized driver package of the target database; wherein the storage path of the customized driver package is determined according to the specified storage path, and the database type and database version indicated by the database information; generating basic information according to the database information and the storage path of the customized driver package, thereby enabling the generated basic information to be used to load the customized driver package corresponding to the target database.
[0122] In order to clearly explain how to load the customized driver package corresponding to the target database according to the basic information in the above embodiment to access the target database, the present disclosure proposes another database access method.
[0123] Figure 6 It is a flowchart of the database access method shown in the sixth embodiment of the present disclosure.
[0124] like Figure 6 As shown, the database access method includes the following steps:
[0125] Step 601 : determining a common driver package according to a business application scenario of a database driver package of at least one version of at least one reference database.
[0126] The public driver package is used to indicate the public business functions that each database driver package needs to implement.
[0127] Step 602: Generate a customized driver package corresponding to each database driver package according to each database driver package and the common driver package.
[0128] Among them, the customized driver package includes the functional implementation of the corresponding database driver and the functional implementation of the public driver package.
[0129] Step 603: Obtain basic information for loading the customized driver package corresponding to the target database.
[0130] Step 604, create a custom class loader.
[0131] In order to effectively isolate the program from the database driver package, and the database driver package from the database driver package, so that they do not affect each other, as a possible implementation method, a custom class loader can be created and used to load the customized driver package.
[0132] Step 605: Use the basic information as a parameter of a custom class loader acquisition method to acquire the custom class loader corresponding to the target database.
[0133] In order to improve the flexibility of database access, as a possible implementation method, a custom class loader corresponding to the target database may be created based on basic information for loading a customized driver package corresponding to the target database.
[0134] Step 606: Use the custom class loader corresponding to the target database to load the customized driver package corresponding to the target database to access the target database.
[0135] It should be noted that the execution process of steps 601 to 603 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0136] In summary, by creating a custom class loader; using the basic information as the parameter of the custom class loader acquisition method to obtain the custom class loader corresponding to the target database; using the custom class loader corresponding to the target database to load the customized driver package corresponding to the target database to access the target database, thus, when loading the customized driver package, it is loaded according to the basic information, thereby realizing the decoupling of the program and the database driver package, avoiding the influence of a large number of database driver packages on the main program. In addition, the customized driver package is loaded by the custom class loader, which not only ensures the isolation between different customized driver packages, but also can dynamically load the data source according to actual needs, thereby improving the flexibility of database access.
[0137] Based on any embodiment of the present disclosure, Figure 7 As shown, the database access method of the embodiment of the present disclosure can also be implemented based on the following steps:
[0138] 1. Determine the database manufacturer, database type, and database version, and assemble the code to obtain the following code: xx manufacturer-xx database-xx version. According to the above code, locate the storage path of the customized driver package. At this stage, the system will collect basic information of the target database, such as database type, database version, storage path of the customized driver package, etc.;
[0139] 2. Use a custom loader to load the customized driver package. In this stage, the system dynamically loads (selectively loads) the customized driver package. It should be noted that before loading the customized driver package, a corresponding customized driver package has been generated based on the public driver package and at least one version of the database driver package of at least one database. Figure 7 As shown, based on the public driver package and the driver package default-mysql-5.7.jar provided by the manufacturer, the corresponding customized driver package mysql5.7.jar is generated; based on the public driver package and the driver package default-mysql-8.0.jar provided by the manufacturer, the corresponding customized driver package mysql8.0.jar is generated; based on the public driver package and the driver package default-hive-3.1.0.jar provided by the manufacturer, the corresponding customized driver package hive.jar is generated; based on the public driver package and the driver package tbds-hive-3.1.0.jar provided by the manufacturer, the corresponding customized driver package hive-tbds.jar is generated. These customized driver packages are stored in the specified path, and the dependencies are completely isolated;
[0140] For example, when storing customized driver packages, you can determine in advance that customized driver packages are placed in C: / dataSource, and C: / dataSource is the designated storage path. Suppose there is a mysql version 5.7, the manufacturer provides a default-mysql-5.7.jar database driver package, and based on the driver package provided by the manufacturer, customized development is completed to form a customized driver package of mysql5.7.jar (the name of the customized driver package here can be generated according to a certain rule, or it can be written at will, and it is recommended to use a meaningful name). Then, you can splice the designated storage path with the database type and database version of the database driver package, and determine that the storage path of the customized driver package of the database driver package is C: / dataSource / mysql / 5.7, so that the customized driver package of mysql5.7.jar is stored in C: / dataSource / mysql / 5.7. Then, when loading the customized driver package, based on the basic information obtained, a custom class loader can be used to find mysql5.7.jar in C: / dataSource / mysql / 5.7 for loading. After loading, this driver can be used to connect to the database;
[0141] 3. After the system loads the customized driver package, it can execute the specified business logic.
[0142] According to the processing flow of the above solution, each version of each type of database can be made into an independent module. The module uses the driver package provided by the manufacturer to complete the actual functions, and then uses a custom class loader to load the drivers of different databases, which completely solves the problem of multiple databases and multiple versions.
[0143] Corresponding to the database access method provided in the above embodiment, the present disclosure also provides a database access device. Since the database access device provided in the embodiment of the present disclosure corresponds to the database access method provided in the above embodiment, the implementation method of the database access method is also applicable to the database access device provided in the embodiment of the present disclosure, and will not be described in detail in the embodiment of the present disclosure.
[0144] Figure 8 It is a structural diagram of a database access device shown in the seventh embodiment of the present disclosure.
[0145] like Figure 8 As shown, the database access device 800 includes: a determination module 810 , a generation module 820 , a first acquisition module 830 and an access module 840 .
[0146] Among them, the determination module 810 is used to determine the common driver package according to the business application scenario of at least one version of the database driver package of at least one reference database; wherein the common driver package is used to indicate the common business functions that each database driver package needs to implement; the generation module 820 is used to generate a customized driver package corresponding to each database driver package according to each database driver package and the common driver package; wherein the customized driver package contains the functional implementation of the corresponding database driver and the functional implementation of the common driver package; the first acquisition module 830 is used to obtain basic information for loading the customized driver package corresponding to the target database; the access module 840 is used to load the customized driver package corresponding to the target database according to the basic information to access the target database.
[0147] As a possible implementation method of the embodiment of the present disclosure, a generation module 820 is used to obtain at least one common business function that each database driver package contained in a common driver package needs to implement; based on the at least one common business function, a sub-module corresponding to each database driver package is created; wherein the sub-module is used to implement at least one common business function based on dependence on the corresponding database driver package; and based on each database driver package and the sub-module corresponding to each database driver package, a customized driver package corresponding to each database driver package is generated.
[0148] As a possible implementation method of the embodiment of the present disclosure, generation module 820 is used to, for any database driver package, add the dependency of the database driver package in the sub-module to be created corresponding to the database driver package, and create a target class corresponding to the database driver package; for any business function in at least one public business function, determine the functional implementation method of the business function in the target class; and generate the sub-module corresponding to the database driver package according to the dependency and the functional implementation method of at least one business function in the target class.
[0149] As a possible implementation method of the embodiment of the present disclosure, generation module 820 is used to determine whether the database driver package contains an implementation method corresponding to any business function in at least one common business function; if the database driver package contains a function implementation method of the business function, the function implementation method of the business function contained in the database driver package is referenced in the target class; if the database driver package does not contain a function implementation method of the business function, the function implementation method of the business function is created in the target class.
[0150] As a possible implementation method of the embodiment of the present disclosure, the generation module 820 is used to package the database driver package and the sub-modules corresponding to the database driver package for any database driver package to generate a target database driver package; and use the target database driver package as a customized driver package corresponding to the database driver package.
[0151] As a possible implementation method of the embodiment of the present disclosure, the generation module 820 is used to use a packaging tool to package the database driver package and the sub-modules corresponding to the database driver package into a target database driver package for any database driver package; or, for any database driver package, store the database driver package and the sub-modules corresponding to the database driver package into the target database driver.
[0152] As a possible implementation method of the embodiment of the present disclosure, the first acquisition module 830 is used to obtain database information of the target database; obtain the storage path of the customized driver package of the target database; wherein the storage path of the customized driver package is determined according to the specified storage path, and the database type and database version indicated by the database information; and generate basic information according to the database information and the storage path of the customized driver package.
[0153] As a possible implementation method of the embodiment of the present disclosure, access module 840 is used to create a custom class loader; use basic information as a parameter of the custom class loader acquisition method to obtain the custom class loader corresponding to the target database; use the custom class loader corresponding to the target database to load the customized driver package corresponding to the target database to access the target database.
[0154] As a possible implementation of the embodiment of the present disclosure, the database access device 800 further includes: a second acquisition module, a splicing module and a storage module.
[0155] The second acquisition module is used to acquire the designated storage path of the customized driver package corresponding to each database driver package;
[0156] A splicing module is used to splice the specified storage path with the database type and database version of the database driver package for any database driver package to determine the customized driver package storage path of the database driver package;
[0157] The storage module is used to store the customized driver package corresponding to the database driver package in the customized driver package storage path of the database driver package.
[0158] The database access device of the embodiment of the present disclosure determines a common driver package according to the business application scenario of at least one version of the database driver package of at least one reference database; wherein the common driver package is used to indicate the common business functions that each database driver package needs to implement; generates a customized driver package corresponding to each database driver package according to each database driver package and the common driver package; wherein the customized driver package includes the functional implementation of the corresponding database driver and the functional implementation of the common driver package; obtains basic information for loading the customized driver package corresponding to the target database; and loads the customized driver package corresponding to the target database according to the basic information to access the target database, thereby, based on the common driver package, a customized driver package is generated for each version of each database, which includes the functional implementation of the corresponding database driver and the functional implementation of the common driver package, and according to the basic information, the customized driver package corresponding to the target database is loaded, which not only enables the same program to support different types and versions of databases at the same time, but also decouples the program from the database driver package to avoid a large number of database driver packages affecting the main program.
[0159] In an exemplary embodiment, an electronic device is also provided.
[0160] Among them, electronic equipment includes:
[0161] processor;
[0162] a memory for storing processor-executable instructions;
[0163] The processor is configured to execute instructions to implement the database access method proposed in any of the aforementioned embodiments.
[0164] As an example, Fig. 9 is a schematic diagram of the structure of an electronic device 900 shown in an exemplary embodiment of the present disclosure, such as Fig. 9 As shown, the electronic device 900 may further include:
[0165] The memory 910 and the processor 920, a bus 930 connecting different components (including the memory 910 and the processor 920), the memory 910 stores a computer program, and when the processor 920 executes the program, the database access method described in the embodiment of the present disclosure is implemented.
[0166] Bus 930 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus and Peripheral Component Interconnect (PCI) bus.
[0167] The electronic device 900 typically includes a variety of electronic device readable media. These media can be any available media that can be accessed by the electronic device 900, including volatile and non-volatile media, removable and non-removable media.
[0168] The memory 910 may also include computer system readable media in the form of volatile memory, such as random access memory (RAM) 940 and / or cache memory 950. The server 900 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 960 may be used to read and write non-removable, non-volatile magnetic media ( Fig. 9 not shown, often referred to as a "hard drive packager"). Although Fig. 9 Not shown, a disk driver package for reading and writing a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk driver package for reading and writing a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each driver package can be connected to the bus 930 via one or more data medium interfaces. The memory 910 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.
[0169] A program / utility 980 having a set (at least one) of program modules 970 may be stored, for example, in the memory 910, such program modules 970 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 970 generally perform the functions and / or methods of the embodiments described in the present disclosure.
[0170] The electronic device 900 may also communicate with one or more external devices 990 (e.g., keyboard, pointing device, display 991, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 900, and / or communicate with any device that enables the electronic device 900 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 992. In addition, the electronic device 900 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 993. As shown, the network adapter 993 communicates with other modules of the electronic device 900 through a bus 930. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 900, including but not limited to: microcode, device driver package, redundant processing unit, external disk driver package array, RAID system, tape driver package, and data backup storage system, etc.
[0171] The processor 920 executes various functional applications and data processing by running the programs stored in the memory 910 .
[0172] It should be noted that the implementation process and technical principles of the electronic device of this embodiment refer to the aforementioned explanation of the database access method of the embodiment of the present disclosure, and will not be repeated here.
[0173] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions, and the above instructions can be executed by a processor of an electronic device to complete the database access method proposed in any of the above embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0174] In an exemplary embodiment, a computer program product is also provided, including a computer program / instruction, wherein the computer program / instruction, when executed by a processor, implements the database access method proposed in any of the above embodiments.
[0175] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0176] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.< / version> < / artifactid> < / groupid> < / dependency> < / dependency> < / dependencies> < / dependencies>
Claims
1. A database access method, characterized in that: include: Determine a common driver package according to a business application scenario of at least one version of a database driver package of at least one reference database; wherein the common driver package is used to indicate a common business function that each of the database driver packages needs to implement; Generate a customized driver package corresponding to each database driver package according to each database driver package and the common driver package; wherein the customized driver package includes the function implementation of the corresponding database driver and the function implementation of the common driver package; Obtain basic information for loading the customized driver package corresponding to the target database; According to the basic information, a customized driver package corresponding to the target database is loaded to access the target database.
2. The method according to claim 1, characterized in that Generating a customized driver package corresponding to each database driver package according to each database driver package and the common driver package includes: Acquire at least one common business function that each of the database driver packages contained in the common driver package needs to implement; According to the at least one common business function, a submodule corresponding to each of the database driver packages is created; wherein the submodule is used to implement the at least one common business function based on the corresponding database driver package; According to each of the database driver packages and the sub-modules corresponding to each of the database driver packages, a customized driver package corresponding to each of the database driver packages is generated.
3. The method according to claim 2, characterized in that The step of creating a submodule corresponding to each of the database driver packages according to the at least one common business function includes: For any of the database driver packages, add the dependency of the database driver package in the to-be-created submodule corresponding to the database driver package, and create a target class corresponding to the database driver package; For any business function of the at least one common business function, determining a function implementation method of the business function in the target class; A submodule corresponding to the database driver package is generated according to the dependency and the function implementation method of at least one business function in the target class.
4. The method according to claim 3, characterized in that The step of determining, for any one of the at least one common business function, an implementation method corresponding to the business function in the target class includes: For any business function among the at least one common business function, determining whether the database driver package includes an implementation method corresponding to the business function; In a case where the database driver package includes a function implementation method of the business function, referencing the function implementation method of the business function included in the database driver package in the target class; In the case that the database driver package does not include the function implementation method of the business function, the function implementation method of the business function is created in the target class.
5. The method according to claim 2, characterized in that: Generating a customized driver package corresponding to each database driver package according to each database driver package and a submodule corresponding to each database driver package includes: For any of the database driver packages, packaging the database driver package and the submodules corresponding to the database driver package to generate a target database driver package; The target database driver package is used as the customized driver package corresponding to the database driver package.
6. The method according to claim 5, characterized in that For any of the database driver packages, packaging the database driver package and the submodules corresponding to the database driver package to generate a target database driver package includes: For any of the database driver packages, a packaging tool is used to package the database driver package and the submodules corresponding to the database driver package into the target database driver package; or, For any of the database driver packages, the database driver package and the submodules corresponding to the database driver package are stored in the target database driver.
7. The method according to claim 1, characterized in that The obtaining of basic information for loading a customized driver package corresponding to a target database includes: Acquire database information of the target database; Obtaining a storage path of the customized driver package of the target database; wherein the storage path of the customized driver package is determined according to the designated storage path, and the database type and database version indicated by the database information; The basic information is generated according to the database information and the storage path of the customized driver package.
8. The method according to claim 1, characterized in that The step of loading a customized driver package corresponding to the target database according to the basic information to access the target database includes: Create a custom class loader; The basic information is used as a parameter of a custom class loader acquisition method to obtain a custom class loader corresponding to the target database; The custom class loader corresponding to the target database is used to load the customized driver package corresponding to the target database to access the target database.
9. The method according to any one of claims 1 to 8, characterized in that After generating the customized driver packages corresponding to the database driver packages according to the database driver packages and the common driver package, the method further includes: Obtaining a designated storage path of a customized driver package corresponding to each of the database driver packages; For any of the database driver packages, the designated storage path is concatenated with the database type and database version of the database driver package to determine a customized driver package storage path of the database driver package; The customized driver package corresponding to the database driver package is stored in the customized driver package storage path of the database driver package.
10. A database access device, characterized in that: include: A determination module, configured to determine a common driver package according to a business application scenario of at least one version of a database driver package of at least one reference database; wherein the common driver package is used to indicate a common business function that each of the database driver packages needs to implement; A generating module, used for generating a customized driver package corresponding to each of the database driver packages according to each of the database driver packages and the common driver package; wherein the customized driver package includes the function implementation of the corresponding database driver and the function implementation of the common driver package; A first acquisition module is used to acquire basic information for loading a customized driver package corresponding to a target database; The access module is used to load the customized driver package corresponding to the target database according to the basic information to access the target database.
Citation Information
Patent Citations
JDBC drive coexistence method and system for multiple versions of same database
CN109033436A
Database drive loading method and database drive loading device
CN111679871A
SPI (Serial Peripheral Interface)-based configuration-free multi-data source packaging method and system
CN114880026A
Database data acquisition method and device based on class isolation mode
CN116303349A
Transparent tunneling architecture for a secured database
US20160149858A1
