Database connection method and device, equipment, storage medium and program product
By obtaining user access requests in the database connection and matching the database name to determine the JDBC connection object, the problem of wasted database connection resources and difficult to meet the needs of multiple scenarios in the prior art is solved, and a more efficient and reliable database connection is achieved.
Patent Information
- Application Number
- CN202510072757.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-16
- Publication Date
- 2025-05-16
AI Technical Summary
The prior art can easily cause resource waste when database connections and it is difficult to meet database connection requirements in different scenarios.
By obtaining the user's access request, determining the user ID and the target data source ID, determining the user interface session information and the target data source connection instance in the session information, matching the source JDBC connection object according to the database name, and connecting to the target database based on the target source JDBC connection object.
Improves the universality and reliability of database connections in multiple scenarios, saves database connection resources, and avoids the frequent process of creating and closing connection objects.
Smart Images

Figure CN120011406A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to computer technology, and in particular to a database connection method, device, equipment, storage medium and program product. Background Art
[0002] JDBC (Java Database Connectivity) refers to the standard interface for interaction between Java applications and different databases. Through the JDBC API, you can implement the function of accessing the database in the development of Java applications.
[0003] The prior art creates a certain number of JDBC connection objects in a buffer pool before the application establishes a connection with the database. When the application receives an http request, it reads the pre-created connection object from the buffer pool for use, and puts it back into the buffer pool after each use for the next call.
[0004] However, the prior art pre-establishes a certain number of database connections in the buffer pool, which easily causes a waste of database connection resources, and the connection objects randomly selected in the buffer pool are difficult to meet the database connection requirements in different scenarios. Summary of the invention
[0005] The embodiments of the present application provide a database connection method, apparatus, device, storage medium and program product to improve the versatility and reliability of database connection in multiple scenarios and save database connection resources.
[0006] In a first aspect, an embodiment of the present application provides a database connection method, comprising:
[0007] Obtaining the access request sent by the user, and determining the user ID and target data source ID indicated by the access request;
[0008] Determine the user interface session information corresponding to the user ID in the session information, and determine the target data source connection instance corresponding to the target data source ID based on the source connection object mapping relationship corresponding to the user interface session information, where the source connection object mapping relationship is a mapping relationship between the data source ID and the data source connection instance;
[0009] When it is determined that the access request indicates the target database name, determine whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name; if so, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
[0010] Optionally, if the target data source connection instance corresponding to the target data source ID does not exist in the user interface session information, then based on the connection information mapping relationship corresponding to the user interface session information, it is determined whether there is connection information corresponding to the target data source ID, where the connection information mapping relationship is a mapping relationship between the data source ID and the connection information;
[0011] When it is determined that the connection information exists, a new target data source connection instance is generated according to the connection information, and the user interface session information is updated to connect to the target database according to the updated user interface session information.
[0012] Optionally, before generating a new data source connection instance based on the connection information, if it is determined that no connection information exists, the connection information corresponding to the target data source ID in the lightweight database is determined; wherein the lightweight database includes connection information corresponding to multiple data sources respectively obtained before obtaining the access request, and each connection information includes the IP address, port number and user name corresponding to the data source.
[0013] Optionally, if it is determined that there is no source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name, the source JDBC connection object in the target data source connection instance corresponding to the target data source ID is closed.
[0014] Optionally, if it is determined that the access request does not indicate the target database name, a database connection is made based on a source JDBC connection object that was last used in the target data source connection instance.
[0015] Optionally, when the access request indicates modification of connection information of any data source, determining a data source ID and modification information of the data source;
[0016] Based on the data source ID, determine the connection information corresponding to the data source ID in the lightweight database, update the connection information based on the modification information, and obtain the updated connection information in the lightweight database;
[0017] The connection information corresponding to the data source ID in the user interface session information is updated to obtain the updated connection information in the user interface session information.
[0018] In a second aspect, an embodiment of the present application provides a database connection device, including:
[0019] An acquisition module, used to acquire an access request sent by a user, and determine a user ID and a target data source ID indicated by the access request;
[0020] A processing module, used to determine the user interface session information corresponding to the user ID in the session information, and determine the target data source connection instance corresponding to the target data source ID based on the source connection object mapping relationship corresponding to the user interface session information, wherein the source connection object mapping relationship is a mapping relationship between the data source ID and the data source connection instance;
[0021] Optionally, the processing module is also used to determine whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name when it is determined that the access request indicates the target database name; if so, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
[0022] Optionally, the processing module is further configured to determine whether there is connection information corresponding to the target data source ID based on a connection information mapping relationship corresponding to the user interface session information when there is no target data source connection instance corresponding to the target data source ID in the user interface session information, where the connection information mapping relationship is a mapping relationship between the data source ID and the connection information;
[0023] When it is determined that the connection information exists, a new target data source connection instance is generated according to the connection information, and the user interface session information is updated to connect to the target database according to the updated user interface session information.
[0024] Optionally, the processing module is also used to determine the connection information corresponding to the target data source ID in the lightweight database if it is determined that the connection information does not exist before generating a new data source connection instance based on the connection information; wherein the lightweight database includes connection information corresponding to multiple data sources respectively obtained before obtaining the access request, and each connection information includes the IP address, port number and user name corresponding to the data source.
[0025] Optionally, the processing module is further configured to close the source JDBC connection object in the target data source connection instance corresponding to the target data source ID when it is determined that there is no source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name.
[0026] Optionally, the processing module is further configured to, when it is determined that the access request does not indicate the name of the target database, perform a database connection based on the source JDBC connection object last used in the target data source connection instance.
[0027] Optionally, the processing module is further used to determine the data source ID and modification information of the data source when the access request indicates modification of the connection information of any data source;
[0028] Based on the data source ID, determine the connection information corresponding to the data source ID in the lightweight database, update the connection information based on the modification information, and obtain the updated connection information in the lightweight database;
[0029] The connection information corresponding to the data source ID in the user interface session information is updated to obtain the updated connection information in the user interface session information.
[0030] In a third aspect, an embodiment of the present application provides an electronic device, including: a memory, a processor;
[0031] Memory stores computer-executable instructions;
[0032] The processor executes the computer-executable instructions stored in the memory, so that the processor executes the above first aspect and / or various possible implementations of the first aspect.
[0033] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the first aspect above and / or various possible implementations of the first aspect.
[0034] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the above first aspect and / or various possible implementation methods of the first aspect.
[0035] The database connection method, device, equipment, storage medium and program product provided by the embodiment of the present application obtains the access request sent by the user, and determines the user ID and target data source ID indicated by the access request, determines the user interface session information corresponding to the user ID in the session information, and determines the target data source connection instance corresponding to the target data source ID in the user interface session information according to the source connection object mapping relationship corresponding to the user interface session information. When the access request indicates the target database name, it is further determined whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name. If so, the target source JDBC connection object is obtained, and the target database is connected based on the target source JDBC connection object. The present application improves the versatility and reliability of database connection in multiple scenarios, and does not require frequent new connection objects and pre-stored connection objects, saving database connection resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0037] Figure 1A schematic diagram of an application scenario of the database connection method provided in this application;
[0038] Figure 2 Schematic diagram of the database connection method provided for this application Figure 1 ;
[0039] Figure 3 Schematic diagram of the database connection method provided for this application Figure 2 ;
[0040] Figure 4 A schematic diagram of the structure of the database connection device provided in this application;
[0041] Figure 5 A schematic diagram of the structure of the electronic device provided in this application.
[0042] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0043] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0044] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant data comply with the relevant laws, regulations and standards of the relevant regions, take necessary confidentiality measures, do not violate public order and good customs, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0045] In Java Web development, a connection is established between an application and a database to manage the connected database. Figure 1 A schematic diagram of an application scenario of the database connection method provided in this application, such as Figure 1As shown, the specific application scenarios of the present application include an application, database 1, database 2, ... database n, where n is a positive integer. When an http request indicates access to database 1, a connection is established between the application and database 1 to achieve access to database 1.
[0046] In the prior art, every time an application needs to interact with a database, it needs to create a new JDBC connection object and load it into the JVM (Java Virtual Machine) memory. After verifying the database IP address, port number and user name corresponding to the JDBC connection object, it interacts with the database after verification, and closes the current JDBC connection object after the interaction ends. However, the process of establishing a connection between an application and a database is complicated and occupies a large amount of system resources. When the concurrency is large, it is easy to cause the application service to crash. In addition, since there are a large number of JDBC connection objects in the application, when a program exception occurs, it is easy to cause the program to fail to close normally, thereby continuously occupying the database connection resources, further resulting in waste and leakage of database connection resources, and affecting the stability of the database.
[0047] Before the application program establishes a connection with the database, the prior art creates a preset number of JDBC connection objects in the buffer pool in advance, so that when the application program needs to establish a connection with the database, the pre-created JDBC connection object is directly called from the buffer pool, and after the JDBC connection object is used, it is directly returned to the buffer pool to wait for the next call, thereby avoiding the resource occupation of a newly created JDBC connection object each time it is used. However, this method requires the pre-occupation of a preset number of database connections, which is likely to cause waste of database connection resources, and because the JDBC connection object called by the prior art is randomly selected from the buffer pool, it is difficult for the prior art to provide the same JDBC connection object to multiple http requests of the same database in a scenario where multiple databases are frequently changed (that is, the database IP address, port number and user name corresponding to the JDBC connection object used by the multiple http requests are the same), which reduces the versatility and reliability of the database connection.
[0048] The database connection method provided by the present application obtains the user ID and the target data source ID indicated by the access request by obtaining an access request, determines the user interface session information corresponding to the user ID in the session information, determines the target data source connection instance corresponding to the target data source ID in the user interface session information, and when the access request does not indicate the target database name, performs a database connection based on any source JDBC connection object in the target data source connection instance, and when the target database name is indicated, determines whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name, and when it is determined that it exists, determines the source JDBC connection object as the target source JDBC connection object, and connects to the target database based on the target source JDBC connection object, thereby solving the technical problem of the prior art that a new connection is required each time, reduces the complexity of database connection, and realizes providing the same connection object for access requests to the same database, thereby improving the convenience of database access. When it is determined that it does not exist, all source JDBC connection objects in the current target data source connection instance are closed to avoid continuous occupation of a large amount of system resources, and a new data source connection instance corresponding to the target data source ID is generated according to the connection information corresponding to the target data source ID in the target data source connection instance, so as to connect to the database according to the newly generated data source connection instance, thereby achieving reliability and universality of database connection.
[0049] A database connection refers to the communication channel established between an application and a database server. The database connection is uniquely identified by the IP address of the database instance, the port number of the database instance, the user name of the database instance, and the name of the database instance. The IP address of the database instance is the network address where the database server is located, the port number of the database instance is the port number monitored by the database server, the user name of the database instance is the user name used to connect to the database, which is usually related to the access rights of the database, and the name of the database instance specifies the specific database instance to be connected. The IP address of the database instance, the port number of the database instance, the user name of the database instance, and the name of the database instance jointly define the unique path for the application to connect to the corresponding database server.
[0050] A data source represents a database connection or multiple database connections, for example, Figure 1 The data source a is used to represent the database connection object corresponding to database 1, and the data source b is used to represent the database connection object corresponding to database 2, ..., and the database connection object corresponding to database n.
[0051] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0052] Figure 2 Schematic diagram of the database connection method provided for this application Figure 1 ,like Figure 2 As shown, the method includes:
[0053] S201: Get an access request and determine the user ID and target data source ID.
[0054] More specifically, an access request sent by a user is acquired, and a user ID and a target data source ID indicated by the access request are determined.
[0055] Optionally, before obtaining an access request, the user ID sent by the user after logging into the database management tool, the IDs of each data source to be managed, and the connection information corresponding to each data source are received, a source connection object mapping relationship is established between the data source ID and the corresponding connection information, and a connection information mapping relationship is established between the data source ID and the corresponding data source connection instance, and user interface session information is generated according to the user ID, the source connection object mapping relationship, and the connection information mapping relationship, and the user interface session information is written into the session information. At the same time, the connection information corresponding to each data source ID is stored in a lightweight database. Among them, the data source connection instance includes a source JDBC connection object, and the source JDBC connection object is a null value. This embodiment solves the technical problem of waste of database connection resources caused by the prior art pre-occupying a preset number of database connections.
[0056] Exemplarily, the data source ID is an identity code corresponding to the data source that the user needs to access, the user ID is user account information, and the access request is an http request.
[0057] Exemplarily, the source JDBC connection object in the embodiment of the present application refers to the JDBC connection of the database, and the JDBC connection is determined by the connection information of the data source corresponding to the database and the database name.
[0058] Optionally, a timeout mechanism is set for the user interface session information. When the preset time is exceeded, the source JDBC connection object in the user interface session information is automatically closed. The timeout mechanism can rely on the timeout mechanism of the session, or the timeout mechanism can be customized according to user needs. This embodiment avoids the leakage of database connection resources caused by the unclosed source JDBC connection object due to application exceptions by setting a timeout mechanism.
[0059] Exemplarily, the session information in this embodiment is determined based on the http session, and the lightweight database refers to a memory database that is different from each database managed by the application and has a persistence function, such as a derby database and a redis (Remote Dictionary Server) database. The source connection object mapping relationship and the connection information mapping relationship in this embodiment can be implemented in the form of structured documents such as json (JavaScript Object Notation, a lightweight data exchange format), xml (Extensible Markup Language, extensible markup language), and data dictionary.
[0060] Optionally, the user interface session information is saved to an in-memory database.
[0061] S202: Determine user interface session information corresponding to the user ID, and determine a target data source connection instance corresponding to the target data source ID based on a source connection object mapping relationship corresponding to the user interface session information.
[0062] More specifically, the user interface session information corresponding to the user ID is determined in the session information, and the target data source connection instance corresponding to the target data source ID is determined based on the source connection object mapping relationship corresponding to the user interface session information. The source connection object mapping relationship is the mapping relationship between the data source ID and the data source connection instance.
[0063] Optionally, if there is no target data source connection instance corresponding to the target data source ID in the user interface session information, it is determined whether there is connection information corresponding to the target data source ID based on the connection information mapping relationship corresponding to the user interface session information, where the connection information mapping relationship is a mapping relationship between the data source ID and the connection information; when it is determined that the connection information exists, a new target data source connection instance is generated according to the connection information, and the user interface session information is updated to connect to the target database according to the updated user interface session information.
[0064] Optionally, the new target data source connection instance includes a new target source JDBC connection object, and the new target source JDBC connection object is generated according to the obtained connection information and the database name indicated by the access request.
[0065] Optionally, while generating a new target data source connection instance, the existing data source connection instance in the user interface session information is not closed, so that when the data source corresponding to the database accessed by the user changes frequently, the source JDBC connection object under each data source is kept open, so as to quickly provide the user with database connections under different data sources. This solves the technical problem of the prior art that the source JDBC connection object needs to be frequently created when the data source of the database requested to be accessed changes frequently, saves system resources, and improves the database connection rate.
[0066] For the scenario where there is no target data source connection instance corresponding to the target data source ID in the user interface session information, this embodiment searches for connection information corresponding to the target data source ID in the user interface session information to generate a new target data source connection instance according to the connection information, updates the target data source connection instance to the user interface session information and connects to the corresponding database, thereby realizing accurate provision of database connection objects according to real-time user needs and improving the versatility and reliability of database connections.
[0067] Optionally, before generating a new data source connection instance based on the connection information, if it is determined that no connection information exists, the connection information corresponding to the target data source ID in the lightweight database is determined; wherein the lightweight database includes connection information corresponding to multiple data sources respectively obtained before obtaining the access request, and each connection information includes the IP address, port number and user name corresponding to the data source.
[0068] Exemplarily, for any data source, the IP address of the data source is consistent with the IP address of the database instance corresponding to the data source, the port number of the data source is consistent with the port number of the database instance corresponding to the data source, and the user name of the data source is consistent with the user name of the database instance corresponding to the data source.
[0069] This embodiment aims at the scenario where there is no target data source connection instance corresponding to the target data source ID in the user interface session information, and the connection information corresponding to the target data source ID cannot be found in the user interface session information. By obtaining the connection information corresponding to the target data source ID in a lightweight database, the target data source connection instance is ensured to be up to date, thereby improving the reliability of the database connection.
[0070] S203: When it is determined that the access request indicates the target database name, determine whether there is any source JDBC connection object in the target data source connection instance whose database name is consistent with the target database name.
[0071] More specifically, when it is determined that the access request indicates the target database name, it is determined whether there is any source JDBC connection object in the target data source connection instance whose database name is consistent with the target database name.
[0072] Optionally, if it is determined that the access request does not indicate the target database name, a database connection is made based on a source JDBC connection object that was last used in the target data source connection instance.
[0073] In a possible embodiment, based on an application scenario where the user does not specify the target database name, if it is determined that the target database name exists in the access request sent by the user, the source JDBC connection object last used in the target data source connection instance is used as the target source JDBC connection object, so as to connect to the corresponding database according to the target source JDBC connection object. In the scenario where the target database name does not exist in the access request, this embodiment improves the reliability of the database connection and the convenience of the application program accessing the data source by providing the user with the last connection object.
[0074] Optionally, if it is determined that there is no source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name, the source JDBC connection object in the target data source connection instance corresponding to the target data source ID is closed.
[0075] In a possible embodiment, in a scenario where the target database name exists in the access request sent by the user, if it is determined that the database name corresponding to any source JDBC connection object in the target data source connection instance is not consistent with the target database name, all source JDBC connection objects in the target data source connection instance corresponding to the target data source ID are closed. When it is determined that the connection information corresponding to the target data source connection instance does not exist in the current user interface session information, by obtaining the connection information corresponding to the target data source connection instance in the lightweight database, a new target data source connection instance is generated according to the connection information and the target database name indicated by the access request, and the target data source connection instance includes a new target source JDBC connection object, and the database name corresponding to the new target source JDBC connection object is consistent with the target database name indicated by the access request. This embodiment further determines whether the database name corresponding to any source JDBC connection object in the target data source connection instance is consistent with the target database name indicated by the current access request on the premise that the target data source connection instance has been determined, so that when it is determined that it is inconsistent, all source JDBC connection objects existing in the current target data source connection instance are closed in time, saving system resources.
[0076] S204. If it exists, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
[0077] More specifically, in a scenario where the target database name exists in the access request sent by the user, if it is determined that there is a source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name, then the source JDBC connection object is determined as the target source JDBC connection object, and the corresponding target database is connected through the target source JDBC connection object.
[0078] The database connection method provided in the embodiment of the present application further determines whether there is any source JDBC connection object in the target data source connection instance whose database name is consistent with the target database name indicated by the current access request, on the premise that the target data source connection instance has been determined, so that when consistency is determined, the same source JDBC connection object is provided to the user, thereby improving the database connection rate, expanding the scope of usage scenarios, improving the versatility and reliability of database connections in multiple scenarios, and enhancing user experience.
[0079] Optionally, when an access request indicates a modification to the connection information of any data source, the data source ID and modification information of the data source are determined; based on the data source ID, the connection information corresponding to the data source ID in the lightweight database is determined; based on the modification information, the connection information is updated to obtain the updated connection information in the lightweight database; and the connection information corresponding to the data source ID in the user interface session information is updated to obtain the updated connection information in the user interface session information.
[0080] When this embodiment determines that an access request indicates a modification to the connection information of a data source, the connection information corresponding to the data source ID in the lightweight database and the connection information corresponding to the data source ID are updated according to the indicated modification information, thereby achieving real-time synchronization when the data source changes, and improving the accuracy and reliability of the database connection.
[0081] Figure 3 Schematic diagram of the database connection method provided for this application Figure 2 ,like Figure 3 As shown, in this embodiment Figure 2 Based on the embodiment, the database connection method is described in detail, and the method includes:
[0082] S301: Get access request.
[0083] More specifically, an access request is acquired, and the user ID and target data source ID indicated by the access request are obtained.
[0084] S302: Determine user interface session information corresponding to the user ID.
[0085] More specifically, the user interface session information corresponding to the user ID is searched in the session information according to the user ID.
[0086] S303: Determine the target data source connection instance corresponding to the target data source ID.
[0087] More specifically, the target data source connection instance corresponding to the target data source ID is determined in the user interface session information, and multiple source JDBC connection objects consistent with the connection information corresponding to the target data source ID are obtained, wherein different source JDBC connection objects are distinguished by database names.
[0088] S304: Determine whether the target data source connection instance is valid.
[0089] More specifically, whether the target data source connection instance is valid is determined by judging whether the time for writing session information of the currently obtained target data source connection instance exceeds a preset time.
[0090] Optionally, when it is determined that the preset time has not been exceeded, it is determined that the target data source connection instance is valid, and step S305 is executed.
[0091] Optionally, when it is determined that the preset time has been exceeded, it is determined that the target data source connection instance is invalid, and step S306 is executed.
[0092] S305: When it is determined to be valid, determine whether the access request indicates a target database name.
[0093] More specifically, when it is determined that the target data source connection instance is valid, it is determined whether the access request indicates the target database name.
[0094] Optionally, when it is determined to indicate the name of the target database, step S307 is executed.
[0095] Optionally, when it is determined that the target database name is not indicated, step S309 is executed.
[0096] S306: Close the source JDBC connection object in the target data source connection instance.
[0097] More specifically, when it is determined that the target data source connection instance is invalid, each source JDBC connection object in the current target data source connection instance is closed, and then steps S310-S313 are executed.
[0098] Optionally, after determining the target data source connection instance and the target database name, if there is no database name corresponding to any source JDBC connection object in the target data source connection instance that is consistent with the target database name, it is determined that the user currently needs to connect to a new database under the target data source connection instance, and it is further determined that there is no need to use the existing source JDBC connection object in the target data source connection instance temporarily. At this time, each source JDBC connection object in the target data source connection instance is closed to save system resources.
[0099] S307: Determine whether there is any source JDBC connection object whose corresponding database name is consistent with the target database name.
[0100] More specifically, when it is determined that the access request indicates the target database name, it is determined whether there is any source JDBC connection object in the target data source connection instance whose database name is consistent with the target database name.
[0101] Optionally, when it is determined that the database name corresponding to any source JDBC connection object in the target data source connection instance is consistent with the target database name, step S308 is executed.
[0102] Optionally, when it is determined that there is no database name corresponding to any source JDBC connection object in the target data source connection instance that is consistent with the target database name, step S306 is executed.
[0103] S308. If it exists, determine the source JDBC connection object corresponding to the target database name target in the target data source connection instance.
[0104] More specifically, when it is determined that the database name corresponding to any source JDBC connection object in the target data source connection instance is consistent with the target database name, the source JDBC connection object corresponding to the target database name in the target data source connection instance is determined to obtain the target source JDBC connection object. Then, step S313 is executed.
[0105] S309: If it does not exist, determine the source JDBC connection object that was last used in the target data source connection instance.
[0106] More specifically, when the access request does not indicate the target database name, the source JDBC connection object used last time in the currently obtained target data source connection instance is determined, and the source JDBC connection object is used as the target source JDBC connection object. Then, step S313 is executed.
[0107] S310: Determine the connection information corresponding to the target data source ID.
[0108] More specifically, when the connection information corresponding to the target data source ID exists in the user interface session information, the connection information corresponding to the target data source ID is obtained. Otherwise, the connection information corresponding to the target data source ID is obtained in the lightweight database. Then, step S311 is executed.
[0109] S311. Generate a new target data source connection instance according to the connection information.
[0110] More specifically, when the connection information is obtained and the access request indicates the target database name, a new target source JDBC connection object corresponding to the target database is generated according to the connection information and the target database name, and a new target data source connection instance corresponding to the target data source ID is obtained. Then, step S312 is executed.
[0111] S312: Update the user interface session information and determine a new target source JDBC connection object.
[0112] More specifically, the user interface session information is updated with the new target data source connection instance, and the target source JDBC connection object is obtained. Then, step S313 is executed.
[0113] Optionally, if the connection information in step S310 is obtained from a lightweight database, the connection information is updated to the mapping relationship of the target data source ID in the user interface session information.
[0114] S313. Connect to the target database.
[0115] More specifically, the target database is connected according to the target source JDBC connection object.
[0116] The database connection method provided in the embodiment of the present application determines whether there is a source JDBC connection object that meets the access request in the user interface session information. When it is determined that the connection information and database name corresponding to any source JDBC connection object are consistent with the connection information and target database name corresponding to the data source ID, the source JDBC connection object is used to connect to the database. This ensures that the same user can use the same source JDBC connection object every time when the data source remains unchanged, thereby improving the user's database connection convenience and connection speed, enhancing the user experience, and satisfying user needs through a new connection when there is no source JDBC connection object that meets the access request.
[0117] Figure 4 The schematic diagram of the structure of the database connection device provided by this application is as follows: Figure 4 As shown, the database connection device 40 provided in this embodiment includes:
[0118] The acquisition module 401 is used to acquire the access request sent by the user and determine the user ID and the target data source ID indicated by the access request;
[0119] Processing module 402 is used to determine the user interface session information corresponding to the user ID in the session information, and determine the target data source connection instance corresponding to the target data source ID based on the source connection object mapping relationship corresponding to the user interface session information, where the source connection object mapping relationship is a mapping relationship between the data source ID and the data source connection instance;
[0120] Optionally, processing module 402 is further used to determine whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name when it is determined that the access request indicates the target database name; if so, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
[0121] Optionally, the processing module 402 is further configured to determine whether there is connection information corresponding to the target data source ID based on a connection information mapping relationship corresponding to the user interface session information when there is no target data source connection instance corresponding to the target data source ID in the user interface session information, where the connection information mapping relationship is a mapping relationship between the data source ID and the connection information;
[0122] When it is determined that the connection information exists, a new target data source connection instance is generated according to the connection information, and the user interface session information is updated to connect to the target database according to the updated user interface session information.
[0123] Optionally, the processing module 402 is also used to determine the connection information corresponding to the target data source ID in the lightweight database before generating a new data source connection instance based on the connection information, if it is determined that the connection information does not exist; wherein the lightweight database includes connection information corresponding to multiple data sources respectively obtained before obtaining the access request, and each connection information includes the IP address, port number and user name corresponding to the data source.
[0124] Optionally, processing module 402 is further configured to close the source JDBC connection object in the target data source connection instance corresponding to the target data source ID when it is determined that there is no source JDBC connection object in the target data source connection instance whose database name is consistent with the target database name.
[0125] Optionally, the processing module 402 is further configured to perform a database connection based on a source JDBC connection object last used in the target data source connection instance when it is determined that the access request does not indicate a target database name.
[0126] Optionally, the processing module 402 is further configured to determine the data source ID and modification information of the data source when the access request indicates modification of the connection information of any data source;
[0127] Based on the data source ID, determine the connection information corresponding to the data source ID in the lightweight database, update the connection information based on the modification information, and obtain the updated connection information in the lightweight database;
[0128] The connection information corresponding to the data source ID in the user interface session information is updated to obtain the updated connection information in the user interface session information.
[0129] The database connection device provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and this embodiment will not be described in detail here.
[0130] Figure 5 This is a schematic diagram of the structure of the electronic device provided in this application. Figure 5 As shown, the electronic device 50 provided in this embodiment includes: at least one processor 501 and a memory 502. Optionally, the device 50 also includes a communication component 503. The processor 501, the memory 502 and the communication component 503 are connected via a bus 504.
[0131] In a specific implementation process, at least one processor 501 executes the computer-executable instructions stored in the memory 502, so that at least one processor 501 executes the above method.
[0132] The specific implementation process of the processor 501 can be found in the above method embodiment, and its implementation principle and technical effect are similar, so this embodiment will not be repeated here.
[0133] In the above embodiments, it should be understood that the processor can be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in the invention can be directly implemented as a hardware processor, or can be implemented by a combination of hardware and software modules in the processor.
[0134] The memory may include a high-speed memory (Random Access Memory, RAM), and may also include a non-volatile memory (NVM), such as at least one disk storage.
[0135] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of this application is not limited to only one bus or one type of bus.
[0136] The present application also provides a computer program product, including a computer program, which implements the above method when executed by a processor.
[0137] The present application also provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, the above method is implemented.
[0138] The above-mentioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The readable storage medium can be any available medium that can be accessed by a general or special-purpose computer.
[0139] An exemplary readable storage medium is coupled to a processor so that the processor can read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can be located in an application specific integrated circuit (Application Specific Integrated Circuits, referred to as: ASIC). Of course, the processor and the readable storage medium can also exist in the device as discrete components.
[0140] The division of units is only a logical function division, and there may be other divisions in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interface, device or unit, which can be electrical, mechanical or other forms.
[0141] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0142] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0143] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0144] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.
[0145] Finally, it should be noted that those skilled in the art will readily conceive of other embodiments of the present invention after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses or adaptations of the present invention, which follow the general principles of the present invention and include common knowledge or customary technical means in the art not disclosed by the present invention, are not limited to the precise structure described above and shown in the drawings, and may be modified and changed in various ways without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.
Claims
1. A database connection method, characterized in that: include: Obtaining an access request sent by a user, and determining a user ID and a target data source ID indicated by the access request; Determine the user interface session information corresponding to the user ID in the session information, and determine the target data source connection instance corresponding to the target data source ID based on the source connection object mapping relationship corresponding to the user interface session information, wherein the source connection object mapping relationship is a mapping relationship between the data source ID and the data source connection instance; When it is determined that the access request indicates the target database name, determine whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name; if so, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
2. The method according to claim 1, characterized in that Also includes: If the target data source connection instance corresponding to the target data source ID does not exist in the user interface session information, determining whether there is connection information corresponding to the target data source ID based on the connection information mapping relationship corresponding to the user interface session information, where the connection information mapping relationship is a mapping relationship between the data source ID and the connection information; When it is determined that the connection information exists, a new target data source connection instance is generated according to the connection information, and the user interface session information is updated to connect to the target database according to the updated user interface session information.
3. The method according to claim 2, characterized in that Also includes: Before generating a new data source connection instance according to the connection information, if it is determined that the connection information does not exist, the connection information corresponding to the target data source ID in the lightweight database is determined; wherein the lightweight database includes connection information corresponding to multiple data sources respectively obtained before obtaining the access request, and each connection information includes the IP address, port number and user name corresponding to the data source.
4. The method according to claim 2, characterized in that: Also includes: If it is determined that there is no source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name, the source JDBC connection object in the target data source connection instance corresponding to the target data source ID is closed.
5. The method according to claim 1, characterized in that: Also includes: If it is determined that the access request does not indicate the target database name, a database connection is performed based on the source JDBC connection object last used in the target data source connection instance.
6. The method according to claim 1, characterized in that Also includes: When the access request indicates that the connection information of any data source is to be modified, determining the data source ID and modification information of the data source; Based on the data source ID, determining the connection information corresponding to the data source ID in the lightweight database, and updating the connection information based on the modification information to obtain the updated connection information in the lightweight database; The connection information corresponding to the data source ID in the user interface session information is updated to obtain the updated connection information in the user interface session information.
7. A database connection device, characterized in that: include: An acquisition module, used to acquire an access request sent by a user, and determine a user ID and a target data source ID indicated by the access request; a processing module, configured to determine the user interface session information corresponding to the user ID in the session information, and determine the target data source connection instance corresponding to the target data source ID based on the source connection object mapping relationship corresponding to the user interface session information, wherein the source connection object mapping relationship is a mapping relationship between the data source ID and the data source connection instance; The processing module is further used to determine whether there is any source JDBC connection object in the target data source connection instance whose corresponding database name is consistent with the target database name when it is determined that the access request indicates the target database name; if so, obtain the target source JDBC connection object; and connect to the target database based on the target source JDBC connection object.
8. An electronic device, characterized in that: include: Memory, processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory, so that the processor performs the method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 6 when executed by a processor.
10. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 6 when being executed by a processor.