Multi-version dynamically scalable data source method, device, terminal, and storage medium
By creating a JAVA SDK package, dynamic expansion of multi-version databases is supported, solving the problem of the inability to dynamically expand data sources in existing technologies, achieving system flexibility and efficient management, and improving system performance and stability.
Patent Information
- Application Number
- CN202510764624.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-10
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2045-06-10
AI Technical Summary
Existing technologies cannot support the dynamic expansion of multi-version databases, resulting in poor system flexibility and maintainability, making it difficult to adapt to complex and changing business needs.
Provides a multi-version dynamically extensible data source method by creating a JAVA SDK package, which includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, and target data source mapping. It supports dynamic loading and management of data sources and uses thread isolation mechanism to avoid mutual interference between different data sources.
It improves the flexibility and adaptability of the system, supports dynamic expansion of new database connection pools, reduces the overhead of connection establishment and destruction, improves system performance and stability, and simplifies the data source management process.
Smart Images

Figure CN120315681B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of database data source loading, and in particular to a multi-version dynamically scalable data source method, device, terminal, and storage medium. Background Art
[0002] Traditional database connection management methods, such as statically configuring data sources, are simple and easy to use, but lack flexibility and are difficult to adapt to rapidly changing requirements. For example, in applications using the Spring framework, data source information is often configured through application.properties or application.yml files. This approach may be sufficient in the early stages of development, but as the project scales and the number of environments increases, manually managing and maintaining these configuration files becomes cumbersome and error-prone.
[0003] Sharding-JDBC and MyCAT, among other database and table sharding middleware, have addressed horizontal scalability to a certain extent. They distribute data across multiple database instances using sharding rules, improving the system's concurrent processing capabilities and storage capacity. However, these solutions primarily focus on the physical distribution of data and are unable to meet requirements such as dynamically switching data sources and supporting multi-version database access.
[0004] The Spring Cloud Configuration Center provides a centralized way to manage configuration information, enabling dynamic refresh of application configurations, including data source configurations. While this improves configuration flexibility, developers still need to write additional logic at the application level to handle data source switching. Furthermore, it typically supports only a limited number of data sources, making it difficult to meet complex and changing business needs, and it cannot support multiple data sources simultaneously.
[0005] Using the @Primary and @Qualifier annotations for multiple data source configuration, although you can register multiple data sources in the Spring container and specify the default data source and select a specific data source through annotations, this method is still a static configuration and cannot dynamically change the data source at runtime. It also easily leads to a high degree of coupling between code and configuration, which is not conducive to maintenance and expansion.
[0006] Therefore, the market urgently needs a technical solution that can support multiple versions and dynamically expand data sources. Summary of the Invention
[0007] The present application provides a method, device, terminal and storage medium for a multi-version dynamically scalable data source to solve the problem that the prior art cannot support multi-version dynamic scalability of data sources.
[0008] In a first aspect, the present application provides a multi-version dynamically scalable data source method, comprising:
[0009] Create a JAVA SDK package, which includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, basic properties of the routing data source classes, and target data source mappings. The target data source mappings are used to store loaded database connection pools.
[0010] Initialize the system database connection pool in the application system based on the JAVA SDK package;
[0011] After the system database connection pool is initialized in the application system, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package.
[0012] In a second aspect, the present application provides a multi-version dynamically scalable data source device, comprising:
[0013] A creation module is used to create a JAVA SDK package. The JAVA SDK package includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, basic properties of the routing data source classes, and target data source mappings. The target data source mappings are used to store loaded database connection pools.
[0014] An initialization module, used to initialize the system database connection pool in the application system based on the JAVA SDK package;
[0015] The expansion module is used to dynamically expand a new database connection pool in the application system based on the JAVA SDK package after the system database connection pool in the application system is initialized.
[0016] In a third aspect, the present application provides a terminal comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the steps of the method described in the first aspect or any possible implementation of the first aspect are implemented.
[0017] In a fourth aspect, the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the steps of the method described in the first aspect or any possible implementation of the first aspect.
[0018] The present application provides a multi-version dynamically extensible data source method, device, terminal and storage medium. By creating a JAVA SDK package, the JAVA SDK package includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, and basic properties and target data source mappings of the routing data source classes. The target data source mapping is used to store loaded database connection pools. Based on the JAVA SDK package, the system database connection pool in the application system is initialized. After the system database connection pool in the application system is initialized, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package. This application provides developers with a powerful toolset by creating a JAVA SDK package that includes key components such as core dependencies, core entity classes, and core interface classes. This allows developers to easily meet the needs of different data sources and database versions, and supports dynamic expansion of new database connection pools, which means that the system can add new data sources according to actual needs without interrupting service, thereby improving system flexibility and adaptability. In addition, the introduction of the database connection pool management core class enables the system to efficiently manage and reuse database connections, reducing the overhead of connection establishment and destruction, thereby improving system performance. The thread isolation mechanism ensures that operations of different data sources do not interfere with each other, further enhancing the system's concurrent processing capabilities and stability. In addition, the provision of routing data sources and their basic properties and target data source mappings makes data source configuration and management simpler and more intuitive. Developers only need to configure routing rules to achieve data source switching and load balancing without complex code modifications. The storage mechanism of target data source mappings enables the system to quickly identify and reuse already loaded database connection pools, thereby simplifying the data source management process. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments or descriptions of the prior art. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0020] Figure 1 This is a flowchart of the implementation of the multi-version dynamically scalable data source method provided in the embodiment of the present application;
[0021] Figure 2 This is an overview diagram of the database connection pool provided in an embodiment of the present application;
[0022] Figure 3 This is an overview diagram of the interaction between the JAVA SDK package and the application system provided in the embodiment of the present application;
[0023] Figure 4 This is a detailed workflow diagram for dynamically loading a database to create a database connection pool, as provided in an embodiment of the present application;
[0024] Figure 5 This is a detailed flow chart of the dynamically expanded new database connection pool provided by an embodiment of the present application;
[0025] Figure 6 This is a detailed flowchart of removing a database connection pool provided in an embodiment of the present application;
[0026] Figure 7 This is a schematic diagram of the structure of a multi-version dynamically scalable data source device provided in an embodiment of the present application;
[0027] Figure 8 It is a schematic diagram of a terminal provided in an embodiment of the present application. DETAILED DESCRIPTION
[0028] In the following description, specific details such as specific system structures and techniques are provided for purposes of illustration rather than limitation to facilitate a thorough understanding of the embodiments of the present application. However, it will be apparent to those skilled in the art that the present application may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid obscuring the description of the present application with unnecessary detail.
[0029] In order to make the purpose, technical solutions and advantages of this application clearer, specific embodiments will be described below with reference to the accompanying drawings.
[0030] In order to solve the problem that the existing technology cannot support multi-version dynamic expansion of data sources, it is necessary to provide a solution that can seamlessly integrate with existing persistence frameworks (such as Spring Boot, Mybatis, JPA, JDBC, Hibernate, etc.) to realize dynamic loading, switching and management of data sources, while maintaining loose coupling with business code to improve the flexibility and maintainability of the system. Based on the above requirements, this application provides a flexible and efficient data source management solution that can meet the data management and processing needs in complex business scenarios.
[0031] Figure 1 The implementation flow chart of the multi-version dynamically scalable data source method provided in the embodiment of this application is detailed as follows:
[0032] In step 101, a JAVA SDK package is created. The JAVA SDK package includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data sources, basic properties of routing data sources, and target data source mapping. The target data source mapping is used to store the loaded database connection pool.
[0033] Among them, the embodiments of the present application are mainly implemented in a multi-version, multi-instance, and multi-database environment of GBase8s, and other databases are similar.
[0034] In this embodiment of the present application, a JAVA SDK package is created that includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data sources, and their basic properties and target data source mapping. The target data source mapping is used to store the loaded database connection pool. For a detailed overview of the database connection pool, refer to Figure 2 .
[0035] The embodiment of the present application adopts a loosely coupled design concept to improve the flexibility and scalability of the system. That is, by integrating the JAVA SDK package, users only need to add a small amount of configuration code to load multi-version database connection pools, greatly reducing the complexity and development cost of system integration.
[0036] In addition, the embodiment of the present application utilizes the JAVA SDK package and can be used in combination with a variety of persistence frameworks (such as Mybatis, JPA, JDBC, Hibernate, etc.), so that the system can adapt to different application scenarios and needs more flexibly, thereby improving the versatility and practicality of the system.
[0037] In one possible implementation, creating a JAVA SDK package may include:
[0038] Build a Maven project and add core dependencies to the Maven project;
[0039] After adding core dependencies to the Maven project, define the core entity class, core interface class, and database connection pool management core class respectively;
[0040] Configure thread isolation;
[0041] Customize the routing data source class, define the basic properties of the routing data source class, and define the target data source mapping. The target data source mapping is used to store the loaded database connection pool.
[0042] Maven is an open-source project management tool based on the Project Object Model (POM) and developed purely in Java. It's primarily used to manage Java projects, including dependency management (JAR package dependency management) and project builds (project compilation, packaging, testing, and deployment). It also allows for modular development to improve efficiency.
[0043] Optionally, use Maven as a project management tool and build a Maven project to facilitate dependency management and construction.
[0044] After the Maven project is built, add two core dependencies: spring-jdbc and druid. Spring-jdbc is the JDBC support provided by the Spring Framework, and druid is a database connection pool implementation.
[0045] The purpose of adding core dependencies in this embodiment of the application is:
[0046] (1) Simplify database operations: spring-jdbc provides an abstraction of JDBC, making database operations simpler and more unified.
[0047] (2) Database connection pool management: Druid is an implementation of a database connection pool that provides powerful monitoring and scaling capabilities. Using a database connection pool can significantly improve the performance of database operations because it allows you to reuse already established database connections instead of creating new connections for each database operation. The connection pool can also help limit the number of database connections open at any one time, preventing excessive database connections from exhausting database server resources.
[0048] (3) Integrate Spring ecosystem: If the project is already using the Spring framework, then integrating spring-jdbc and druid will be more natural because they are both part of the Spring ecosystem and can work well with other Spring components.
[0049] After adding core dependencies to the Maven project, the embodiment of the present application also defines core entity classes, core interface classes and database connection pool management core classes.
[0050] Among them, the core entity class, namely DataSourceConfigInfo, contains the basic information required for database connection, such as server address IP, database port PORT, database account, database password, instance name, database name, database version, database driver address, and the full path of the Driver class.
[0051] The core interface class, DataSourceService, defines a method for obtaining basic information about the existing databases that need to be managed in the system. This allows the service layer or control layer to query and manage database configurations.
[0052] Then, the embodiment of the present application configures thread isolation by using the thread local variable type attributes of different data sources to configure the isolation of the corresponding data sources between different threads, that is, through ThreadLocal <string>The dataSourceKeyHolder property implements thread isolation.
[0053] ThreadLocal is a thread-local variable provided by Java. Each thread has its own independent copy of the variable, preventing interference. In this scenario, dataSourceKeyHolder is used to store the data source key that the current thread should use. The data source key is (database type: database version: server address: database port: instance name: database name).
[0054] The purpose of configuring thread isolation in this embodiment of the application is to:
[0055] (1) Maintaining an independent data source key: Each thread can independently set and obtain its own data source key without conflicting or interfering with the data source keys of other threads.
[0056] (2) Implementing dynamic data source switching: In application scenarios with multiple data sources, the current thread can dynamically switch data sources as needed. For example, when processing requests from different users, it may be necessary to select different data sources based on certain user attributes (such as region, tenant ID, etc.). Through ThreadLocal, each thread can independently manage these data source keys, thereby implementing dynamic data source switching.
[0057] This embodiment of the application also requires a custom RoutingDataSource class. This class maintains a mapping between data source keys and data sources. It inherits from AbstractRoutingDataSource, an abstract class provided by the Spring Framework that implements the functionality of a RoutingDataSource, which selects a data source based on a strategy (usually through thread-local variables). In the RoutingDataSource class, override the determineCurrentLookupKey method to determine which data source the current thread should use. This method returns a key (usually a string) that is used to retrieve the actual data source from the data source mapping.
[0058] For example, the following program is used to represent a custom routing data source class:
[0059] private static final ThreadLocal <string>dataSourceKeyHolder = newThreadLocal<>();
[0060] / / Custom routing data source class, which inherits from AbstractRoutingDataSource
[0061] public static class RoutingDataSource extendsAbstractRoutingDataSource {
[0062] @Override
[0063] protected Object determineCurrentLookupKey() {
[0064] String currentKey = dataSourceKeyHolder.get();
[0065] if (currentKey == null) {
[0066] / / Set the default data source key, such as "master"
[0067] currentKey = masterKey;
[0068] }
[0069] return currentKey;
[0070] }
[0071] }
[0072] After customizing the routing data source class, this embodiment of the application also needs to customize the basic properties of the routing data source class, which are used to manage all database connection pool information. Then, it is necessary to define the target data source map ConcurrentHashMap targetDataSources to store the loaded database connection pool.
[0073] For example, the implementation method of loading the JAVA SDK package by the application is as follows:
[0074] This example uses Spring Boot and Mybatis for persistence, using a MySQL database to store business data. If you need to use another persistence framework (such as Hibernate or JPA), simply configure and integrate it according to the corresponding framework documentation without having to repeat the entire implementation process.
[0075] Build Maven project: Use Maven as a project management tool to facilitate dependency management and construction.
[0076] Add core dependencies: Add SDK, MyBatis, and MySQL core dependencies to the project.
[0077] Configure the data source: Configure the MySQL database connection information in the application.properties or application.yml file, including the URL, username, password, etc. If it is consistent with the normal Spring Boot, no changes are required. That is:
[0078] spring:
[0079] datasource:
[0080] type: com.alibaba.druid.pool.DruidDataSource
[0081] driver-class-name: com.mysql.cj.jdbc.Driver
[0082] url: jdbc:mysql: / / 192.168.202.140:3306 / demo
[0083] username: root
[0084] password: root
[0085] Implement the getDataSourceInfos interface of the core class DataSourceService. This interface can obtain a list of basic database connection information that needs to be dynamically initialized, including the server address IP, database port PORT, database account, database password, instance name, database name, database type, database version, database driver address, and the full path of the Driver class.
[0086] Add the core configuration class DbConfig, which is the application database connection pool management class.
[0087] Use the @Bean annotation to create an application connection pool based on the MySQL database connection information configured in the application.properties or application.yml file. This is the only place where modification is required. For example:
[0088] / / Set the data source, such as "master" data source
[0089] DruidDataSource masterDataSource = new DruidDataSource();
[0090] masterDataSource.setUrl(properties.getUrl());
[0091] masterDataSource.setUsername(properties.getUsername());
[0092] masterDataSource.setPassword(properties.getPassword());
[0093] masterDataSource.setDriverClassName(properties.getDriverClassName());
[0094] Add the connection pool to dataSourceConfig as the primary data source, for example:
[0095] dataSourceConfig.getTargetDataSources().put("master",masterDataSource);
[0096] Set the default data source, for example:
[0097] dataSourceConfig.getRoutingDataSource().setDefaultTargetDataSource(masterDataSource);
[0098] Create Mapper interface and XML mapping file: Create Mapper interface and write corresponding XML mapping file according to business requirements to define SQL statements and mapping rules.
[0099] Use the @MapperScan annotation: Use the @MapperScan annotation on the Spring Boot startup class to specify the package path where the Mapper interface is located so that Spring Boot can scan and register these Mapper interfaces.
[0100] Use @ComponentScan(basePackages = {"package1", "package2"}) to enable the application to scan the SDK package directory.
[0101] In step 102, based on the JAVA SDK package, the system database connection pool in the application system is initialized.
[0102] In this embodiment of the present application, the database connection pool in the application system is initialized based on the JAVA SDK package created in step 101. For a detailed overview of the interaction between the JAVA SDK package and the application system, refer to Figure 3 , involving the association between different interface services A and B and SDK.
[0103] System initialization is loading at startup. The first time the system is loaded is loading the existing database connection information (known quantity) in the system, such as system initialization and system restart.
[0104] In a possible implementation, initializing the system database connection pool in the application system based on the JAVA SDK package may include:
[0105] Based on the JAVA SDK package, execute the @PostConstruct annotation to initialize the system database connection pool in the application system.
[0106] The @PostConstruct annotation is an annotation from the javax.annotation package that is used to mark a method to indicate that the method should be executed after dependency injection is completed. This annotation is part of the JSR-250 specification and is widely used in Java EE-based applications, especially when using the Spring framework.
[0107] After the program is started, the embodiment of the present application automatically executes the @PostConstruct annotation method and starts loading the existing database connection pool that needs to be managed in the system.
[0108] The effect of using the @PostConstruct annotation in the embodiment of this application is:
[0109] (1) Initialization operations: The @PostConstruct annotated method is usually used to perform initialization operations after dependency injection is completed. These operations may include resource initialization, state setting, etc.
[0110] (2) Delayed dependency injection: It allows developers to execute some code immediately after dependency injection is completed, rather than in the constructor. This can avoid complex initialization logic in the constructor and keep the constructor concise.
[0111] (3) Enhanced readability and maintainability: By using @PostConstruct, the initialization code can be separated from the business logic, improving the readability and maintainability of the code.
[0112] (4) Ensure that dependencies are injected: In the @PostConstruct annotated method, all dependencies have been injected, so they can be used safely.
[0113] (5) Applicable to various scenarios: @PostConstruct can be used for bean initialization in the Spring framework and for lifecycle management of EJB (Enterprise JavaBeans).
[0114] In a possible implementation, initializing the system database connection pool in the application system based on the JAVA SDK package may include:
[0115] Create a target routing data source and set the default database identification code;
[0116] Get a list of basic database information from the application system;
[0117] Determine whether the database basic information list is empty;
[0118] If the database basic information list is not empty, traverse the database basic information list and configure the corresponding database connection pool information based on each database basic information in the database basic information list;
[0119] Use the target data source mapping to temporarily store the database connection pool information corresponding to each database basic information configuration in the database basic information list in the current database connection pool;
[0120] At the end of the traversal, the routing data source is configured using the database connection pool information temporarily stored in the current database connection pool.
[0121] Optionally, refer to Figure 4 , the specific implementation process is as follows:
[0122] In step 1.1, create a routing data source (i.e., the target routing data source) and set the default database identifier. Then, use the getDataSourceInfos interface of the core DataSourceService class to obtain a list of basic database information from the application system.
[0123] Step 1.2: Determine whether the database basic information list is empty. If it is empty, end this initialization; if not, go to step 1.3.
[0124] Step 1.3, traverse the database basic information list, and configure the corresponding database connection pool information based on each database basic information in the database basic information list.
[0125] Step 1.4, use the target data source mapping<k,v> The database connection pool corresponding to each database basic information configuration in the database basic information list is temporarily stored in the current database connection pool, that is, the corresponding database connection pool information is stored in the target data source mapping targetDataSources with a unique identification code k.
[0126] Step 1.5: After waiting for the traversal to complete, use the database connection pool information temporarily stored in the current database connection pool to configure the routing data source, that is, configure the routing data source and configure targetDataSources into routingDataSource.
[0127] When obtaining a basic database information list from an application system, the database at this time is any database.
[0128] In addition, the target data source mapping<k,v> Where k is a unique identifier, i.e. (database type: database version: server address: database port: instance name: database name), and v is the database connection pool information.
[0129] In a possible implementation, after traversing the basic information list of the database, the method may further include:
[0130] Define a corresponding database connection pool unique identification code based on each database basic information in the database basic information list, and use each database connection pool unique identification code as the first database connection pool unique identification code;
[0131] For each first database connection pool unique identification code, perform the following steps:
[0132] Determine whether the first database connection pool unique identification code already exists in the target data source mapping;
[0133] If the first database connection pool unique identification code does not exist in the target data source mapping, determining whether the first target database driver package has been loaded, the first target database driver package being the database driver package corresponding to the database basic information corresponding to the first database connection pool unique identification code in the JAVA SDK package;
[0134] If the first target database driver package is not loaded, then load the first target database driver package and determine whether an abnormality occurs in the driver loading;
[0135] If no exception occurs when loading the driver, execute the step of configuring the corresponding database connection pool information based on the basic information of each database in the database basic information list.
[0136] Optionally, step 1.3 is as follows:
[0137] Step 1.3.1: define a corresponding database connection pool unique identification code k based on each database basic information in the database basic information list, and use each database connection pool unique identification code k as a first database connection pool unique identification code k.
[0138] For each first database connection pool unique identification code k, perform the following steps:
[0139] Step 1.3.2: Determine whether the first database connection pool unique identifier k already exists in the target data source mapping<k,v> If it already exists, skip this step and go to the step of traversing the database basic information list and continue the next loop; if it does not exist, go to step 1.3.3.
[0140] Step 1.3.3, determine whether the first target database driver package has been loaded, where the first target database driver package is the database driver package corresponding to the database basic information corresponding to the unique identification code k of the first database connection pool in the JAVA SDK package. If not loaded, go to step 1.3.4; if loaded, go to the step of configuring the corresponding database connection pool information based on each database basic information in the database basic information list.
[0141] The database driver package can be provided in two ways: the JAVA SDK package contains a certain amount of database driver packages, or the user can place the database driver package in a specified directory for the JAVA SDK package to load. The loading order is to load the database driver package in the user-specified directory first, and then load the database driver package in the JAVA SDK package. When the same database driver package in the user-specified directory has been loaded, the JAVA SDK package will not be loaded again.
[0142] For example, the configuration of loading and routing data sources is expressed as the following program:
[0143] private static final RoutingDataSource routingDataSource = new RoutingDataSource(); / / Global routingDataSource
[0144] @PostConstruct
[0145] public RoutingDataSource dataSource() {
[0146] List <gbasesourceconfiginfo>dataSourceConfigInfoList = dataSourceService.getDataSourceInfos();
[0147] if (dataSourceConfigInfoList.isEmpty()) {
[0148] return routingDataSource;
[0149] }
[0150] for (GBaseSourceConfigInfo configInfo : dataSourceConfigInfoList) {
[0151] initDataSource(configInfo);
[0152] }
[0153] / / Configure the routing data source
[0154] routingDataSource.setTargetDataSources(targetDataSources);
[0155] return routingDataSource;
[0156] }
[0157] Step 1.3.4: Create a data driver Driver based on the database version, database driver address, and full path of the Driver class in DataSourceConfigInfo, load the first target database driver package, and determine whether there is an exception when loading the driver. If an exception occurs, go to the step of traversing the database basic information list; if no exception occurs, go to the step of configuring the corresponding database connection pool information based on the basic information of each database in the database basic information list.
[0158] The purpose of creating a data driver is to enable the application to communicate with a specific database management system (DBMS).
[0159] Data drivers are part of the JDBC API and act as a bridge between Java applications and databases.
[0160] By loading and registering the database driver, the application can use the interfaces provided by the JDBC API (such as Connection, Statement, ResultSet, etc.) to execute SQL statements, manage database connections, etc.
[0161] The embodiment of the present application can automatically obtain a list of database information in the current system by initializing the database connection pool, and then traverse the list to create a default connection pool for each database.
[0162] In step 103, after the system database connection pool in the application system is initialized, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package.
[0163] The dynamic expansion of the new database connection pool is to add one or more new database systems.
[0164] In the embodiment of the present application, after the database connection pool is initialized, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package created in step 101.
[0165] In one possible implementation, after initializing the system database connection pool in the application system, dynamically expanding a new database connection pool in the application system based on the JAVASDK package may include:
[0166] Obtain new database basic information, and use the new database basic information to define a corresponding database connection pool unique identification code, and use the database connection pool unique identification code as the second database connection pool unique identification code;
[0167] Determine whether the second database connection pool unique identification code already exists in the target data source mapping;
[0168] If the second database connection pool unique identification code does not exist in the target data source mapping, then determine whether the second target database driver package has been loaded, the second target database driver package being the database driver package corresponding to the new database basic information in the JAVA SDK package;
[0169] If the second target database driver package is not loaded, then load the second target database driver package and determine whether an abnormality occurs in the driver loading process;
[0170] If no exception occurs when loading the driver, the corresponding database connection pool information is configured using the new database basic information;
[0171] Use the target data source mapping to temporarily store the database connection pool information corresponding to the new database basic information configuration in the current database connection pool;
[0172] Get a new routing data source, and reconfigure the new routing data source using the database connection pool information temporarily stored in the current database connection pool, and refresh the configured routing data source to make the new routing data source effective.
[0173] Optionally, refer to Figure 5 The specific steps to dynamically expand the new database connection pool are as follows:
[0174] In step 2.1, DataSourceConfig provides a new database connection pool interface and obtains new database basic information. The interface parameter is DataSourceConfigInfo, which contains the server IP address, database port, database account, database password, instance name, database name, database type, database version, database driver address, and the full path of the Driver class. The new database basic information is used to define the corresponding database connection pool unique identifier and use this database connection pool unique identifier as the second database connection pool unique identifier. The unique identifier is (database type: database version: server address: database port: instance name: database name).
[0175] Step 2.2: In targetDataSources, determine whether the unique identification code of the second database connection pool already exists in the target data source mapping. If so, end; if not, go to step 2.3.
[0176] Step 2.3, determine whether the second target database driver package has been loaded, wherein the second target database driver package is the database driver package corresponding to the new database basic information in the JAVA SDK package. If it has been loaded, go to step 2.5; if not, go to step 2.4.
[0177] Step 2.4: Create a data driver Driver to load the second target database driver package based on the database version, database driver address, and full path of the Driver class in DataSourceConfigInfo, and determine whether there is an exception when loading the driver. If there is an exception, end; if not, go to step 2.5.
[0178] Step 2.5, use the new database basic information to configure the corresponding database connection pool information;
[0179] In step 2.6, the target data source mapping is used to temporarily store the database connection pool information corresponding to the new database basic information configuration in the current database connection pool. That is, a database connection pool is created based on the database basic information, and the database connection pool is stored in targetDataSources with a unique identification code k.
[0180] Step 2.7, obtain a new routing data source, and use the database connection pool information temporarily stored in the current database connection pool to reconfigure the new routing data source, that is, configure the routing data source and configure targetDataSources into routingDataSource;
[0181] Step 2.8: Refresh the configured routing data source to make the new routing data source take effect.
[0182] The embodiment of the present application dynamically expands the database connection pool. Users can flexibly add new database connection pools according to their needs through relevant database information (such as database type, address, user name, password, etc.), so that the system can be easily expanded to new data sources without complex configuration or code modification.
[0183] Based on step 102 and step 103, the embodiment of the present application has powerful dynamic expansion capabilities and loosely coupled design features, which can greatly improve the flexibility and scalability of the system and reduce development and maintenance costs.
[0184] In a possible implementation, the embodiment of the present application can also switch the data source. When it is necessary to switch the database, the data source KEY that the current thread should use is set through dataSourceKeyHolder.set(key).
[0185] The specific process is as follows:
[0186] Step 3.1: Set a unique identifier for dataSourceKeyHolder (database type: database version: server address: database port: instance name: database name). For example, DataSourceConfig.setDataSourceHolder("GBase8s:360:172.1.1.1:9088:gbase01:sysmaster").
[0187] Step 3.2, execute mybatis mapper, for example, logMapper.getSysOnLineLog().
[0188] In step 3.3, after the execution is completed, the data source needs to be cleared, for example, using DataSourceConfig.clearDataSourceHolder().
[0189] In step 3.4, after clearing the current data source, the system will continue to use the default system data source and no further operations will be performed.
[0190] For example, the application switching database connection pool can be described in the following procedure:
[0191] DataSourceConfig.setDataSourceHolder("GBase8s:360:172.1.1.1:9088:gbase01:sysmaster ");
[0192] logMapper.getSysOnLineLog();
[0193] DataSourceConfig.clearDataSourceHolder();
[0194] Page <userbo>page=newPage<>(queryParams.getPageNum(),queryParams.getPageSize());
[0195] / / Query data
[0196] Page <userbo>userPage = this.baseMapper.getUserPage(page,queryParams);
[0197] The embodiment of the present application can switch the database connection pool at will and replace the existing database connection pool at will.
[0198] In a possible implementation, the embodiment of the present application can also clear the data source. After switching the database, the data source KEY of the current thread is cleared through dataSourceKeyHolder.remove() to avoid potential resource leakage or incorrect use.
[0199] Reference Figure 6 , the process steps for clearing the database connection pool are as follows:
[0200] Step 4.1, obtain the database connection pool of the current data source from the routing data source;
[0201] Step 4.2, determine whether the current routing data source is the default data source, if so, end; if not, go to step 4.3.
[0202] Step 4.3, reconfigure the routing data source;
[0203] Step 4.4: Refresh the routing data source to make it take effect.
[0204] The embodiment of the present application removes existing database connection pools that are no longer in use, allowing users to easily remove them, which helps to optimize system resources and reduce unnecessary overhead.
[0205] The present application provides a multi-version dynamically extensible data source method, which creates a JAVA SDK package, which includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, and basic properties and target data source mappings of the routing data source classes. The target data source mapping is used to store loaded database connection pools. Based on the JAVA SDK package, the system database connection pool in the application system is initialized. After the system database connection pool in the application system is initialized, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package. This application provides developers with a powerful toolset by creating a JAVA SDK package that includes key components such as core dependencies, core entity classes, and core interface classes. This allows developers to easily meet the needs of different data sources and database versions, and supports dynamic expansion of new database connection pools, which means that the system can add new data sources according to actual needs without interrupting service, thereby improving system flexibility and adaptability. In addition, the introduction of the database connection pool management core class enables the system to efficiently manage and reuse database connections, reducing the overhead of connection establishment and destruction, thereby improving system performance. The thread isolation mechanism ensures that operations of different data sources do not interfere with each other, further enhancing the system's concurrent processing capabilities and stability. In addition, the provision of routing data sources and their basic properties and target data source mappings makes data source configuration and management simpler and more intuitive. Developers only need to configure routing rules to achieve data source switching and load balancing without complex code modifications. The storage mechanism of target data source mappings enables the system to quickly identify and reuse already loaded database connection pools, thereby simplifying the data source management process.
[0206] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0207] The following are device embodiments of the present application. For details not fully described therein, please refer to the corresponding method embodiments described above.
[0208] Figure 7 A schematic diagram of the structure of a multi-version dynamically scalable data source device provided in an embodiment of the present application is shown. For ease of explanation, only the parts related to the embodiment of the present application are shown, which are detailed as follows:
[0209] like Figure 7 As shown, the multi-version dynamically scalable data source device 7 includes:
[0210] A creation module 71 is used to create a JAVA SDK package. The JAVA SDK package includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, basic properties of routing data source classes, and target data source mappings. The target data source mappings are used to store loaded database connection pools.
[0211] Initialization module 72, used to initialize the system database connection pool in the application system based on the JAVA SDK package;
[0212] The expansion module 73 is used to dynamically expand a new database connection pool in the application system based on the JAVA SDK package after initializing the system database connection pool in the application system.
[0213] The present application provides a multi-version dynamically extensible data source device, which creates a JAVA SDK package, which includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, and basic properties and target data source mappings of routing data source classes. The target data source mapping is used to store loaded database connection pools. Based on the JAVA SDK package, the system database connection pool in the application system is initialized. After the system database connection pool in the application system is initialized, a new database connection pool is dynamically expanded in the application system based on the JAVA SDK package. This application provides developers with a powerful toolset by creating a JAVA SDK package that includes key components such as core dependencies, core entity classes, and core interface classes. This allows developers to easily meet the needs of different data sources and database versions, and supports dynamic expansion of new database connection pools, which means that the system can add new data sources according to actual needs without interrupting service, thereby improving system flexibility and adaptability. In addition, the introduction of the database connection pool management core class enables the system to efficiently manage and reuse database connections, reducing the overhead of connection establishment and destruction, thereby improving system performance. The thread isolation mechanism ensures that operations of different data sources do not interfere with each other, further enhancing the system's concurrent processing capabilities and stability. In addition, the provision of routing data sources and their basic properties and target data source mappings makes data source configuration and management simpler and more intuitive. Developers only need to configure routing rules to achieve data source switching and load balancing without complex code modifications. The storage mechanism of target data source mappings enables the system to quickly identify and reuse already loaded database connection pools, thereby simplifying the data source management process.
[0214] In one possible implementation, the creation module can be used to:
[0215] Build a Maven project and add core dependencies to the Maven project;
[0216] After adding core dependencies to the Maven project, define the core entity class, core interface class, and database connection pool management core class respectively;
[0217] Configure thread isolation;
[0218] Customize the routing data source class, define the basic properties of the routing data source class, and define the target data source mapping. The target data source mapping is used to store the loaded database connection pool.
[0219] In one possible implementation, the creation module can also be used to:
[0220] Use the thread-local variable type attributes of different data sources to configure the isolation of the corresponding data sources between different threads.
[0221] In one possible implementation, the initialization module can be used to:
[0222] Create a target routing data source and set the default database identification code;
[0223] Get a list of basic database information from the application system;
[0224] Determine whether the database basic information list is empty;
[0225] If the database basic information list is not empty, traverse the database basic information list and configure the corresponding database connection pool information based on each database basic information in the database basic information list;
[0226] Use the target data source mapping to temporarily store the database connection pool information corresponding to each database basic information configuration in the database basic information list in the current database connection pool;
[0227] At the end of the traversal, the routing data source is configured using the database connection pool information temporarily stored in the current database connection pool.
[0228] In a possible implementation, the initialization module can also be used to:
[0229] Define a corresponding database connection pool unique identification code based on each database basic information in the database basic information list, and use each database connection pool unique identification code as the first database connection pool unique identification code;
[0230] For each first database connection pool unique identification code, perform the following steps:
[0231] Determine whether the first database connection pool unique identification code already exists in the target data source mapping;
[0232] If the first database connection pool unique identification code does not exist in the target data source mapping, determining whether the first target database driver package has been loaded, the first target database driver package being the database driver package corresponding to the database basic information corresponding to the first database connection pool unique identification code in the JAVA SDK package;
[0233] If the first target database driver package is not loaded, then load the first target database driver package and determine whether an abnormality occurs in the driver loading;
[0234] If no exception occurs when loading the driver, execute the step of configuring the corresponding database connection pool information based on the basic information of each database in the database basic information list.
[0235] In one possible implementation, the extension module can be used to:
[0236] Obtain new database basic information, and use the new database basic information to define a corresponding database connection pool unique identification code, and use the database connection pool unique identification code as the second database connection pool unique identification code;
[0237] Determine whether the second database connection pool unique identification code already exists in the target data source mapping;
[0238] If the second database connection pool unique identification code does not exist in the target data source mapping, then determine whether the second target database driver package has been loaded, the second target database driver package being the database driver package corresponding to the new database basic information in the JAVA SDK package;
[0239] If the second target database driver package is not loaded, then load the second target database driver package and determine whether an abnormality occurs in the driver loading process;
[0240] If no exception occurs when loading the driver, the corresponding database connection pool information is configured using the new database basic information;
[0241] Use the target data source mapping to temporarily store the database connection pool information corresponding to the new database basic information configuration in the current database connection pool;
[0242] Get a new routing data source, and reconfigure the new routing data source using the database connection pool information temporarily stored in the current database connection pool, and refresh the configured routing data source to make the new routing data source effective.
[0243] In a possible implementation, the initialization module can also be used to:
[0244] Based on the JAVA SDK package, execute the @PostConstruct annotation to initialize the system database connection pool in the application system.
[0245] Figure 8 Schematic diagram of the terminal provided in the embodiment of the present application. Figure 8 As shown, the terminal 8 of this embodiment includes: a processor 80, a memory 81, and a computer program 82 stored in the memory 81 and executable on the processor 80. When the processor 80 executes the computer program 82, the steps in the above-mentioned embodiments of the multi-version dynamically scalable data source method are implemented, such as Figure 1 Alternatively, when the processor 80 executes the computer program 82, the functions of the modules / units in the above-mentioned device embodiments are realized, for example, Figure 7 The functions of each module are shown.
[0246] For example, the computer program 82 may be divided into one or more modules / units, which are stored in the memory 81 and executed by the processor 80 to complete the present application. The one or more modules / units may be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program 82 in the terminal 8. For example, the computer program 82 may be divided into Figure 7 The modules shown.
[0247] The terminal 8 can be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal 8 can include, but is not limited to, a processor 80 and a memory 81. Those skilled in the art will understand that Figure 8 It is only an example of terminal 8 and does not constitute a limitation on terminal 8. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the terminal may also include input and output devices, network access devices, buses, etc.
[0248] The processor 80 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0249] The memory 81 can be an internal storage unit of the terminal 8, such as a hard drive or memory of the terminal 8. Alternatively, the memory 81 can be an external storage device of the terminal 8, such as a plug-in hard drive, a Smart Media Card (SMC), a Secure Digital (SD) card, a flash memory card, etc. Furthermore, the memory 81 can include both an internal storage unit of the terminal 8 and an external storage device. The memory 81 is used to store the computer program and other programs and data required by the terminal. The memory 81 can also be used to temporarily store data that has been output or is about to be output.
[0250] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0251] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0252] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0253] In the embodiments provided in this application, it should be understood that the disclosed devices / terminals and methods can be implemented in other ways. For example, the device / terminal embodiments described above are merely illustrative. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, 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 through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0254] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0255] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0256] If the integrated module / unit is implemented as 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 present application can implement all or part of the processes in the above-mentioned embodiment method by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, it can implement the steps of the above-mentioned embodiments of the multi-version dynamically expandable data source method. The computer program includes computer program code, which can be in source code form, object code form, executable file, or some intermediate form. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard drive, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium.
[0257] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present application, and should all be included in the scope of protection of the present application.< / userbo> < / userbo> < / gbasesourceconfiginfo> < / string> < / string>
Claims
1. A multi-version dynamically scalable data source method, characterized in that: include: Create a JAVA SDK package, which includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, basic properties of the routing data source classes, and target data source mappings. The target data source mappings are used to store loaded database connection pools. Initialize the system database connection pool in the application system based on the JAVA SDK package; After initializing the system database connection pool in the application system, dynamically expanding a new database connection pool in the application system based on the JAVA SDK package; After initializing the system database connection pool in the application system, dynamically expanding a new database connection pool in the application system based on the JAVA SDK package includes: Obtain new database basic information, and use the new database basic information to define a corresponding database connection pool unique identification code, and use the database connection pool unique identification code as the second database connection pool unique identification code; Determining whether the second database connection pool unique identification code already exists in the target data source mapping; If the second database connection pool unique identification code does not exist in the target data source mapping, determining whether a second target database driver package has been loaded, where the second target database driver package is a database driver package corresponding to the new database basic information in the JAVA SDK package; If the second target database driver package is not loaded, then loading the second target database driver package and determining whether an abnormality occurs in the driver loading process; If no abnormality occurs in the loading driver, the corresponding database connection pool information is configured using the new database basic information; Temporarily storing the database connection pool information corresponding to the new database basic information configuration in the current database connection pool using the target data source mapping; A new routing data source is obtained, and the database connection pool information temporarily stored in the current database connection pool is used to reconfigure the new routing data source, and the configured routing data source is refreshed to make the new routing data source effective.
2. The multi-version dynamically scalable data source method according to claim 1, characterized in that: The creation of the JAVASDK package includes: Build a Maven project and add core dependencies to the Maven project; After adding the core dependencies to the Maven project, define the core entity class, core interface class and database connection pool management core class respectively; Configure thread isolation; Customize the routing data source class, define the basic properties of the routing data source class, and define the target data source mapping. The target data source mapping is used to store the loaded database connection pool.
3. The multi-version dynamically scalable data source method according to claim 2, characterized in that: The configuration thread isolation includes: Use the thread-local variable type attributes of different data sources to configure the isolation of the corresponding data sources between different threads.
4. The multi-version dynamically scalable data source method according to claim 2, characterized in that: Initializing the system database connection pool in the application system based on the JAVA SDK package includes: Create a target routing data source and set the default database identification code; Obtaining a list of basic database information from the application system; Determine whether the basic information list of the database is empty; If the database basic information list is not empty, traverse the database basic information list and configure corresponding database connection pool information based on each database basic information in the database basic information list; Temporarily storing the database connection pool information corresponding to each database basic information configuration in the database basic information list in the current database connection pool by using the target data source mapping; After the traversal is completed, the routing data source is configured using the database connection pool information temporarily stored in the current database connection pool.
5. The multi-version dynamically scalable data source method according to claim 4, characterized in that: After traversing the basic information list of the database, the method further includes: Based on each database basic information in the database basic information list, a corresponding database connection pool unique identification code is defined, and each database connection pool unique identification code is used as a first database connection pool unique identification code; For each first database connection pool unique identification code, perform the following steps: Determine whether the first database connection pool unique identification code already exists in the target data source mapping; If the first database connection pool unique identification code does not exist in the target data source mapping, determining whether a first target database driver package has been loaded, where the first target database driver package is a database driver package corresponding to the database basic information corresponding to the first database connection pool unique identification code in the JAVA SDK package; If the first target database driver package is not loaded, then loading the first target database driver package and determining whether an abnormality occurs in the driver loading; If no exception occurs in the loading driver, the step of configuring corresponding database connection pool information based on each database basic information in the database basic information list is executed.
6. The multi-version dynamically scalable data source method according to claim 1, characterized in that: Initializing the system database connection pool in the application system based on the JAVA SDK package includes: Based on the JAVA SDK package, the @PostConstruct annotation is executed to initialize the system database connection pool in the application system.
7. A multi-version dynamically scalable data source device, characterized in that: include: A creation module is used to create a JAVA SDK package. The JAVA SDK package includes core dependencies, core entity classes, core interface classes, database connection pool management core classes, thread isolation, routing data source classes, basic properties of the routing data source classes, and target data source mappings. The target data source mappings are used to store loaded database connection pools. An initialization module, used to initialize the system database connection pool in the application system based on the JAVA SDK package; An extension module, configured to dynamically extend a new database connection pool in the application system based on the JAVA SDK package after initializing the system database connection pool in the application system; Wherein, the expansion module is used for: Obtain new database basic information, and use the new database basic information to define a corresponding database connection pool unique identification code, and use the database connection pool unique identification code as the second database connection pool unique identification code; Determining whether the second database connection pool unique identification code already exists in the target data source mapping; If the second database connection pool unique identification code does not exist in the target data source mapping, determining whether a second target database driver package has been loaded, where the second target database driver package is a database driver package corresponding to the new database basic information in the JAVA SDK package; If the second target database driver package is not loaded, then loading the second target database driver package and determining whether an abnormality occurs in the driver loading process; If no abnormality occurs in the loading driver, the corresponding database connection pool information is configured using the new database basic information; Temporarily storing the database connection pool information corresponding to the new database basic information configuration in the current database connection pool using the target data source mapping; A new routing data source is obtained, and the new routing data source is reconfigured using the database connection pool information temporarily stored in the current database connection pool, and the configured routing data source is refreshed to make the new routing data source effective.
8. A terminal comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the steps of the multi-version dynamically extensible data source method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the multi-version dynamically extensible data source method as described in any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Method for achieving DB monitoring through dynamic database connection pool
CN110347554A
Database connection pool configuration method, device and system
CN113779119A
