Multi-data source management method and system based on Spring project and electronic equipment

By configuring multi-data sources and custom data source annotations in Spring projects, dynamic data source switching and context delivery are achieved, which solves the problems of complex transaction management and lack of read and write separation in Spring multi-data source scenarios, and realizes flexible multi-data source transaction management and database read and write separation.

CN120234318APending Publication Date: 2025-07-01BEIJING ZIJING TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510389364.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-31
Publication Date
2025-07-01

AI Technical Summary

Technical Problem

The existing Spring technology system has complex transaction management, poor scalability, and lacks support for database read and write separation in multiple data source scenarios, resulting in high code complexity, difficulty in maintenance and increased transaction delays across data source.

Method used

By configuring multiple data sources in Spring configuration files and customizing data source annotation marks, dynamic switching of data sources can be achieved at runtime; defining the data source context delivery mechanism, and automatically selecting the target data source; setting up a virtual data source binding to Spring Transaction Manager to cache the resources of the previous transaction when switching data sources in a transaction.

Benefits of technology

It realizes multi-data source transaction management in Spring project, allows new transactions to be started in Spring transactions, and dynamically switches data sources, solving the problems of complex configuration and poor transaction isolation in traditional methods, and implements the read and write separation of databases.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure HDA0005337001270000011
    Figure HDA0005337001270000011
  • Figure HDA0005337001270000021
    Figure HDA0005337001270000021
  • Figure HDA0005337001270000031
    Figure HDA0005337001270000031
Patent Text Reader

Abstract

The invention relates to the technical field of data source synchronous management, in particular to a Spring framework-based data source synchronous management method and system and electronic equipment, which comprises the following steps of: configuring a plurality of data sources in a Spring configuration file, and dividing the data sources into read data sources and write data sources; according to the read-write attribute of the data source, customizing a data source annotation mark, and obtaining a dynamic switching position of the data source in the transaction during operation; defining a data source context transmission mechanism, storing and transmitting a current data source name and read-write attributes, and automatically selecting a target data source; setting a virtual data source used for managing all data sources, and binding the virtual data source as a single data source with the Spring transaction manager; when the data source is switched in the transaction, the resource of the previous transaction is cached, and the actual data source is switched according to the context of the data source. Through the scheme, the method is suitable for Spring projects needing to use multiple data sources at the same time, and the problems that in a traditional method, configuration is complex, and transaction isolation is poor are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of data source synchronization management, and particularly relates to a multi-data source management method, system and electronic device based on a Spring project. Background Art

[0002] In current enterprise application development, with the increase in business complexity and the explosive growth of data scale, the system architecture is gradually evolving towards microservices, multi-tenancy, database sharding and table partitioning, etc. In such architectures, the scenario where a single application may need to connect to multiple databases simultaneously is becoming increasingly common. Possible scenarios include, for example, vertical splitting of business modules: different modules (such as orders, users, payments) use independent databases; read-write separation and load balancing: the master database is responsible for write operations, and the slave database cluster bears high-concurrency queries; in addition, there are also scenarios such as multi-tenant data isolation (distinguishing databases), etc.

[0003] Java enterprise application development often uses Spring for construction and development, so basically Spring manages database transactions. In the existing Spring technology system, the transaction manager by default adopts a single data source binding mechanism, and the above-mentioned multi-data source scenario requirements pose a severe challenge to the traditional transaction management mechanism. A common design solution is to use multiple data sources to bind multiple transaction managers. This will increase the code complexity of the application and the maintenance burden on developers.

[0004] Moreover, the Spring framework itself does not support database read-write separation and needs to be implemented by itself. In current common implementation solutions, the setting granularity and complexity of read-write separation vary greatly.

[0005] In the current Spring architecture, database transaction management usually needs to be initialized with one data source; it only supports the transaction atomicity and consistency of a single database and cannot directly adapt to the multi-data source scenario. When existing technical solutions attempt to solve this problem, they all have significant defects.

[0006] In traditional methods, a common method is to implement multi-data source transaction management by configuring multiple transaction managers and perform static multi-data source manual switching; similarly, read-write separation is similar. But this method has the following defects.

[0007] Complex transaction management and poor scalability: The configuration of multiple transaction managers is complex. It is necessary to manually specify the transaction manager in the code, which increases the complexity of the code; it is difficult to add new data sources later.

[0008] High code invasiveness: It is necessary to explicitly specify which data source or transaction manager to use in the code, and the data source switching code (such as using ThreadLocal to save the data source identifier) is mixed in the business logic, reducing the maintainability.

[0009] Nested call transaction failure: If method A (using data source DSA) calls method B (using data source DSB), due to the lack of coordination between the transaction propagation mechanism and data source switching, the data source cannot be dynamically switched within the Spring transaction boundary; or it may lead to connection leakage or loss of data source context.

[0010] Another common method is to introduce a distributed transaction framework; based on the XA protocol or two-phase commit (2PC), the global transaction coordinator ensures the atomicity of cross-data source operations, but there are the following defects:

[0011] It is necessary to introduce an additional global transaction coordinator component; this brings complexity to application interaction and operation and maintenance.

[0012] Cross-data source transactions rely on distributed transaction protocols (such as XA). The XA protocol requires multiple network communications and resource locks, resulting in a significant increase in latency in high-concurrency scenarios.

[0013] Some databases or middleware do not support the XA protocol well (such as the XARECOVER vulnerability in MySQL).

[0014] Over-design. For lightweight multi-data source requirements (such as only requiring dynamic routing without strong consistency), introducing a distributed transaction framework is not worth the effort. Summary of the Invention

[0015] Based on this, in view of the above technical problems, it is necessary to provide a multi-data source management method, system and electronic device based on the Spring project, which can implement multi-data source transaction management in the Spring project, allow starting a new transaction in the Spring transaction and dynamically switching the data source; solve problems such as complex configuration and poor transaction isolation in traditional methods.

[0016] To achieve the above invention purpose, the present invention adopts the following technical solutions:

[0017] In a first aspect, the present invention provides a data source synchronization management method based on the Spring framework, the method includes:

[0018] Configure multiple data sources in the Spring configuration file and divide them into read data sources and write data sources;

[0019] According to the read-write attributes of the data source, customize the data source annotation mark to obtain the position where the data source in the transaction is dynamically switched during runtime;

[0020] Define a data source context transfer mechanism to store and transfer the current data source name and read-write attributes, and automatically select the target data source;

[0021] Set up a virtual data source for managing all data sources, and bind it to the Spring transaction manager as a single data source; when switching data sources in a transaction, cache the resources of the previous transaction and switch the actual data source according to the data source context.

[0022] In one embodiment, configuring multiple data sources in a Spring configuration file and dividing them into read data sources and write data sources includes: marking the data source read / write properties through data source annotations; if the data source annotation is not used, the write data source of the data source is used by default.

[0023] In one embodiment, obtaining the position of the data source dynamically switched at runtime in the transaction according to the read / write attribute of the data source includes:

[0024] Respond to data source switching demand information and determine the scope of data sources to be annotated; filter data sources related to the business scope, and determine the location of dynamic switching of data sources in transactions at runtime based on data source annotation tags.

[0025] In one embodiment, the method of determining the location of the data source dynamically switched at runtime in a transaction based on the data source annotation tag includes: obtaining annotation information representing the meaning of different data sources by the data source type, id, tenant, and read / write type; adding annotation tags at the location of the Spring transaction method, and associating the data source annotation tag information obtained by the tag with the id or name of the data source dynamically switched at runtime in the transaction to obtain the data source location information and attribute values.

[0026] In one embodiment, the automatic selection of the target data source includes: a data source context transfer mechanism, which uses AOP interception and thread local variables to maintain data source consistency during transaction propagation to ensure that the target data source is automatically selected when the ORM framework executes JDBC operations.

[0027] In one embodiment, the Spring transaction manager can suspend the current transaction and re-initiate another transaction in a Spring transaction. Transactions are executed in an interleaved and isolated manner, and the ACID properties of the transaction are maintained within a single transaction.

[0028] In one embodiment, the method further includes: parsing data source annotation tags, obtaining data source names and read / write information, building a data source context and passing it within the thread; before the method execution interface call, the Spring transaction manager calls a virtual data source with a routing function to obtain a connection; the virtual data source finds the corresponding real data source according to the data source context and returns a database connection; the Spring transaction manager uses the data source connection to perform JDBC operations through the ORM framework.

[0029] In one embodiment, the method further includes: when nestedly invoking other transaction methods, if the current data source is different from the previous data source in the transaction, start the mechanism for switching data sources and transactions, empty and temporarily store the resources of the previous transaction, the Spring transaction manager starts a new transaction, uses the new data source connection to execute new JDBC operations, and restores the transaction resources of the previous transaction after committing the current new transaction.

[0030] In a second aspect, the present invention provides a data source synchronization management method system based on the Spring framework, including:

[0031] A configuration module for configuring multiple data sources in the Spring configuration file and dividing them into read data sources and write data sources;

[0032] A marking module for customizing data source annotation marks according to the read / write attributes of the data source and obtaining the dynamic switching position of the data source in the transaction at runtime;

[0033] A definition module for defining a data source context passing mechanism, storing and passing the current data source name and read / write attributes, and automatically selecting the target data source;

[0034] A synchronous acquisition module for setting a virtual data source for managing all data sources, binding it to the Spring transaction manager as a single data source; when switching data sources in the transaction, cache the resources of the previous transaction and switch the actual data source according to the data source context.

[0035] In a third aspect, the present invention provides an electronic device, including at least one processor; and

[0036] A memory communicatively connected to the at least one processor; wherein,

[0037] The memory stores a computer program executable by the at least one processor, and when the computer program is executed by the at least one processor, the at least one processor is enabled to execute the method according to any one of the first aspect.

[0038] Compared with the closest prior art, the beneficial effects of the present invention are:

[0039] The present invention provides a multi-data source management method, system and electronic device based on a Spring project, which includes configuring multiple data sources in the Spring configuration file and dividing them into read data sources and write data sources; customizing data source annotation marks according to the read-write attributes of the data sources to obtain the dynamic switching position of the data source in the transaction at runtime; defining a data source context passing mechanism to store and pass the current data source name and read-write attributes, and automatically select the target data source; setting a virtual data source for managing all data sources, binding it to the Spring transaction manager as a single data source; when switching data sources in the transaction, caching the resources of the previous transaction and switching the actual data source according to the data source context. By developing the above data source manager, data source context, data source dynamic switching mechanism, data source annotation, transaction resource caching and restoration mechanism, and integrating them with the Spring transaction framework, the following problems can be effectively solved:

[0040] 1. The problem of multi-data source configuration in the Spring project and integration with the ORM framework;

[0041] 2. The problem of losing the data source context due to dynamic data source switching in nested method calls within a Spring transaction;

[0042] 3. Lack of atomicity guarantee for cross-data source operations;

[0043] 4. Achieving database read-write separation without changing the existing package and class hierarchy.

[0044] The multi-data source management method, system and electronic device based on the Spring project proposed by the present invention can switch data sources in the transaction, temporarily jump out of the current transaction, start a new transaction and easily achieve database read-write separation without introducing a distributed transaction framework. Through the technical solution of the present invention, multi-data source transaction management can be realized in the Spring project, allowing new transactions to be started and data sources to be dynamically switched in the Spring transaction. It solves the problems of complex configuration and poor transaction isolation in traditional methods.

[0045] The present invention has the advantages of small development effort, simple configuration, small code invasiveness, easy extensibility, etc., and is applicable to Spring projects that need to use multiple data sources simultaneously. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] The drawings are only for the purpose of illustrating specific embodiments and are not considered to be a limitation of the present invention. Throughout the drawings, the same reference numerals represent the same components. Obviously, the drawings in the following description are only some embodiments recorded in the embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings based on these drawings;

[0047] Figure 1 Flow chart of the multi - data source management method based on Spring project in the embodiment of the present invention;

[0048] Figure 2 Schematic diagram of the specific operation process implementation principle of the multi - data source management method based on Spring project in the embodiment of the present invention;

[0049] Figure 3 Schematic diagram of the structure of the multi - data source management system based on Spring project in the embodiment of the present invention;

[0050] Figure 4 Schematic diagram of the structure of an electronic device in the embodiment of the present invention. Specific implementation manners

[0051] Hereinafter, embodiments of the technical solutions of the present invention will be described in detail with reference to the accompanying drawings. The following embodiments are only used to illustrate the technical solutions of the present invention more clearly, and thus are only examples and cannot be used to limit the protection scope of the present invention.

[0052] It should be noted that unless otherwise specified, the technical terms or scientific terms used in this application should have the ordinary meanings understood by those skilled in the art to which the present invention belongs.

[0053] Based on the above problems, the present invention provides a multi - data source management method, system and electronic device based on Spring project. In the embodiment of the present invention, by developing a data source manager, a data source context, a data source dynamic switching mechanism, data source annotations, a transaction resource temporary storage and recovery mechanism, and integrating with the Spring transaction framework, the following multiple features are overall realized.

[0054] Free definition of data sources: including a primary data source (primaryDataSource) and multiple other data sources (otherDataSource); each data source can be further divided into read and write data sources;

[0055] Annotation - driven routing: Mark the data source used by the method through a custom annotation @DataSource(name, R / W);

[0056] Transaction context passing: By intercepting with AOP and using thread-local variables, data source consistency is maintained during transaction propagation. Eventually, when the ORM framework executes API interface calls (application programming). It provides Java developers with a standard and unified way to access various types of databases, such as MySQL, Oracle, SQL Server, etc., enabling developers to use a set of common Java code to operate different database systems without having to write specific code for each database. Through JDBC, developers can implement functions such as database connection, execution of SQL statements (including queries, inserts, updates, deletes, etc.), and processing of the result sets returned by the database. When operating, the accurate data source is automatically selected;

[0057] Cross-database transactions: In a Spring transaction, the current transaction can be suspended and another database transaction can be initiated. The transactions are executed interleaved and isolated from each other; the ACID characteristics of the transaction are maintained within a single transaction. However, unlike fractional transactions, the present invention does not guarantee strong consistency between all transactions.

[0058] In one embodiment, the present invention provides a multi-data source management method based on a Spring project. The embodiments of the present invention will be described below with reference to the accompanying drawings. Please refer to Figure 1 , and the specific steps include:

[0059] S101 Configure multiple data sources in the Spring configuration file and divide them into read data sources and write data sources;

[0060] S102 According to the read-write attributes of the data source, customize the data source annotation mark to obtain the position where the data source in the transaction is dynamically switched at runtime;

[0061] S103 Define a data source context passing mechanism to store and pass the current data source name and read-write attributes, and automatically select the target data source;

[0062] S104 Set up a virtual data source for managing all data sources, bind it to the Spring transaction manager as a single data source; when switching data sources in a transaction, cache the resources of the previous transaction, including the transaction name, status, transaction synchronizer, etc.; switch the actual data source according to the data source context.

[0063] Through the above steps, multiple data sources can be defined in the Spring configuration file, and the read and write data sources can be distinguished according to business requirements. The key lies in creating a dynamic data source routing and using AOP to achieve dynamic switching of data sources.

[0064] Among them, the content that needs to be developed for the above steps is as follows:

[0065] 1. Develop a data source configuration function that allows multiple data sources to be configured and differentiates between read and write data sources;

[0066] 2. Develop a custom annotation @DataSource(name, R / W) that can specify data source properties in the annotation;

[0067] 3. Develop a data source context object and passing mechanism for storing and passing the current data source name and read / write properties;

[0068] 4. Inherit Spring's AbstractRoutingDataSource to implement a virtual data source with routing capabilities; manage all built-in data sources; be able to switch the actual data source according to the data source context; and bind this virtual data source as a single data source to the Spring transaction manager and register and bind it to the ORM framework;

[0069] 5. Develop an AOP aspect to intercept the @DataSource annotation and dynamically change the data source context during method execution to achieve the effect of switching data sources;

[0070] 6. Develop a mechanism for temporarily storing, clearing, and restoring Spring transaction resources when switching data sources in a transaction to solve the problem of ineffective data source switching within a transaction

[0071] 7. In the Service methods of the project, specify the data source and read / write properties required by the method through the @DataSource annotation. For example:

[0072] @DataSource(name=”otherDS”,RW=”Read”)

[0073] public void doSomething(){…}

[0074] Specify to use the data source named "otherDS" and the read database during the execution of the method. If no annotation is specified, the write database of the primary data source is used by default.

[0075] Based on the above method steps, in step S101, the configuration of multiple data sources in the Spring configuration file and the division into read data sources and write data sources include: marking the read / write properties of the used data source through the data source annotation. If the data source annotation is not used for marking, the write data source of the data source is used by default.

[0076] In the above embodiment, the obtaining of the position where the data source in the transaction is dynamically switched according to the read / write properties of the data source includes:

[0077] Respond to data source switching demand information and determine the scope of data sources to be annotated; filter data sources related to the business scope, and determine the location of dynamic switching of data sources in transactions at runtime based on data source annotation tags.

[0078] In the above embodiment, the determination of the position of the data source dynamically switched at runtime in the transaction based on the data source annotation tag includes: obtaining annotation information representing the meaning of different data sources by the data source type, id, tenant, and read / write type; adding annotation tags at the position of the Spring transaction method, and associating the data source annotation tag information obtained by the tag with the id or name of the data source dynamically switched at runtime in the transaction to obtain the data source location information and attribute values.

[0079] Based on the above method steps, it can be known that in step S102, the automatic selection of the target data source includes: a data source context transfer mechanism, which uses AOP interception and thread local variables to maintain data source consistency during transaction propagation to ensure that the target data source is automatically selected when the ORM framework executes JDBC operations.

[0080] In the above embodiment, the Spring transaction manager can suspend the current transaction and re-initiate another transaction in a Spring transaction. Transactions are executed in an interleaved and isolated manner, and the ACID properties of transactions are maintained within a single transaction.

[0081] In the above embodiment, the method also includes: parsing the data source annotation tag, obtaining the data source name and read / write information, building the data source context and passing it within the thread, before the method execution interface call, the Spring transaction manager calls the virtual data source with routing function to obtain the connection, the virtual data source finds the corresponding real data source according to the data source context, and returns the database connection, and the Spring transaction manager uses the data source connection to perform JDBC operations through the ORM framework.

[0082] In the above embodiment, the method also includes: when nested calls to other transaction methods, if the current data source is different from the previous data source in the transaction, the mechanism for switching data sources and transactions is started, the resources of the previous transaction are cleared and temporarily stored, the Spring transaction manager starts a new transaction, uses the new data source connection to execute new JDBC operations, and restores the transaction resources of the previous transaction after the current new transaction is committed.

[0083] Through the above steps, a specific application scenario is simulated to further illustrate the specific implementation of the multi-data source management method based on the Spring project: Figure 2 shown.

[0084] A1: The AOP aspect in the application intercepts the methods to be executed that are annotated with @DataSource at runtime.

[0085] A2: Before the method to be executed is executed, parse the @DataSource annotation to obtain the data source name and read / write information in the annotation.

[0086] A3: Use the information obtained by parsing the @DataSource annotation to construct a data source context; and pass it within the thread.

[0087] A4: Before the method executes JDBC, the Spring transaction manager calls the virtual data source with routing function to obtain a connection. The virtual routing data source finds the corresponding real data source according to the data source context, and obtains a database connection from it and returns it to Spring.

[0088] A5: The Spring transaction manager uses the current data source connection to execute JDBC operations through the ORM framework.

[0089] A6: Continue to nestedly call other transaction methods.

[0090] B1: As described in step A4.

[0091] B2: As described in step A3.

[0092] B3: As described in step A2.

[0093] B4: If the current data source is the same as the previous data source in the transaction, execute JDBC operations as described in step A5; otherwise, start the mechanism to switch the data source and transaction, empty and temporarily store the resources of the previous transaction.

[0094] B5: Since the transaction resources have been emptied in the previous step, the Spring transaction manager will start a new transaction.

[0095] B6: The Spring transaction manager uses the new data source connection to execute new JDBC operations.

[0096] B7: Commit the current new transaction.

[0097] B8: Restore the transaction resources of the previous transaction so that the original transaction can continue to execute or be committed and ended.

[0098] Based on the same inventive concept, the embodiment of the present application also provides a log analysis and upload system based on network status for a log analysis and upload method based on network status. The implementation solution provided by this system to solve problems is similar to the implementation solution described in the above method. Therefore, the specific limitations in one or more embodiments of the log analysis and upload system based on network status provided below can refer to the limitations on the log analysis and upload method based on network status in the above text, and will not be repeated here.

[0099] In one embodiment, as Figure 3 shown, a data source synchronization management method and system based on the Spring framework are provided, including: a configuration module 210, an annotation module 220, a definition module 230, and a synchronization acquisition module 240, where:

[0100] The configuration module 210 is used to configure multiple data sources in the Spring configuration file and divide them into read data sources and write data sources;

[0101] The annotation module 220 is used to customize data source annotation marks according to the read / write attributes of the data source and obtain the position where the data source in the transaction is dynamically switched during runtime;

[0102] The definition module 230 is used to define a data source context passing mechanism, store and pass the current data source name and read / write attributes, and automatically select the target data source;

[0103] The synchronization acquisition module 240 is used to set up a virtual data source for managing all data sources, bind it to the Spring transaction manager as a single data source; when switching data sources in a transaction, cache the resources of the previous transaction and switch the actual data source according to the data source context.

[0104] At the same time, this application also proposes a computer-readable storage medium and an electronic device. In one embodiment, the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the steps of the method described in any one of steps S101 to step S104.

[0105] In one embodiment, an electronic device is provided. The electronic device can be a terminal, and its internal structure diagram can be as Figure 4 shown. The electronic device includes a processor, a memory, a communication interface, a display screen, and an input device connected through a system bus. Among them, the processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The communication interface of the electronic device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements the method described in any one of S101 to step S103. The display screen of the electronic device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the electronic device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad provided on the housing of the electronic device, or an external keyboard, a touchpad, or a mouse, etc.

[0106] Those skilled in the art can understand that Figure 4 the structure shown in [[ID=]] is only a block diagram of some structures related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.

[0107] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or fully authorized by all parties.

[0108] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the above method embodiments. Among them, any reference to a memory, database or other medium used in the various embodiments provided in the present application can include at least one of non-volatile and volatile memories. Non-volatile memories can include read-only memory ROM, magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory ReRAM, magnetoresistive random access memory MRAM, ferroelectric random access memory FRAM, phase change memory PCM, graphene memory, etc. Volatile memories can include random access memory RAM or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory SRAM or dynamic random access memory DRAM, etc. The databases involved in the various embodiments provided in the present application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., and are not limited thereto. The processors involved in the various embodiments provided in the present application can be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, data processing logics based on quantum computing, etc., and are not limited thereto.

[0109] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope recorded in this specification.

[0110] The above-described embodiments merely represent several implementation manners of the present application. The description thereof is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all fall within the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the appended claims.

Claims

1. A data source synchronization management method based on Spring framework, characterized in that: The method comprises: Configure multiple data sources in the Spring configuration file and divide them into read data sources and write data sources; Customize the data source annotation tags based on the read and write properties of the data source to obtain the location of the data source dynamically switched at runtime in the transaction; Define the data source context transfer mechanism, store and transfer the current data source name and read-write properties, and automatically select the target data source; Set up a virtual data source for managing all data sources, and bind it to the Spring transaction manager as a single data source; when switching data sources in a transaction, cache the resources of the previous transaction and switch the actual data source according to the data source context.

2. The method according to claim 1, characterized in that The configuration of multiple data sources in the Spring configuration file and dividing them into read data sources and write data sources includes: marking the data source read / write properties through data source annotations. If the data source annotation is not used, the write data source of the data source is used by default.

3. The method according to claim 1, characterized in that The step of obtaining the position of the data source dynamically switched at runtime in the transaction according to the read-write property of the data source includes: Respond to data source switching demand information and determine the scope of data sources to be annotated; filter data sources related to the business scope, and determine the location of dynamic switching of data sources in transactions at runtime based on data source annotation tags.

4. The method according to claim 3, characterized in that The method of determining the location of the data source dynamically switched at runtime in a transaction based on the data source annotation tag includes: obtaining annotation information representing the meaning of different data sources by the data source type, id, tenant, and read / write type; adding annotation tags at the location of the Spring transaction method, and associating the data source annotation tag information obtained by the tag with the id or name of the data source dynamically switched at runtime in the transaction, to obtain the data source location information and attribute values.

5. The method according to claim 1, characterized in that: The automatic selection of the target data source includes: a data source context transfer mechanism, which uses AOP interception and thread local variables to maintain data source consistency during transaction propagation to ensure that the target data source is automatically selected when the ORM framework executes JDBC operations.

6. The method according to claim 1, characterized in that The Spring transaction manager can suspend the current transaction and re-initiate another transaction in a Spring transaction. Transactions are interleaved and isolated from each other, and the ACID properties of transactions are maintained within a single transaction.

7. The method according to claim 1, characterized in that The method also includes: parsing data source annotation tags, obtaining data source names and read / write information, building data source contexts and passing them within threads; before the method execution interface is called, the Spring transaction manager calls a virtual data source with a routing function to obtain a connection; the virtual data source finds the corresponding real data source according to the data source context and returns a database connection; the Spring transaction manager uses the data source connection to perform JDBC operations through the ORM framework.

8. The method according to claim 6, characterized in that The method also includes: when nested calls to other transaction methods, if the current data source is different from the previous data source in the transaction, a mechanism for switching data sources and transactions is started, resources of the previous transaction are cleared and temporarily stored, a Spring transaction manager starts a new transaction, a new JDBC operation is executed using a new data source connection, and transaction resources of the previous transaction are restored after the current new transaction is committed.

9. A data source synchronization management method system based on Spring framework, characterized in that: include: Configuration module, used to configure multiple data sources in the Spring configuration file and divide them into read data sources and write data sources; The annotation module is used to customize the data source annotation tags according to the read and write properties of the data source, and obtain the position of the data source dynamically switched at runtime in the transaction; Definition module, used to define data source context transfer mechanism, store and transfer current data source name and read-write properties, and automatically select target data source; Synchronous acquisition module, used to set up a virtual data source for managing all data sources, and bind to the Spring transaction manager as a single data source; When switching data sources in a transaction, the resources of the previous transaction are cached and the actual data source is switched according to the data source context.

10. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can perform the method according to any one of claims 1 to 8.

Citation Information

Cited By

  • Autonomous transaction implementation method and device based on PostgreSQL database and medium

    CN122387974A