Database connection pool assembly suitable for QtSQL and access method
By designing a database connection pool component based on thread local storage, the problem that Qt SQL cannot integrate with the open source database connection pool component is solved, dynamic management and health check of database connections are realized, and the performance and resource utilization efficiency of the software system are improved.
Patent Information
- Application Number
- CN202411969832.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-05-09
AI Technical Summary
In the prior art, Qt SQL cannot be integrated with open source database connection pool components, resulting in the need to manually manage database connections when accessing the database, the code is cumbersome and easy to cause resource leakage.
Design a database connection pool component suitable for Qt SQL, using thread local storage to manage database connections, including global QSqlDatabase and thread local storage QThreadStorage, dynamically create and release database connections, and perform health checks before the connection returns.
It realizes seamless integration of Qt SQL and database connection pool components, improves software system performance by multiplexing database connections, avoids resource leakage, and automatically releases database connections at the end of the thread.
Smart Images

Figure CN119961334A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development based on Qt, and in particular to a database connection pool component and an access method suitable for Qt SQL. Background Art
[0002] When accessing a database, you need to create a database connection and release it after use. Creating and releasing database connections is actually time-consuming and expensive. When accessing the database in large quantities, if you repeatedly create and release database connections each time, it will have a great impact on the performance of the entire software system. The database connection pool component is responsible for the management and allocation of database connections, allowing applications to reuse database connections, thereby improving the performance of the software system.
[0003] The open source database connection pool component pre-creates a certain number of database connections, puts them into a shared queue, and assumes that each database connection can be used in any thread. However, for the sake of concurrency safety, the database connections created using Qt SQL have "thread affinity", that is, the database connection created by a thread is only allowed to be used in this thread and not across threads. This limitation makes it impossible for Qt SQL to be integrated with the open source database connection pool component.
[0004] Regarding the above-mentioned related technologies, it is found that when Qt SQL is actually used, if the database connection pool component is not used, it is necessary to manually manage the database connection directly in the application. The code is very cumbersome and it is easy to cause resource leakage, and the related code cannot be reused in other software. Summary of the invention
[0005] In order to use a database connection pool component when using Qt SQL, the present application provides a database connection pool component and an access method suitable for Qt SQL.
[0006] The present invention is achieved in that:
[0007] A database connection pool component suitable for Qt SQL, the database connection pool component is based on thread local storage, and includes:
[0008] Global QSqlDatabase: used to store all database connections used by all threads;
[0009] Thread local storage QThreadStorage: used to store the database connection name used by each thread;
[0010] Data source: Data source represents the configuration of database connection;
[0011] Data source name: The Key of QMap is the data source name;
[0012] Value: database connection name.
[0013] Further, it specifically includes:
[0014] Creation of database connection: During initialization, only the configuration information of the data source is read from the configuration file. During the operation of the software, a database connection is dynamically created according to the access requirements of each thread. When a thread needs to access a data source, the getConnection interface function of this component is called;
[0015] Health check of database connection: Before returning the database connection to the application, this component automatically checks the health status of the database connection:
[0016] If there is no abnormality in the database connection, it returns directly;
[0017] Otherwise, first release the old database connection with the exception, then create and return a new database connection.
[0018] Release of database connections: When a thread ends execution, the close slot function of this component automatically releases all database connections that have been created for the current thread.
[0019] Furthermore, the getConnection interface function is as follows:
[0020] If this component has already created a database connection for the data source of the current thread, the thread-local storage QThreadStorage and the global QSqlDatabase are cascaded to reuse the created database connection;
[0021] Otherwise, this component first creates a new database connection in the global QSqlDatabase according to the configuration information of the data source, and then stores the correspondence between the data source name and the database connection name in the thread local storage QThreadStorage. The database connection name is represented by UUID and is invisible to the application.
[0022] If a thread does not need to access the database, this component will not create any database connection for it, avoiding unnecessary waste of resources.
[0023] Furthermore, the release using the close slot function includes the following two situations:
[0024] If it is a UI thread, you need to connect the aboutToQuit signal of QCoreApplication to the close slot function;
[0025] If it is a background thread, you need to connect the finished signal of QThread to the close slot function.
[0026] Furthermore, the data source represents the configuration of a database connection. Different data sources may be connections to databases of different types or connections to databases of the same type. They may be connections to different database services or connections to the same database service.
[0027] Furthermore, the configuration information of the data source is stored in a file in JSON format, and the fields are as follows:
[0028] dataSourceName: data source name;
[0029] driver: Qt SQL driver for the database;
[0030] hostName: the domain name or IP address of the database;
[0031] port: the port number of the database;
[0032] userName: the user name for logging into the database;
[0033] password: password for logging into the database;
[0034] databaseName: database name;
[0035] connectOptions: Database connection options. Different databases use different options.
[0036] Furthermore, the connection options of the database include a connection timeout or a query timeout.
[0037] A method for thread accessing a data source applicable to Qt SQL comprises the following steps:
[0038] First, get the database connection name from the thread local storage QThreadStorage according to the data source name;
[0039] Then, get the database connection once created in the current thread from the global QSqlDatabase according to the database connection name.
[0040] Compared with the prior art, the beneficial effects of the present invention are: the database connection pool component of the present application can be seamlessly integrated with Qt SQL, providing database connection pool functions for Qt-based applications, improving the performance of the software system by reusing database connections, and during use, the component dynamically creates database connections according to the access requirements of threads, and also has the functions of automatically checking the health status of database connections and automatically releasing database connections when the thread ends execution. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0042] Figure 1 It is the overall architecture diagram of the present invention. DETAILED DESCRIPTION
[0043] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention.
[0044] Reference Figure 1 As shown, a database connection pool component suitable for Qt SQL, the database connection pool component is based on thread local storage and includes:
[0045] Data source: A data source represents the configuration of a database connection. Different data sources can be connections to different types of databases or the same type of databases. They can be connections to different database services or the same database service.
[0046] The data source configuration information is stored in a JSON format file with the following fields:
[0047] dataSourceName: data source name.
[0048] driver: The Qt SQL driver for the database.
[0049] hostName: The domain name or IP address of the database.
[0050] port: The port number of the database.
[0051] userName: User name for logging into the database.
[0052] password: the password for logging into the database.
[0053] databaseName: database name.
[0054] connectOptions: Database connection options. Different databases use different options, such as connection timeout, query timeout, etc.
[0055] Database connection:
[0056] All database connections used by all threads are stored in the global QSqlDatabase, and the database connection name used by each thread is stored in the thread-local storage QThreadStorage, where the Key of QMap is the data source name and the Value is the database connection name.
[0057] The definition is as follows:
[0058] QThreadStorage <QMap<QString
[0059] QString>*>connectionNames.
[0060] When a thread needs to access a data source,
[0061] First, get the database connection name from the thread local storage QThreadStorage according to the data source name.
[0062] Then, get the database connection once created in the current thread from the global QSqlDatabase according to the database connection name.
[0063] The database connection of QtSQL is a "value class". If you are not careful when using it, it will cause resource leakage.
[0064] According to the design of this component, the thread-local storage QThreadStorage does not directly store the database connection, but only stores the database connection name, and the database connection is only stored in the global QSqlDatabase and obtained on demand, which greatly simplifies the code logic of database connection management and allocation and completely avoids resource leakage.
[0065] According to Qt's official documentation, QThreadStorage is definitely "thread safe", and QSqlDatabase is also "thread safe", thus ensuring the concurrency safety of this component.
[0066] Creating a database connection:
[0067] Unlike open source database connection pool components, which create a certain number of database connections in advance, this component only reads the configuration information of the data source from the configuration file during initialization. During the operation of the software, it dynamically creates database connections according to the access requirements of each thread. When a thread needs to access a data source, it calls the getConnection interface function of this component:
[0068] If this component has already created a database connection for the data source of the current thread, the thread-local storage QThreadStorage and the global QSqlDatabase are cascaded to reuse the created database connection.
[0069] Otherwise, this component first creates a new database connection in the global QSqlDatabase according to the configuration information of the data source, and then stores the correspondence between the data source name and the database connection name in the thread local storage QThreadStorage. The database connection name is represented by UUID and is invisible to the application.
[0070] Among them, UUID (Universally Unique Identifier) is a standardized identification code used to uniquely identify information in a computer system. UUID is a 128-bit identifier consisting of 32 hexadecimal digits, usually separated by 5 parts, in the form of a string of "8-4-4-4-12".
[0071] The purpose of UUID is to generate globally unique identifiers in distributed systems without relying on centralized identity allocation management. Its generation algorithm is based on multiple factors such as timestamp, generator's unique identifier and hardware information, making it very unlikely that UUID will be repeated.
[0072] UUID is widely used in various software systems. In a distributed system, each node can use UUID to uniquely identify itself, avoiding centralized identity allocation management. In database systems, UUID is often used as a primary key to ensure the uniqueness of records in a distributed environment. In addition, UUID is also commonly used to build a unique session identifier, generate a unique file name, mark resources, and other scenarios.
[0073] If a thread does not need to access the database, this component will not create any database connection for it, avoiding unnecessary waste of resources.
[0074] Health check of database connection:
[0075] Before returning the database connection to the application, this component automatically checks the health status of the database connection:
[0076] If there is no abnormality in the database connection, it returns directly.
[0077] Otherwise, first release the old database connection with the exception, then create and return a new database connection.
[0078] Release of database connection:
[0079] Because the database connection of this component is essentially stored by thread, the application does not need to return the database connection after using it.
[0080] When a thread ends execution, the close slot function of this component automatically releases all database connections that have been created for the current thread.
[0081] Because of the particularity of Qt's UI thread, there is a key point to note when implementing it:
[0082] If it is a UI thread, you need to connect the aboutToQuit signal of QCoreApplication to the close slot function.
[0083] If it is a background thread, you need to connect the finished signal of QThread to the close slot function.
[0084] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A database connection pool component suitable for Qt SQL, characterized in that: The database connection pool component is based on thread local storage and includes: Global QSqlDatabase: used to store all database connections used by all threads; Thread local storage QThreadStorage: used to store the database connection name used by each thread; Data source: Data source represents the configuration of database connection; Data source name: The Key of QMap is the data source name; Value: database connection name.
2. A database connection pool component suitable for Qt SQL according to claim 1, characterized in that: Specifically include: Creation of database connection: During initialization, only the configuration information of the data source is read from the configuration file. During the operation of the software, a database connection is dynamically created according to the access requirements of each thread. When a thread needs to access a data source, the getConnection interface function of this component is called; Health check of database connection: Before returning the database connection to the application, this component automatically checks the health status of the database connection: If there is no abnormality in the database connection, it returns directly; Otherwise, first release the old database connection with the exception, then create and return a new database connection. Release of database connections: When a thread ends execution, the close slot function of this component automatically releases all database connections that have been created for the current thread.
3. A database connection pool component suitable for Qt SQL according to claim 2, characterized in that: The getConnection interface function is as follows: If this component has already created a database connection for the data source of the current thread, the thread-local storage QThreadStorage and the global QSqlDatabase are cascaded to reuse the created database connection; Otherwise, this component first creates a new database connection in the global QSqlDatabase according to the configuration information of the data source, and then stores the correspondence between the data source name and the database connection name in the thread local storage QThreadStorage. The database connection name is represented by UUID and is invisible to the application. If a thread does not need to access the database, this component will not create any database connection for it, avoiding unnecessary waste of resources.
4. A database connection pool component suitable for Qt SQL according to claim 2, characterized in that: There are two cases when releasing using the close slot function: If it is a UI thread, you need to connect the aboutToQuit signal of QCoreApplication to the close slot function; If it is a background thread, you need to connect the finished signal of QThread to the close slot function.
5. A database connection pool component suitable for Qt SQL according to claim 1, characterized in that: The data source represents the configuration of a database connection. Different data sources can be connections to databases of different types or the same type, and can be connections to different database services or the same database service.
6. A database connection pool component suitable for Qt SQL according to claim 1, characterized in that: The configuration information of the data source is stored in a JSON format file, and the fields are as follows: dataSourceName: data source name; driver: Qt SQL driver for the database; hostName: the domain name or IP address of the database; port: the port number of the database; userName: the user name for logging into the database; password: password for logging into the database; databaseName: database name; connectOptions: Database connection options. Different databases use different options.
7. A database connection pool component suitable for Qt SQL according to claim 3, characterized in that: The connection options of the database include a connection timeout or a query timeout.
8. A method for thread accessing data source applicable to Qt SQL, characterized in that: The steps include: First, get the database connection name from the thread local storage QThreadStorage according to the data source name; Then, get the database connection once created in the current thread from the global QSqlDatabase according to the database connection name.