Data access method and device suitable for multiple databases

By monitoring and dynamically managing dialect plugins and instances during application runtime, the problem of insufficient linkage between dialect plugin changes and SQL generation is solved, achieving flexibility and efficiency in database access.

CN123178442APending Publication Date: 2026-11-27BEIJING LINGYAN ZHIYUAN TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202611430796.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-15
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

In existing multi-database access frameworks, the addition, modification, or deletion of dialect plugins lacks linkage with the dialect instances called during the actual SQL generation process. This makes it difficult for newly added or modified dialects or currently missing target dialects to participate in subsequent database access processing in a timely manner, usually requiring application restart or additional intervention.

Method used

During application runtime, monitor changes in the dialect plugin set, reload and instantiate the dialect implementation class, rebuild the dialect instance cache, and perform supplementary loading based on the dialect configuration when the target dialect instance is not obtained, ensuring that the update path triggered by plugin changes and the supplementary loading path of data access requests work together to generate SQL.

Benefits of technology

This allows for changes to plugins during application runtime without requiring a restart of the business application. Newly added or modified database dialects can promptly participate in subsequent database accesses, ensuring the continuity and efficiency of data access.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123178442A_ABST
    Figure CN123178442A_ABST
Patent Text Reader

Abstract

The application discloses a data access method and device suitable for multiple databases, and the method comprises the following steps: obtaining a target database identifier corresponding to a data access request; monitoring the change of a dialect plug-in set during application running; when detecting that the dialect plug-in is added, modified or deleted, reloading and instantiating the dialect implementation class in the changed dialect plug-in set, and rebuilding the dialect instance cache used for obtaining the corresponding dialect instance according to the database identifier. The application monitors the change of the dialect plug-in set during application running, and when detecting that the dialect plug-in is added, modified or deleted, the dialect implementation class in the changed dialect plug-in set is reloaded and instantiated, and the dialect instance cache used for obtaining the dialect instance according to the database identifier is rebuilt.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer data processing technology, specifically relating to a data access method and apparatus adapted to multiple databases. Background Technology

[0002] As the types of data sources accessed by business applications increase, the same business application may need to access different types of databases such as MySQL and Oracle. Since different databases have differences in SQL expression in terms of record quantity limits, pagination queries, database functions, date processing, database keywords, and data definition languages, multi-database access frameworks usually need to set up corresponding database dialects for different databases. The corresponding dialects of different databases are used to process the database differences involved in the SQL generation process, so that business applications can perform corresponding data access operations according to the SQL expression methods supported by the target database.

[0003] Existing technologies already contain solutions for unified access and database dialect adaptation for multiple databases. For example, CN116089542A discloses a JDBC-based database adaptation method and apparatus, which sets corresponding syntax conversion processes for different database types to convert the initial SQL into SQL that the target database can execute. CN117667260A discloses a Java-based dynamic ORM component, which completes entity data operations and SQL construction through a unified data operation method and corresponding dialects for different databases. These solutions can, to some extent, solve the problems of data access and SQL syntax adaptation between different databases.

[0004] However, in existing multi-database access frameworks, there is a lack of effective linkage between the addition, modification, or deletion of dialect plugins during application runtime and the dialect instances called in the actual SQL generation process. When dialect plugins change or the dialect instance corresponding to the target database is missing during the actual data access process, the newly added or modified dialect or the currently missing target dialect cannot participate in the subsequent database access processing in a timely manner. As a result, the corresponding database dialect usually needs to be restarted, redeployed, or otherwise intervened before it can participate in the actual SQL generation and execution process. Summary of the Invention

[0005] The purpose of this invention is to provide a data access method and apparatus that is compatible with multiple databases, in order to solve the problem mentioned in the background art that the dialect plugin status during application operation lacks linkage with the dialect instance called in the actual SQL generation process, resulting in the difficulty for newly added or modified dialects or currently missing target dialects to participate in subsequent database access processing in a timely manner.

[0006] To achieve the above objectives, the present invention provides the following technical solution: A data access method adaptable to multiple databases includes: Obtain the target database identifier corresponding to the data access request; During application operation, monitor changes in the dialect plugin set. When a dialect plugin is added, modified, or deleted, reload and instantiate the dialect implementation class based on the changed dialect plugin set, and rebuild the dialect instance cache used to obtain the corresponding dialect instance based on the database identifier. For data access requests to the target database, the corresponding dialect instance is retrieved from the dialect instance cache based on the target database identifier. If it is not retrieved, the corresponding dialect implementation class is determined based on the dialect configuration that records the correspondence between the database identifier and the dialect implementation class. After loading and instantiating, it is written to the dialect instance cache. The function calls the obtained dialect instance to process the preset database differences in the SQL generation process, and generates and executes SQL applicable to the target database.

[0007] In one embodiment, after detecting a change in the dialect plugin set, the original dialect class loader is closed and the original dialect instance cache is cleared. A new dialect class loader is created, and the dialect implementation classes in the changed dialect plugin set are reloaded and instantiated. The dialect instance cache is then rebuilt with the obtained dialect instances.

[0008] In one embodiment, after loading the classes in the dialect plugin, dialect interface verification is performed on the loaded classes, the classes that implement the preset dialect interface are determined as dialect implementation classes, and the dialect implementation classes are instantiated as dialect instances.

[0009] In one embodiment, the dialect plugin is set in the dialect plugin extension directory, and file changes in the dialect plugin extension directory are monitored through a file monitoring service. When a new, modified, or deleted event of a dialect plugin is detected, the dialect manager is notified to rebuild the dialect instance cache.

[0010] In one embodiment, the new dialect class loader scans dialect plugin files in the dialect plugin extension directory, configures the dialect plugin files as loadable resources of the new dialect class loader, and loads the dialect implementation classes in the dialect plugin files.

[0011] In one embodiment, obtaining the target database identifier includes: obtaining a database connection object that has established a connection with the target database, obtaining database metadata based on the database connection object, and determining the target database identifier based on the database product name in the database metadata.

[0012] In one embodiment, the dialect configuration includes a database identifier and a dialect implementation class identifier corresponding to the database identifier. The dialect manager determines the corresponding dialect implementation class from the dialect configuration based on the target database identifier.

[0013] In one embodiment, if a corresponding dialect instance is not obtained from the dialect instance cache based on the target database identifier, and if a dialect implementation class identifier corresponding to the target database identifier exists in the dialect configuration, then the corresponding dialect implementation class is loaded and instantiated, the obtained dialect instance is written to the dialect instance cache and provided to the current data access request; if no corresponding dialect implementation class identifier exists, then an exception is thrown.

[0014] In a preferred embodiment, the preset database difference items include at least one of record quantity limitation, pagination query, database function, date conversion, database keyword processing, and data definition language operation. Different dialect instances generate SQL expressions suitable for the corresponding database based on the preset database difference items.

[0015] A data access device adaptable to multiple databases includes a database identifier acquisition unit, a dialect plugin monitoring unit, a dialect instance management unit, and an SQL processing unit. The database identifier acquisition unit is used to acquire the target database identifier. The dialect plugin monitoring unit is used to monitor changes in the dialect plugin set during application operation. The dialect instance management unit is used to reload and instantiate the dialect implementation classes based on the changed dialect plugin set when dialect plugins are added, modified, or deleted, rebuild the dialect instance cache used to acquire dialect instances based on the database identifier, and supplement the loading of corresponding dialect instances according to the dialect configuration when the corresponding dialect instance for the target database is not obtained. The SQL processing unit is used to call the acquired dialect instance to process preset database difference items, generate and execute SQL applicable to the target database.

[0016] Compared with the prior art, the beneficial effects of the present invention are: This invention monitors changes to the dialect plugin set during application operation. When a dialect plugin is added, modified, or deleted, the dialect implementation class is reloaded and instantiated based on the changed dialect plugin set. The dialect instance cache used to obtain dialect instances based on database identifiers is also rebuilt. Simultaneously, when the actual data access request fails to obtain the dialect instance corresponding to the target database, supplementary loading is performed according to the dialect configuration. This allows the active update path triggered by plugin changes and the supplementary loading path triggered by data access requests to work together on the dialect instance management system used for subsequent SQL generation. This enables newly added or modified database dialects during operation, as well as currently missing but already configured database dialects, to participate in subsequent actual database accesses without requiring the entire business application to be restarted due to changes in database dialects. Attached Figure Description

[0017] Figure 1 This is a schematic diagram illustrating the overall process of the data access method adapted to various databases in this invention.

[0018] Figure 2 This is a schematic diagram illustrating the process of dialect plugin changes and dialect instance cache reconstruction in this invention.

[0019] Figure 3 This is a schematic diagram illustrating the process of obtaining and loading additional language instances for data access requests in this invention.

[0020] Figure 4 This is a schematic diagram illustrating the correspondence between database identifiers, dialect configurations, dialect implementation classes, and dialect instances in this invention.

[0021] Figure 5 This is a structural block diagram of a data access device adapted to various databases according to the present invention. Detailed Implementation

[0022] The technical solution of the present invention will be further described below with reference to specific embodiments. The present invention is applicable to business application systems that need to access different types of databases. Different databases may have differences in SQL expression in terms of record quantity limits, pagination queries, database functions, date processing, database keywords, and data definition languages. The present invention handles the preset database differences in the SQL generation process by implementing dialects corresponding to different databases, and dynamically manages dialect plugins and corresponding dialect instances during the operation of business applications, so that changes in the state of dialect plugins can be passed to the dialect instances called by subsequent actual database accesses. Example 1

[0023] In a specific application scenario, this embodiment is applied to a continuously running data processing business system. The business system needs to access business data stored in different types of databases. For example, the business system is currently querying and processing data through a MySQL database. While the business system is running, it also needs to access a data source using an Oracle database. Since MySQL and Oracle databases differ in SQL expressions such as record quantity limits, pagination queries, database functions, and date processing, after accessing the Oracle database data source, it is necessary to enable the dialect implementation adapted to the Oracle database to enter the current data access process for subsequent data access requests to the Oracle database.

[0024] In the above application scenario, when the dialect plugin corresponding to the Oracle database is added to the set of dialect plugins that the business application can recognize, this embodiment monitors the changes of the dialect plugins and reconstructs the dialect instances that can be called based on the changed set of dialect plugins. When a data access request for the Oracle database is subsequently generated, the corresponding Oracle dialect instance is obtained according to the target database identifier. The dialect instance participates in the database difference processing during the SQL generation process and executes the generated SQL through the Oracle database connection. This enables the database dialect added during the operation to participate in the subsequent actual database access.

[0025] This embodiment provides a data access method adapted to multiple databases. In one specific implementation, the data access process of a business application can be completed collaboratively by a data operation interface, an SQL constructor, a dialect manager, a file monitoring service, and a dialect class loader. The data operation interface is used to receive data operation requests for the target database. The SQL constructor is used to construct SQL according to the data operation request. The dialect manager is used to obtain and manage dialect instances corresponding to different databases. The file monitoring service is used to monitor changes in dialect plugins during the operation of the business application. The dialect class loader is used to load dialect implementation classes in dialect plugins. Each dialect implementation class provides SQL difference processing capabilities corresponding to the corresponding database according to the preset Dialect interface.

[0026] In this embodiment, the database identifier is used to characterize the database type targeted by the current data access request and serves as the basis for determining the corresponding database dialect. In a specific implementation, after the business application establishes a database connection with the target database, it obtains DatabaseMetaData through the database connection object and obtains DatabaseProductName from DatabaseMetaData. The obtained database product name can be used directly as the database identifier or as the basis for determining a unified database identifier, such as determining the database identifier corresponding to MySQL database and Oracle database respectively.

[0027] The business application has a dialect configuration to record the correspondence between the database identifier and the corresponding dialect implementation class. In one specific implementation, the dialect configuration is stored in a configuration file, which can record the database product identifier through the meta field and the class identifier of the corresponding dialect implementation class through the clazz field. Therefore, the database identifier is used to determine the dialect corresponding to the target database, and the dialect implementation class identifier is used to further locate the specific dialect implementation class that needs to be loaded or instantiated.

[0028] When a business application starts, the dialect manager performs initialization, reads the current dialect configuration and the dialect plugins accessible during startup, determines the corresponding dialect implementation class based on the dialect configuration, loads and instantiates the dialect implementation class, and saves the formed dialect instance in the dialect instance cache for subsequent database access. Dialect plugins can be in the form of JAR files, and a dialect plugin can contain one or more dialect implementation classes. The existing dialect implementation classes form the initial dialect instance when the application starts.

[0029] In this embodiment, the dialect instance cache refers to a cache structure used to store instantiated dialect objects and enable the dialect manager to obtain the corresponding dialect instance based on the target database identifier. In one specific implementation, the dialect instance cache stores the cache relationship between the dialect implementation class identifier and the corresponding dialect instance. When the actual database access requires obtaining the corresponding dialect instance based on the target database identifier, the dialect implementation class identifier corresponding to the target database identifier is first determined according to the dialect configuration, and then the cached dialect instance is obtained using the dialect implementation class identifier.

[0030] In one specific implementation, the Dialect interface can also provide information to indicate the database type that the corresponding dialect is adapted to, so that the dialect manager can determine the database type corresponding to the loaded dialect instance. A continuous correspondence is formed between the database identifier, the dialect implementation class identifier and the instantiated dialect object in the dialect configuration. The database identifier is used by the business data access side to determine the corresponding dialect, and the implementation class identifier is used for class loading and cache management. The two are at different data processing levels.

[0031] After the business application enters the running state, it monitors the changes of dialect plugins that can be identified by the dialect loading mechanism and participate in the dialect implementation class loading. The dialect plugins constitute the current dialect plugin set. In a specific implementation, the dialect plugin set is determined by the plugin files that currently exist in the independent dialect plugin extension directory and can be identified by the dialect loading mechanism. When dialect plugins in the extension directory are added, modified or deleted, the corresponding changes to the current dialect plugin set are formed.

[0032] After the file monitoring service detects a change in a dialect plugin, it provides the corresponding plugin change information to the dialect manager, which then triggers the reconstruction of the dialect instance cache. In this embodiment, the reconstruction of the dialect instance cache refers to the process of reconstructing cached content that can be used for subsequent data access processes to obtain corresponding dialect instances based on the changed state of the dialect plugin after the change in the state of the dialect plugin. This ensures that the dialect instances that can be obtained by subsequent database accesses correspond to the dialect plugins that can be loaded at present.

[0033] In one specific implementation of dialect instance cache reconstruction, the dialect manager removes the original dialect instance from the current dialect instance management state, closes the original dialect class loader, and clears the original dialect instance cache. Then, a new dialect class loader is created. This new loader reloads the corresponding dialect implementation classes based on the changed current dialect plugin set, instantiates the loaded dialect implementation classes, and rebuilds the dialect instance cache. This reconstruction process targets the runtime loading state and dialect instance management state of the dialect plugins and does not require restarting the entire application.

[0034] When loading the current dialect plugin, the dialect class loader parses the classes contained in the plugin and determines whether the parsed classes implement the preset Dialect interface. For classes that implement the Dialect interface, they are identified as dialect implementation classes and instantiated. For classes that do not implement the Dialect interface, they are not added to the current dialect instance management process as database dialect instances. The Dialect interface is used to specify the unified calling method provided by different database dialects to the SQL generation process. The dialect implementation classes corresponding to each database implement the specific difference processing logic of the corresponding database.

[0035] When a new dialect plugin is added, the new plugin is added to the changed current dialect plugin set, and a corresponding dialect instance is formed during the reloading process. When an existing dialect plugin is modified, the corresponding dialect implementation class is reloaded with the modified plugin, and a new dialect instance is formed. When an existing dialect plugin is deleted, the deleted plugin is no longer part of the changed current dialect plugin set. If a certain database dialect is only provided by the deleted plugin, the dialect instance cache formed based on the deleted current dialect plugin set will no longer contain the corresponding dialect instance provided by that plugin.

[0036] When a business application generates a data access request for a target database, the database identifier is determined based on the target database corresponding to this data access request, and the corresponding dialect instance is obtained based on the database identifier. If a dialect instance that corresponds to the target database identifier already exists in the current dialect instance cache, the dialect manager returns the obtained dialect instance to the SQL builder for the current data access request to generate SQL.

[0037] When a corresponding dialect instance is not retrieved from the current dialect instance cache based on the target database identifier, the dialect manager performs supplementary loading on the request side. It rereads or queries the current dialect configuration and determines whether a corresponding dialect implementation class configuration exists based on the target database identifier. If a corresponding configuration exists, the corresponding dialect implementation class is determined based on the dialect configuration, loaded and instantiated, and the resulting dialect instance is saved in the dialect instance cache. The dialect instance is then returned to the data access request that triggered this supplementary loading. If the dialect implementation class corresponding to the target database does not exist in the current dialect configuration, the corresponding dialect instance cannot be retrieved and an exception is thrown.

[0038] The dialect instances that can be invoked can be formed during the business application startup phase through initialization, or they can be re-formed during operation due to changes in the dialect plugin set, or they can be supplemented during actual database access when the target dialect instance is not found. All dialect instances obtained in the above different ways are included in the current dialect instance management system. The subsequent SQL generation process can obtain the corresponding dialect instance that can be invoked based on the target database identifier.

[0039] After obtaining the dialect instance corresponding to the target database, the SQL builder constructs SQL based on the current data operation request. During the SQL generation process, it calls the corresponding Dialect method according to the preset database differences involved. Preset database differences may include one or more of the following: record quantity limits, pagination queries, database functions, date conversion, database keyword processing, and Data Definition Language operations. The Dialect implementations corresponding to different databases form SQL expressions suitable for their respective databases.

[0040] In this embodiment, database difference processing refers to calling the corresponding Dialect method to process pre-determined database difference items during SQL generation, rather than performing a general database syntax translation on any already formed SQL. For example, the same record quantity limit requirement can be used by Dialect implementations corresponding to different databases to generate SQL fragments that conform to the target database syntax rules, thereby encapsulating specific database syntax differences in different dialect implementations.

[0041] After the dialect instance completes the corresponding database difference processing, the SQL builder obtains the SQL applicable to the target database and executes the SQL through the target database connection to complete the corresponding data query, addition, modification, deletion or other data operations. The dialect plugin runtime management, target database identification, dialect instance acquisition and SQL generation and execution form a continuous data access processing process, so that the database dialect added or modified during the runtime can participate in subsequent actual database access. Example 2

[0042] This embodiment, based on Embodiment 1, further explains the specific process of dialect plugin change monitoring, dynamic loading of dialect implementation classes, and dialect instance cache reconstruction during the operation of the business application. The business application sets up a separate dialect plugin extension directory to store dialect plugins that can be recognized by the dialect loading mechanism. In one specific implementation, the dialect plugin is in the form of a jar file, and a dialect plugin can contain one or more dialect implementation classes that implement the preset Dialect interface.

[0043] During the business application startup phase, the dialect plugin extension directory can be configured as a class loading path accessible during application startup, enabling existing dialect plugins to participate in initialization when the application starts. In one specific implementation, the extension directory can be set as an accessible loading path for the application through startup parameters. The dialect manager reads the current dialect configuration, loads existing dialect implementation classes according to the correspondence between database identifiers and corresponding dialect implementation class identifiers, and forms an initial dialect instance cache.

[0044] After the business application enters the running state, an independent file change monitoring mechanism is used to monitor changes in plugin files in the dialect plugin extension directory. In one specific implementation, FileWatchService monitors file addition, modification or deletion events in the extension directory, and provides plugin change information to DialectManager after detecting the corresponding changes. FileWatchService is used to discover changes in the state of dialect plugin files, and DialectManager is responsible for performing subsequent dialect loading, unloading and hot update processing based on the changes.

[0045] The class loading process that allows existing dialect plugins to participate in initialization during the startup phase is a different processing phase from the dynamic loading process of plugins after the business application has entered the running state. The startup class loading path is used to solve the problem of identifying existing dialect plugins when the application starts, while the addition, modification or deletion that occurs during the runtime is handled through the file listening service and the runtime dialect class loading mechanism, so that the changed dialect can enter the subsequent database access process without restarting the entire business application.

[0046] After receiving plugin change information, DialectManager reconstructs the dialect instance cache based on the changed current dialect plugin set. In one specific implementation, DialectManager closes the original DialectClassLoader and clears the original dialect instance cache, causing the original dialect instance to exit the current dialect instance management state. Then, a new DialectClassLoader is created, and the new DialectClassLoader uses the changed current dialect plugin set as the reload object to perform dialect loading.

[0047] DialectClassLoader scans the dialect plugin files in the current dialect plugin collection and configures the corresponding dialect plugin files as loading resources that the current DialectClassLoader can access. Then, it parses the Classes contained in the dialect plugins. For the parsed Classes, it determines whether they implement the preset Dialect interface. Classes that meet the requirements of the Dialect interface are identified as dialect implementation classes and instantiated, so that all objects entering the current dialect instance management system have the unified database difference processing capabilities specified by the Dialect interface.

[0048] In one specific implementation, the Dialect interface may include methods for obtaining the database type, obtaining the database time SQL, date conversion, record quantity limit, table creation processing, database function processing, table list retrieval, field processing, database keyword processing, and determining whether the target database supports a specific deletion syntax. Each dialect implementation class implements the corresponding interface methods according to the SQL syntax of its adapted database.

[0049] For a dialect object that has been instantiated, the database that the corresponding dialect instance is adapted to can be determined by the correspondence between the database identifier and the dialect implementation class identifier in the dialect configuration, and by combining the database type information provided by the loaded dialect implementation. The obtained dialect instance is then saved in the newly formed dialect instance cache so that the corresponding instance can be obtained by the target database identifier in the subsequent database access process.

[0050] For example, when the current dialect plugin collection contains dialect implementation classes corresponding to both MySQL and Oracle databases, after reloading, corresponding MySqlDialect and OracleDialect instances can be instantiated respectively. When the target database identifier corresponds to a MySQL database, a MySqlDialect instance is obtained; when the target database identifier corresponds to an Oracle database, an OracleDialect instance is obtained. This allows the SQL construction process to use a unified Dialect interface call method to handle the specific SQL differences between different databases.

[0051] When a new database dialect plugin is added to the extension directory, FileWatchService detects the addition event and triggers the reconstruction of the aforementioned dialect instance cache. The new plugin becomes part of the changed current dialect plugin set, and its valid dialect implementation classes can be reloaded to form corresponding dialect instances. When an existing dialect plugin is modified, the corresponding dialect implementation class is reloaded with the modified dialect plugin. When an existing dialect plugin is deleted, the deleted plugin does not enter the new loading process. Thus, the reconstructed dialect instance cache reflects the database dialect capabilities provided by the dialect plugins that can currently participate in runtime loading.

[0052] In this embodiment, the reconstruction of the dialect instance cache is used to re-establish the correspondence between the running dialect instance state and the current dialect plugin state. What is re-established is the class loading state and dialect instance management state corresponding to the dialect plugin. The business application itself is still running, so the newly added or modified database dialect can participate in subsequent actual database access without restarting the entire business application. Example 3

[0053] This embodiment, based on the above embodiments, further explains the target database identification, dialect instance acquisition, request-side supplementary loading, and SQL difference processing processes. When the business application receives a data access request for the target database, it first determines the database connection object that has established a connection with the target database. In one specific implementation, it obtains DatabaseMetaData through the database connection object, then obtains DatabaseProductName through the database metadata, and determines the database identifier corresponding to this data access request based on the obtained database product name.

[0054] The dialect configuration records the configuration relationship between the database identifier and the corresponding dialect implementation class. In a specific implementation, the meta field can record the database product identifier and the clazz field can record the corresponding dialect implementation class identifier. Therefore, once the target database identifier is determined, the dialect manager can use the database identifier to determine the dialect implementation class corresponding to the target database and further obtain or create the corresponding dialect instance.

[0055] The dialect instance cache is used to store dialect objects that have been instantiated and can participate in the current data access processing. In one specific implementation, the corresponding dialect instance can be associated with the dialect implementation class identifier. When the actual data access process needs to obtain the dialect instance based on the target database identifier, the dialect implementation class identifier corresponding to the target database is determined according to the dialect configuration, and then the cached dialect instance is obtained based on the implementation class identifier, so as to obtain the corresponding dialect instance based on the target database identifier.

[0056] When the dialect instance corresponding to the target database can be obtained from the current dialect instance cache, the dialect manager directly provides the obtained dialect instance to the SQL builder. When the corresponding dialect instance cannot be obtained, the dialect manager rereads or queries the current dialect configuration and determines whether a corresponding dialect implementation class configuration exists based on the target database identifier.

[0057] If a dialect implementation class corresponding to the target database identifier exists in the dialect configuration, the dialect implementation class to be loaded is determined according to the corresponding dialect implementation class identifier, loaded and instantiated, and then the resulting dialect instance is saved to the dialect instance cache. At the same time, the resulting dialect instance is provided to the current data access request that triggered the cache miss, so that the current data access request can continue to enter the subsequent SQL generation process. If the corresponding dialect implementation class configuration does not exist in the dialect configuration, the corresponding dialect instance cannot be obtained, and an exception is thrown.

[0058] After obtaining the dialect instance corresponding to the target database, the SQL builder constructs SQL based on the current data operation request. During the SQL generation process, it calls the corresponding methods in the Dialect interface for the preset database differences that actually occur. The Dialect interface provides a unified calling method, while different target databases correspond to different dialect implementations. Different dialect instances generate corresponding SQL expressions according to the SQL syntax of the corresponding database.

[0059] For example, for data operation requirements to query the first 100 records, the SQL builder first forms the corresponding basic query SQL and calls the record count limit handling method wrapLimitSql in the Dialect interface.

[0060] When the target database is a MySQL database, it is generated by MySqlDialect according to the syntax supported by the MySQL database: SELECT * FROM T_USER LIMIT 100 When the target database is an Oracle database, it is generated by OracleDialect according to the syntax supported by the Oracle database: SELECT * FROM (SELECT * FROM T_USER) t WHERE t.rownum <= 100 Therefore, for the same record count limitation requirement, the SQL builder uses the same Dialect interface method to initiate the call, while the dialect instances corresponding to different target databases form record count limitation expressions adapted to the SQL syntax of the target database.

[0061] For other preset database differences such as pagination queries, database functions, date conversions, database keyword processing, or data definition languages, the SQL builder can call the corresponding methods in the Dialect interface based on the differences actually involved in the current SQL generation process. Then, the corresponding SQL expression supported by the target database is generated by the dialect of the target database. This embodiment does not require all preset database differences to exist simultaneously in a single data access process.

[0062] After completing the corresponding database difference processing, the SQL builder obtains the SQL applicable to the target database, and then executes the SQL through the target database connection to complete this data access request. Since the dialect instance called in the actual SQL generation process comes from the current dialect instance management system, the dialect instances obtained by reloading due to changes in dialect plugins during the operation, as well as the dialect instances obtained by supplementing the loading after a cache miss occurs in the data access request, can all participate in the subsequent actual SQL generation.

[0063] For example, after adding a dialect plugin corresponding to the Oracle database and updating the dialect instance during the operation of the business system described in Example 1, when a data access request for the Oracle database is generated, the corresponding database identifier can be obtained according to the Oracle database connection, and the OracleDialect instance that can be called can be obtained accordingly. The OracleDialect processes the Oracle database differences involved in the current SQL generation process, and then executes the generated SQL through the Oracle database connection.

[0064] In another scenario, the dialect configuration has already recorded the correspondence between a target database and the corresponding dialect implementation class, but the dialect instance cache has not yet obtained the dialect instance corresponding to the target database. When a data access request for the target database arrives, the cache miss triggers the supplementary loading process. The corresponding dialect implementation class is determined according to the current dialect configuration and instantiated. The obtained dialect instance is saved in the dialect instance cache, and the obtained dialect instance is used to continue processing the current data access request. Thus, the supplementary loading process on the request side becomes the continuing processing path in the actual database access process. Example 4

[0065] This embodiment provides a data access device that is compatible with multiple databases, including a database identifier acquisition unit, a dialect plugin monitoring unit, a dialect instance management unit, and an SQL processing unit. Each unit is logically divided according to the corresponding function undertaken in the data access process, and they cooperate with each other to complete target database identification, runtime dialect plugin management, dialect instance acquisition, and SQL generation and execution.

[0066] The database identifier acquisition unit is used to obtain the target database identifier corresponding to the data access request. In one specific implementation, the database identifier acquisition unit obtains DatabaseMetaData through the database connection object corresponding to the target database, determines the database identifier corresponding to the target database based on DatabaseProductName in the database metadata, and then provides the obtained database identifier to the dialect instance management unit.

[0067] The dialect plugin monitoring unit is used to monitor changes in the current dialect plugin set during the operation of business applications. In one specific implementation, FileWatchService monitors the addition, modification or deletion events in the dialect plugin extension directory, and provides plugin change information to the dialect instance management unit after detecting the corresponding dialect plugin change.

[0068] The dialect instance management unit is used to manage dialect configuration, dialect instance caching, and the dialect plugin runtime loading process. In one specific implementation, the dialect instance management unit includes DialectManager and DialectClassLoader. After receiving dialect plugin change information, DialectManager re-executes dialect loading based on the changed current dialect plugin set and rebuilds the dialect instance cache for obtaining the corresponding dialect instance based on the database identifier. DialectClassLoader is used to load the valid dialect implementation classes in the current dialect plugin and form the corresponding dialect instance.

[0069] In a specific dialect instance cache reconstruction method, DialectManager closes the original DialectClassLoader and clears the original dialect instance cache, then creates a new DialectClassLoader. The new DialectClassLoader scans the current dialect plugin and loads the dialect implementation class that meets the requirements of the Dialect interface. Then, it instantiates the dialect implementation class and rebuilds the dialect instance cache, thereby enabling subsequent database accesses to obtain the dialect instance corresponding to the currently loadable dialect plugin.

[0070] The dialect instance management unit is also used to handle dialect instance queries during actual database access. When a corresponding dialect instance can be obtained based on the target database identifier, the dialect instance is provided to the SQL processing unit. When a corresponding dialect instance cannot be obtained based on the target database identifier, the dialect implementation class corresponding to the target database identifier is determined based on the dialect configuration. If a corresponding configuration exists, the dialect implementation class is loaded and instantiated. The obtained dialect instance is saved to the dialect instance cache and provided to the current data access process. If a corresponding dialect configuration does not exist, an exception is thrown.

[0071] The SQL processing unit is used to construct SQL based on the current data operation request, obtain the corresponding dialect instance based on the target database identifier, call the dialect instance to process the preset database difference items involved in the SQL generation process, form SQL applicable to the target database, and then execute the SQL through the target database connection; the preset database difference items may include one or more of the following: record quantity limit, pagination query, database function, date conversion, database keyword processing, and data definition language operations.

[0072] The aforementioned database identifier acquisition unit, dialect plugin monitoring unit, dialect instance management unit, and SQL processing unit are logical functional divisions formed according to the corresponding processing functions in the data access process. Each unit can call each other in the data processing process of business applications, and they are not required to correspond to independent physical structures.

[0073] In one optional data operation implementation, the business application can also set up an Entity Mapping File (RDM). The Entity Mapping File describes the mapping relationship between Java entity objects and database tables and table fields. After the system reads and parses the Entity Mapping File, it forms the corresponding Entity information. Based on the table and field information recorded in the Entity, it determines the database table and fields corresponding to the data operation, constructs data operation SQL such as Insert, Update, Delete, or Select according to the data operation type, and then executes the corresponding SQL through the database connection.

[0074] This invention monitors changes to the dialect plugin set during application operation. When a dialect plugin is added, modified, or deleted, the dialect implementation class is reloaded and instantiated based on the changed dialect plugin set. The dialect instance cache used to obtain dialect instances based on database identifiers is also rebuilt. Simultaneously, when the actual data access request fails to obtain the dialect instance corresponding to the target database, supplementary loading is performed according to the dialect configuration. This allows the active update path triggered by plugin changes and the supplementary loading path triggered by data access requests to work together on the dialect instance management system used for subsequent SQL generation. This enables newly added or modified database dialects during operation, as well as currently missing but already configured database dialects, to participate in subsequent actual database accesses without requiring the entire business application to be restarted due to changes in database dialects.

[0075] This invention uses a dialect plugin extension directory, a file monitoring service, a dialect manager, and a dialect class loader to handle runtime changes in dialect plugins. After a plugin changes, dialect loading is re-executed based on the current set of dialect plugins. The loaded classes are then validated using preset dialect interfaces to form corresponding dialect instances. A corresponding update relationship is established between the dialect plugin file status and the dialect instances that can be accessed during runtime, ensuring that the status of newly added, modified, or deleted dialect plugins is reflected in subsequent dialect instance acquisition processes.

[0076] This invention can also obtain the corresponding database identifier based on the target database connection, and obtain the corresponding dialect instance based on the database identifier. Then, the dialect instance corresponding to different databases processes the preset database differences involved in the SQL generation process, such as record quantity limit, pagination query, database function, date conversion, database keyword processing, or data definition language. This enables the same data access process to generate and execute the corresponding SQL according to the SQL expression method of the target database, realizes data access adaptation for different types of databases, and allows the dialect instance formed or supplemented during runtime to actually participate in the SQL generation and execution process of the target database.

[0077] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.

Claims

1. A data access method adaptable to multiple databases, characterized in that, Includes the following steps: Obtain the target database identifier corresponding to the data access request; During application operation, monitor changes in the dialect plugin set. When a dialect plugin is added, modified, or deleted, reload and instantiate the dialect implementation class based on the changed dialect plugin set, and rebuild the dialect instance cache used to obtain the corresponding dialect instance based on the database identifier. For data access requests to the target database, the corresponding dialect instance is retrieved from the dialect instance cache based on the target database identifier. If it is not retrieved, the corresponding dialect implementation class is determined based on the dialect configuration that records the correspondence between the database identifier and the dialect implementation class. After loading and instantiating, it is written to the dialect instance cache. The function calls the obtained dialect instance to process the preset database differences in the SQL generation process, and generates and executes SQL applicable to the target database.

2. The data access method adaptable to multiple databases according to claim 1, characterized in that, After detecting a change in the dialect plugin set, the original dialect class loader is closed and the original dialect instance cache is cleared. A new dialect class loader is created, and the dialect implementation classes in the changed dialect plugin set are reloaded and instantiated. The dialect instance cache is then rebuilt with the obtained dialect instances.

3. The data access method adaptable to multiple databases according to claim 1, characterized in that, After loading the classes in the dialect plugin, the loaded classes are validated for dialect interfaces. The classes that implement the preset dialect interfaces are identified as dialect implementation classes, and the dialect implementation classes are instantiated as dialect instances.

4. The data access method adaptable to multiple databases according to claim 1, characterized in that, The dialect plugin is set in the dialect plugin extension directory. The file monitoring service monitors file changes in the dialect plugin extension directory. When a new, modified, or deleted event of a dialect plugin is detected, the dialect manager is notified to rebuild the dialect instance cache.

5. The data access method adaptable to multiple databases according to claim 2, characterized in that, The new dialect class loader scans the dialect plugin files in the dialect plugin extension directory, configures the dialect plugin files as loadable resources for the new dialect class loader, and loads the dialect implementation classes in the dialect plugin files.

6. The data access method adaptable to multiple databases according to claim 1, characterized in that, Obtaining the target database identifier includes: obtaining the database connection object that establishes a connection with the target database, obtaining the database metadata based on the database connection object, and determining the target database identifier based on the database product name in the database metadata.

7. The data access method adaptable to multiple databases according to claim 1, characterized in that, The dialect configuration includes a database identifier and a dialect implementation class identifier corresponding to the database identifier. The dialect manager determines the corresponding dialect implementation class from the dialect configuration based on the target database identifier.

8. The data access method adaptable to multiple databases according to claim 7, characterized in that, If the corresponding dialect instance is not obtained from the dialect instance cache based on the target database identifier, and if a dialect implementation class identifier corresponding to the target database identifier exists in the dialect configuration, then the corresponding dialect implementation class is loaded and instantiated, the obtained dialect instance is written to the dialect instance cache and provided to the current data access request; if the corresponding dialect implementation class identifier does not exist, then an exception is thrown.

9. The data access method adaptable to multiple databases according to claim 1, characterized in that, The preset database difference items include at least one of the following: record quantity limit, pagination query, database function, date conversion, database keyword processing, and data definition language operation. Different dialect instances generate SQL expressions suitable for the corresponding database based on the preset database difference items.

10. A data access device adaptable to multiple databases, characterized in that, It includes a database identifier acquisition unit, a dialect plugin monitoring unit, a dialect instance management unit, and an SQL processing unit; The database identifier acquisition unit is used to acquire the target database identifier; The dialect plugin monitoring unit is used to monitor changes in the set of dialect plugins during application operation; The dialect instance management unit is used to reload and instantiate the dialect implementation classes based on the changed dialect plugin set when dialect plugins are added, modified or deleted, rebuild the dialect instance cache used to obtain dialect instances based on database identifiers, and supplement the loading of corresponding dialect instances according to dialect configuration when the corresponding dialect instance of the target database is not obtained. The SQL processing unit is used to call the obtained dialect instance to process the preset database difference items, generate and execute SQL applicable to the target database.

Citation Information

Patent Citations

  • Database adaptation method and device based on JDBC

    CN116089542A

  • Dynamic ORM component based on Java

    CN117667260A