Data source switching method, device and equipment and computer readable storage medium
By switching the main thread's data source through mapping request parameters and setting the subtask's data source in the asynchronous task, the problem of inaccurate data source switching is solved, achieving consistency between parent and child thread data sources and efficient data source switching.
Patent Information
- Application Number
- CN202410594475.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-10
- Publication Date
- 2025-11-11
AI Technical Summary
In the existing technology, inaccurate data source switching leads to inconsistencies between the parent and child thread data sources, causing business data anomalies.
By mapping the current request to a method that handles asynchronous messages, a dynamic proxy is used to switch the main thread data source based on the request parameters. During the logic processing, asynchronous subtasks are started through a thread pool, and data sources are set and removed. Subtask data source switching logic is added to ensure consistency.
It achieves consistency of data sources between parent and child threads, prevents memory leaks, improves the reliability and efficiency of data source switching, and reduces code redundancy.
Smart Images

Figure CN120929199A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a data source switching method, apparatus, device, and computer-readable storage medium. Background Technology
[0002] Currently, when switching data sources, Spring primarily uses the `@Async` annotation to enable asynchronous data source switching for tasks. However, it's difficult to guarantee consistency between the parent and child threads' data sources, leading to cross-database behavior and causing business data anomalies. Cross-database behavior refers to multiple threads accessing the same database connection simultaneously, resulting in data corruption and inconsistency. For example, if there are two data sources, A and B (where A is the default data source), and Spring uses `@Async` to enable an asynchronous task, assuming the parent thread switches to data source B, the child thread cannot know the parent thread's data source using existing data source switching methods. In this case, the child thread will switch to the default data source A, causing inconsistency between the parent and child threads' data sources, resulting in business data anomalies and impacting business logic.
[0003] Therefore, existing technical solutions suffer from inaccurate data source switching. Summary of the Invention
[0004] In view of this, the purpose of the present invention is to provide a data source switching method, apparatus, device and computer-readable storage medium, which solves the technical problem of inaccurate data source switching in the prior art.
[0005] To address the aforementioned technical problems, this invention provides a data source switching method, comprising:
[0006] Map the current request to an asynchronous message processing method, and use the asynchronous message method to determine the request parameters corresponding to the current request;
[0007] The main thread's data source is switched using dynamic proxies based on the data source information carried in the request parameters.
[0008] During the logic processing, when it is determined that an asynchronous task annotation exists, the data source identifier of the main thread is obtained, and an asynchronous subtask is started through the thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic;
[0009] Set the data source for the asynchronous subtask according to the data source identifier;
[0010] Once the asynchronous subtask has been completed, the data source is removed.
[0011] Optionally, before obtaining the main thread's data source identifier and starting an asynchronous subtask through the thread pool when determining the existence of an asynchronous task annotation during the logical processing, the following steps are also included:
[0012] Custom initial thread pool;
[0013] The initial thread pool inherits the parameters of the mall's public thread pool to obtain the thread pool; wherein, the mall's public thread pool includes data source switching logic.
[0014] Optionally, after removing the data source when it is determined that the asynchronous subtask has completed execution, the method further includes:
[0015] Determine whether the asynchronous thread information corresponding to the asynchronous subtask is consistent with the main thread information;
[0016] When they match, reset the data source for the main thread.
[0017] Optionally, the step of switching the main thread's data source based on the data source information carried in the request parameters using a dynamic proxy includes:
[0018] The main thread's data source is switched based on the data source information carried in the request parameters using aspect-oriented programming.
[0019] Optionally, the data source switching process may also include:
[0020] Monitor the data source switching process and obtain data source switching logs;
[0021] When it is determined that the data source switch is complete, logical analysis is performed based on the data source switch log to determine whether the switch logic is consistent with the preset switch logic of the data source.
[0022] If there is a discrepancy, determine whether to send a data source switching error message or determine whether to return to the state before the data source switching.
[0023] Optionally, the process of switching the data source between the main thread and the asynchronous subtask also includes:
[0024] Check the current thread's flag;
[0025] If the flag indicates that the current thread has switched data sources and the current data source has not changed, then the switching operation is skipped;
[0026] If the flag indicates that the current thread has not switched data sources, or that the current data source is invalid, then a data source switching operation is performed, and the flag is updated.
[0027] Optionally, after switching the main thread's data source based on the data source information carried in the request parameters using a dynamic proxy, the method further includes:
[0028] Determine if a scheduled task exists in the aspect where the data source is switched;
[0029] When the existence of the scheduled task is determined, the scheduled task is triggered, and the site annotation is intercepted in the aspect of data source switching;
[0030] Determine whether the site annotation specifies a target site;
[0031] When the site annotation does not specify the target site, the site to be processed is determined to be all sites by default, and all sites are processed according to the business logic corresponding to each site in the all sites.
[0032] When the site annotation specifies the target site, it is determined that the target site will be processed according to the business logic corresponding to each site in the target site;
[0033] The processed site information is stored in the corresponding data source.
[0034] This application also provides a data source switching device, including:
[0035] The request parameter determination module is used to map the current request to an asynchronous message processing method and use the asynchronous message method to determine the request parameters corresponding to the current request.
[0036] The main thread data source switching module is used to switch the main thread's data source based on the data source information carried in the request parameters through a dynamic proxy.
[0037] The asynchronous subtask initiation module is used to obtain the data source identifier of the main thread when it is determined that an asynchronous task annotation exists during the logic processing, and start an asynchronous subtask through a thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic;
[0038] An asynchronous subtask data source setting module is used to set the data source of the asynchronous subtask according to the data source identifier;
[0039] The data source removal module is used to remove the data source when it is determined that the asynchronous subtask has been completed.
[0040] This application also provides a data source switching device, including:
[0041] Memory, used to store computer programs;
[0042] A processor is used to implement the steps of the above-described data source switching method when executing the computer program.
[0043] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the data source switching method described above.
[0044] As can be seen, this invention maps the current request to an asynchronous message processing method, uses the asynchronous message method to determine the request execution logic and request parameters corresponding to the current request, and switches the main thread's data source based on the data source information carried in the request parameters through dynamic proxy. During the logic processing, when an asynchronous task annotation is determined to exist, the main thread's data source identifier is obtained, and an asynchronous subtask is started through a thread pool. The thread pool includes asynchronous subtask data source switching logic. The data source of the asynchronous subtask is set according to the data source identifier. When it is determined that the asynchronous subtask has finished executing, the data source is removed. Compared to the current child thread not knowing the parent thread's data source, this application, by adding subtask data source switching logic, allows the parent thread's data source and thread information to be obtained first, then the subtask is set to the same data source, and the data source is removed after the subtask logic is executed, preventing memory leaks.
[0045] In addition, the present invention also provides a data source switching device, equipment, and computer-readable storage medium, which also have the above-mentioned beneficial effects. Attached Figure Description
[0046] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0047] Figure 1 A flowchart of a data source switching method provided in an embodiment of the present invention;
[0048] Figure 2 A flowchart illustrating an annotation-based site processing method during data switching, provided as an embodiment of the present invention;
[0049] Figure 3 A flowchart illustrating a data source switching method provided in an embodiment of the present invention;
[0050] Figure 4 A flowchart illustrating another data source switching method provided in an embodiment of the present invention;
[0051] Figure 5 This is a schematic diagram of the structure of a data source switching device provided in an embodiment of the present invention;
[0052] Figure 6 This is a schematic diagram of a data source switching device provided in an embodiment of the present invention. Detailed Implementation
[0053] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0054] Please refer to Figure 1 , Figure 1 A flowchart illustrating a data source switching method provided in an embodiment of the present invention. The method may include:
[0055] S101, map the current request to the asynchronous message handling method, and use the asynchronous message method to determine the request parameters corresponding to the current request.
[0056] The execution subject of this invention is an electronic device, but this embodiment is not limited to a specific electronic device. For example, the electronic device in this embodiment can be a computer, mobile phone, tablet, etc. The asynchronous message method in this embodiment is a handler method (a method for processing business logic). This embodiment can determine the request parameters corresponding to the current request, and the request parameters include data source information (country code information).
[0057] S102 switches the main thread's data source based on the data source information carried in the request parameters using dynamic proxy.
[0058] This embodiment can switch the main thread's data source based on the data source information carried in the request parameters using dynamic proxies. This embodiment is not limited to the specific method of switching the main thread's data source based on the data source information carried in the request parameters using dynamic proxies. For example, this embodiment can perform AOP (Aspect-Oriented Programming) aspect interception, switching the corresponding data source based on the country code information carried in the request parameters; or this embodiment can override the Spring's `@Async` annotation method, obtaining the main thread's data source through dynamic proxies when starting an asynchronous thread. Spring's `@Async` annotation is used to declare a method as an asynchronous method, ensuring that it does not block the current thread when called. The dynamic proxy in this embodiment can be a JDK dynamic proxy (a way to implement dynamic proxies in Java, which allows dynamic generation of proxy classes at runtime) or a CGLIB dynamic proxy (a proxy method based on class inheritance, which does not require the proxied object to implement a specific interface).
[0059] It should be further explained that, to avoid code redundancy, the aforementioned method of switching the main thread's data source based on the data source information carried in the request parameters using dynamic proxies can also include: switching the main thread's data source based on the data source information carried in the request parameters using aspect-oriented programming (AOP). This embodiment switches the main thread's data source using AOP, which allows developers to extract common concerns into aspects that can be reused across multiple modules, thus avoiding code redundancy.
[0060] It should be further explained that, in order to improve the site's processing efficiency, after switching the main thread's data source based on the data source information carried in the request parameters using dynamic proxies, the following can also be included:
[0061] S1021, Determine if a scheduled task exists in the aspect where the data source is switched;
[0062] S1022, When it is determined that a scheduled task exists, the scheduled task is triggered, and the site annotation is intercepted in the aspect where the data source is switched;
[0063] S1023, Determine whether the site annotation specifies a target site;
[0064] S1024, When the site annotation does not specify a target site, the site to be processed is determined to be all sites by default, and all sites are processed according to the business logic corresponding to each site in the all sites.
[0065] S1025, when the site annotation specifies the target site, determine to process the target site according to the business logic corresponding to each site in the target site;
[0066] S1026, Store the processed site information into the corresponding data source.
[0067] For easier understanding, please refer to Figure 2 , Figure 2This is a flowchart illustrating an annotation-based site processing method during data switching, provided as an embodiment of the present invention. This embodiment does not limit the specific target site; the target site can be a specified subset of sites or all specified sites. This embodiment can use the `@Scheduled` annotation to define scheduled tasks: In Spring (an open-source platform), the `@Scheduled` annotation is used to declare a method as a scheduled task. This annotation can be directly written on the method, indicating that the method needs to be executed periodically. This embodiment can utilize the `@EnableScheduling` annotation to start scheduled tasks. The `@EnableScheduling` annotation starts a background thread to execute tasks marked with `@Scheduled`. Considering the current logic that scheduled tasks can only process one site at a time, this embodiment defines a site annotation at the code level. This annotation can specify a site, or defaults to all sites. When a scheduled task is triggered, the annotation is intercepted at the data source switching aspect, and the corresponding site information is processed according to the site information specified by the annotation (or all sites if not specified), thus solving the problem that scheduled tasks can only process one site at a time and reducing code maintenance.
[0068] S103, during the logic processing, when it is determined that there is an asynchronous task annotation, the data source identifier of the main thread is obtained, and an asynchronous subtask is started through the thread pool; wherein, the thread pool includes the asynchronous subtask data source switching logic.
[0069] In this embodiment, an asynchronous task refers to a task that does not require an immediate result during computation. It can run in the background while the main thread continues to perform other operations until the asynchronous task is completed. The asynchronous task annotation in this embodiment is primarily used to identify the asynchronous execution characteristics of a method or class. The thread pool in this embodiment includes asynchronous subtask data source switching logic to ensure that subtasks can successfully switch data sources.
[0070] It should be further explained that, to improve the efficiency of the thread pool design, during the logic processing, when an asynchronous task annotation is determined to exist, before obtaining the data source identifier of the main thread and starting an asynchronous subtask through the thread pool, the following can also be included: a custom initial thread pool; inheriting the parameters of the initial thread pool from the parameters of the marketplace's common thread pool to obtain the thread pool; wherein, the marketplace's common thread pool includes data source switching logic. In this embodiment, a custom initial thread pool refers to creating a custom thread pool class, which can internally manage its own thread collection and task queue. It is understandable that, because data source switching is based on the thread pool, a marketplace common thread pool needs to be defined as the pointcut for data source switching, and the data source switching logic is implemented in the common thread pool class. If the defined thread pool does not inherit from the common thread pool but inherits from different parent thread pools, a generalized design cannot be achieved. This embodiment, by directly inheriting the parameters of the marketplace common thread pool from the custom thread pool, enables the rapid design of a thread pool capable of data source switching, improving the efficiency of thread pool switching.
[0071] S104, Set the data source for the asynchronous subtask based on the data source identifier.
[0072] This embodiment sets the data source for asynchronous subtasks based on the data source identifier corresponding to the main thread, thereby improving the accuracy of the data source identifier setting.
[0073] It should be further explained that, to improve the reliability of data source switching, the data source switching process may also include: monitoring the data source switching process and obtaining data source switching logs; when it is determined that the data source switching is complete, performing logical analysis based on the data source switching logs to determine whether the switching logic is consistent with the preset switching logic of the data source; if inconsistent, determining to send a data source switching error message or determining to return to the state before the data source switching. This embodiment performs logical analysis based on the data source switching logs to prevent switching logic errors that could lead to incorrect data source switching, thereby improving the reliability of data source switching.
[0074] It should be further explained that, to improve the efficiency of data source switching, the process of switching the data source between the main thread and the asynchronous subtask may also include: checking the current thread's flag; if the flag indicates that the current thread has already switched data sources and the current data source has not changed, then the switching operation is skipped; if the flag indicates that the current thread has not yet switched data sources, or the current data source is invalid, then the data source switching operation is performed, and the flag is updated. In this embodiment, the determination of whether to switch data sources is necessary is based on the flag. When the data source has already been switched and the current data source has not changed, the data source switching operation is skipped directly to prevent repeated switching, thereby improving the efficiency of data source switching.
[0075] S105: Remove the data source when it is determined that the asynchronous subtask has been completed.
[0076] This embodiment removes the data source when it is determined that the asynchronous subtask has been completed, to prevent memory leaks.
[0077] It should be further explained that, to improve the accuracy of data source settings, after removing the data source upon completion of the asynchronous subtask, the process can further include: determining whether the asynchronous thread information corresponding to the asynchronous subtask is consistent with the main thread information; if consistent, resetting the main thread's data source. In this embodiment, after removing the data source, it checks whether the asynchronous thread information corresponding to the asynchronous subtask is consistent with the main thread information; if consistent, it resets the main thread's data source. It is understandable that threads allocated by the thread pool may be reused, and sometimes the thread allocated to a subtask may be the same as the parent thread. Removing the subtask's data source will remove the parent thread's data source, so it needs to be reset. Therefore, this improves the accuracy of data source settings.
[0078] The data source switching method provided in this embodiment of the invention may include: S101, mapping the current request to a method for processing asynchronous messages, and using the asynchronous message method to determine the request execution logic and request parameters corresponding to the current request; S102, switching the data source of the main thread according to the data source information carried in the request parameters through dynamic proxy; S103, during the logic processing, when it is determined that there is an asynchronous task annotation, obtaining the data source identifier of the main thread, and starting an asynchronous subtask through a thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic; S104, setting the data source of the asynchronous subtask according to the data source identifier; S105, when it is determined that the asynchronous subtask has been completed, removing the data source. This embodiment adds subtask data source switching logic processing, first obtaining the data source and thread information of the parent thread, then setting the same data source for the subtask, and removing the data source after the subtask logic is completed, further preventing memory leaks. Furthermore, this embodiment switches the main thread's data source using aspect-oriented programming (AOP). AOP allows developers to extract common concerns into aspects, which can be reused across multiple modules, thus avoiding code redundancy. Additionally, by directly inheriting the parameters of the e-commerce platform's public thread pool from custom thread pools, this embodiment enables rapid design of thread pools capable of data source switching, improving the efficiency of thread pool switching. Moreover, this embodiment performs logical analysis based on data source switching logs to prevent switching logic errors that could lead to incorrect data source switching, thereby improving the reliability of data source switching. Finally, this embodiment determines whether a data source switch is necessary based on an indicator; if a data source has already been switched and the current data... If the data source remains unchanged, the data source switching operation will be skipped to prevent repeated switching and improve the efficiency of data source switching. Furthermore, this embodiment considers that threads allocated by the thread pool may be reused; sometimes, the thread allocated to a subtask may be the same as the parent thread. Removing the data source of a sub-thread will remove the data source of the parent thread, thus requiring resetting, thereby improving the accuracy of data source settings. The embodiment defines a site annotation at the code level, which can specify a site (defaulting to all sites). When the scheduled task is triggered, this annotation is intercepted at the data source switching aspect, and the corresponding site information is processed according to the site information specified in the annotation (defaulting to all sites if not specified). This solves the problem that the scheduled task can only process one site at a time, reducing code maintenance.
[0079] For a clearer understanding of this invention, please refer to the following details. Figure 3 , Figure 3 A flowchart illustrating a data source switching method provided in this embodiment of the invention may specifically include:
[0080] S201, use the handler method of the service to determine the request execution logic and request parameters.
[0081] The `handler` method in this embodiment is a message-passing mechanism for inter-thread communication, allowing messages to be passed and code to be executed between different threads. This corresponds to the asynchronous message handling method described above.
[0082] S202 switches the data source based on the data source information carried in the request parameters using a dynamic proxy.
[0083] S203, when determining to execute the asynchronous task annotation according to the request execution logic, obtain the data source identifier of the main thread and start an asynchronous subtask through the thread pool.
[0084] S204 sets the data source and thread information of the asynchronous subtask to be consistent with the parent thread settings.
[0085] S205, Remove the data source after executing the subtask logic.
[0086] S206: After removing the subtask data source, determine whether the current thread information is consistent with the parent thread information.
[0087] S207, if consistent, then determine to reset the data source of the parent thread that was initially obtained.
[0088] For a clearer understanding of this invention, please refer to the following details. Figure 4 , Figure 4 A flowchart illustrating another data source switching method provided in this embodiment of the invention may specifically include:
[0089] Step 1: Request the Handler method of the service.
[0090] Step 2: Perform AOP aspect interception and switch the main thread's data source based on the data source information carried in the request parameters.
[0091] In this embodiment, when a request reaches the handler method of the service, AOP aspect interception is performed. The corresponding data source is switched based on the country code information (data source information) carried in the request parameters, and then the request logic is processed.
[0092] Step 3: Determine if there is an asynchronous task annotation.
[0093] Step 4: When a custom asynchronous task annotation exists.
[0094] Step 5: Perform AOP aspect interception annotations.
[0095] Step 6: The asynchronous task thread pool executes the Execute method to start an asynchronous task that inherits from the public thread pool.
[0096] In this embodiment, the Execute method of Handler typically refers to the task execution method associated with Handler.
[0097] In this embodiment, if an asynchronous task annotation is detected during the logic processing, the AOP aspect will intercept it, first obtain the data source identifier of the main thread (parent thread), and then start an asynchronous task through the thread pool: at this time, the data source and thread information of the parent thread are obtained first, and then the child task is set with the same data source.
[0098] Step 7: When there is no custom asynchronous task annotation, but there is a custom thread pool task, determine that the public thread pool is inherited, and start the asynchronous subtask using the custom thread pool task.
[0099] In this embodiment, there are two ways to start asynchronous subtasks: one is to start the asynchronous task by intercepting the asynchronous task annotation through AOP aspects; the other is to start an asynchronous task directly through a thread pool task. In this embodiment, the common thread pool serves as the pointcut for data source switching, ensuring a generalized design. The thread pool parameters are defined by the thread pool that inherits from this common thread pool, because different thread pool business scenarios have different parameter requirements.
[0100] Step 8: Obtain the data source and thread information of the main thread.
[0101] Step 9: Design the same data source for the child thread.
[0102] Step 10: Remove the data source after the child thread has finished processing.
[0103] In this embodiment, the data source must be removed after the sub-thread finishes executing the sub-task logic to prevent memory leaks.
[0104] Step 11: Determine whether the current thread information is consistent with the parent thread information.
[0105] Step 12: If they match, reset the data source of the original parent thread.
[0106] In this embodiment, after removing the subtask data source, it will determine whether the current thread information is consistent with the parent thread information. If they are consistent, the data source of the parent thread that was initially obtained needs to be reset.
[0107] This invention addresses this issue by defining an annotation at the code level and intercepting it in the AOP aspect during data source switching. It first obtains the country code of the main thread, then starts an asynchronous task via a thread pool or the annotation, switching the data source based on the obtained parent thread data source, ensuring data source consistency between the parent and child threads. A common e-commerce thread pool is defined, and all defined thread pools must inherit from this common thread pool. When the thread pool executes the `Execute` method to start a task, it first obtains the country code of the current main thread (parent thread), and switches the data source in the newly started task based on the obtained country code (i.e., the sub-task data source switching logic described above). The `completableFuture` (used to represent the result of asynchronous execution) multi-threaded task can be defined by defining a thread pool that inherits from this common thread pool, solving the problem of manual data source setting and premature data source removal leading to database cross-referencing. Building upon existing solutions, this invention adds the requirement to remove the data source after the sub-task logic is completed, fundamentally resolving the issue of inconsistent data sources causing business data anomalies in other situations.
[0108] The following describes a data source switching device provided by an embodiment of the present invention. The data source switching device described below can be referred to in correspondence with the data source switching method described above.
[0109] Please refer to the details. Figure 5 , Figure 5 A schematic diagram of a data source switching device provided in an embodiment of the present invention may include:
[0110] The request execution logic and request parameter determination module 100 is used to map the current request to an asynchronous message processing method and use the asynchronous message method to determine the request execution logic and request parameters corresponding to the current request.
[0111] The main thread data source switching module 200 is used to switch the main thread's data source based on the data source information carried in the request parameters through a dynamic proxy.
[0112] The asynchronous subtask initiation module 300 is used to obtain the data source identifier of the main thread when it is determined that an asynchronous task annotation exists during the logic processing, and to start an asynchronous subtask through a thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic;
[0113] The asynchronous subtask data source setting module 400 is used to set the data source of the asynchronous subtask according to the data source identifier;
[0114] The data source removal module 500 is used to remove the data source when it is determined that the asynchronous subtask has been completed.
[0115] Furthermore, based on the above embodiments, the data source switching device may further include:
[0116] The initial thread pool custom module is used to customize the initial thread pool;
[0117] The module inherits the public thread pool of the e-commerce platform, which inherits the parameters of the initial thread pool from the parameters of the public thread pool of the e-commerce platform to obtain the thread pool; wherein, the public thread pool of the e-commerce platform includes data source switching logic.
[0118] Furthermore, based on any of the above embodiments, the data source switching device may further include:
[0119] The asynchronous thread information is consistent with the main thread information determination module, which is used to determine whether the asynchronous thread information corresponding to the asynchronous subtask is consistent with the main thread information;
[0120] The reset module is used to reset the data source of the main thread when consistency is achieved.
[0121] Furthermore, based on any of the above embodiments, the main thread data source switching module 200 may include:
[0122] The main thread data source switching unit is used to switch the main thread's data source based on the data source information carried in the request parameters using aspect-oriented programming.
[0123] Furthermore, based on any of the above embodiments, the data source switching device may further include:
[0124] The monitoring module is used to monitor the data source switching process and obtain data source switching logs;
[0125] The switching logic consistency judgment module is used to perform logical analysis based on the data source switching log when it is determined that the data source switching is completed, and to determine whether the switching logic is consistent with the preset switching logic of the data source.
[0126] The inconsistency determination module is used to determine whether to send a data source switching error message or return the state before the data source switching when inconsistency occurs.
[0127] Furthermore, based on any of the above embodiments, the above data source switching device may further include: a tag checking module, used to check the tag of the current thread;
[0128] The data source switching skip module is used to skip the switching operation if the flag indicates that the current thread has already switched the data source and the current data source has not changed.
[0129] The tag update module is used to perform a data source switching operation and update the tag if the tag indicates that the current thread has not switched the data source or the current data source is invalid.
[0130] Furthermore, based on any of the above embodiments, the above data source switching device may further include: a timed task determination module, used to determine whether a timed task exists in the data source switching aspect;
[0131] The site annotation interception module is used to determine the triggering of the scheduled task when it is determined that the scheduled task exists, and to intercept site annotations at the aspect where the data source is switched.
[0132] The target site specification module is used to determine whether the site annotation specifies a target site.
[0133] The All Sites module is used to determine the site to be processed when the site annotation does not specify the target site. It then determines the site to be processed as the default All Sites and processes the All Sites according to the business logic corresponding to each site in the All Sites module.
[0134] The target site, as a processing site determination module, is used to determine, when the site annotation specifies the target site, to process the target site according to the business logic corresponding to each site in the target site;
[0135] The processed site information storage module is used to store the processed site information into the corresponding data source.
[0136] It should be noted that the order of the modules and units in the aforementioned data source switching device can be changed without affecting the logic.
[0137] The data source switching device provided in this embodiment of the invention may include: a request execution logic and request parameter determination module 100, used to map the current request to an asynchronous message processing method, and use the asynchronous message method to determine the request execution logic and request parameters corresponding to the current request; a main thread data source switching module 200, used to switch the main thread's data source according to the data source information carried by the request parameters through dynamic proxy; an asynchronous subtask starting module 300, used to obtain the main thread's data source identifier and start an asynchronous subtask through a thread pool when it is determined that an asynchronous task annotation exists during logic processing; wherein, the thread pool includes asynchronous subtask data source switching logic; an asynchronous subtask data source setting module 400, used to set the data source of the asynchronous subtask according to the data source identifier; and a data source removal module 500, used to remove the data source when it is determined that the asynchronous subtask has been completed. This embodiment adds subtask data source switching logic processing, first obtaining the parent thread's data source and thread information, then setting the same data source for the subtask, and removing the data source after executing the subtask logic, further preventing memory leaks. Furthermore, this embodiment switches the main thread's data source using aspect-oriented programming (AOP). AOP allows developers to extract common concerns into aspects, which can be reused across multiple modules, thus avoiding code redundancy. Additionally, by directly inheriting the parameters of the e-commerce platform's public thread pool from custom thread pools, this embodiment enables rapid design of thread pools capable of data source switching, improving the efficiency of thread pool switching. Moreover, this embodiment performs logical analysis based on data source switching logs to prevent switching logic errors that could lead to incorrect data source switching, thereby improving the reliability of data source switching. Finally, this embodiment determines whether a data source switch is necessary based on an indicator; if a data source has already been switched and the current data source... If no changes are made, the data source switching operation will be skipped directly to prevent repeated switching and improve the efficiency of data source switching. Furthermore, this embodiment considers that threads allocated by the thread pool may be reused; sometimes the thread allocated to a subtask may be the same as the parent thread. Removing the data source of a sub-thread will remove the data source of the parent thread, so it needs to be reset, thus improving the accuracy of data source settings. This embodiment defines a site annotation at the code level. This annotation can specify a site, or defaults to all sites. When the scheduled task is triggered, it intercepts this annotation at the data source switching aspect and processes the corresponding site information according to the site information specified in the annotation (or all sites if not specified). This solves the problem that the scheduled task can only process one site at a time, reducing code maintenance.
[0138] The following describes a data source switching device provided by an embodiment of the present invention. The data source switching device described below and the data source switching method described above can be referred to in correspondence.
[0139] Please refer to Figure 6 , Figure 6 A schematic diagram of a data source switching device provided in an embodiment of the present invention may include:
[0140] Memory 10 is used to store computer programs;
[0141] Processor 20 is used to execute computer programs to implement the steps of the above-described data source switching method.
[0142] The memory 10, processor 20, and communication interface 30 all communicate with each other through the communication bus 40.
[0143] In this embodiment of the invention, the memory 10 is used to store one or more programs. The programs may include program code, which includes computer operation instructions. In this embodiment of the invention, the memory 10 may store programs for implementing the following functions:
[0144] Map the current request to the asynchronous message handling method, and use the asynchronous message method to determine the request execution logic and request parameters corresponding to the current request;
[0145] The main thread's data source is switched using dynamic proxies based on the data source information carried in the request parameters.
[0146] During the logic processing, when it is determined that an asynchronous task annotation exists, the data source identifier of the main thread is obtained, and an asynchronous subtask is started through the thread pool; the thread pool includes the asynchronous subtask data source switching logic;
[0147] Set the data source for the asynchronous subtask based on the data source identifier;
[0148] Remove the data source once the asynchronous subtask has completed execution.
[0149] In one possible implementation, the memory 10 may include a program storage area and a data storage area, wherein the program storage area may store the operating system and applications required for at least one function; and the data storage area may store data created during use.
[0150] Furthermore, memory 10 may include read-only memory and random access memory, providing instructions and data to the processor. A portion of the memory may also include NVRAM. The memory stores operating systems and operating instructions, executable modules, or data structures, or subsets thereof, or extended sets thereof, wherein the operating instructions may include various operating instructions for implementing various operations. The operating system may include various system programs for implementing various basic tasks and handling hardware-based tasks.
[0151] Processor 20 can be a central processing unit (CPU), an application-specific integrated circuit, a digital signal processor, a field-programmable gate array, or other programmable logic device. Processor 20 can be a microprocessor or any conventional processor. Processor 20 can call programs stored in memory 10.
[0152] The communication interface 30 can be an interface for the communication module, used to connect with other devices or systems.
[0153] Of course, it should be noted that, Figure 6 The structure shown does not constitute a limitation on the data source switching device in the embodiments of the present invention. In practical applications, the data source switching device may include devices such as... Figure 6 More or fewer components as shown, or combinations of certain components.
[0154] The computer-readable storage medium provided in the embodiments of the present invention is described below. The computer-readable storage medium described below and the data source switching method described above can be referred to in correspondence.
[0155] The present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the above-described data source switching method.
[0156] The computer-readable storage medium may include various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0157] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.
[0158] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0159] Finally, it should be noted that in this document, relationships such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.
[0160] The present invention provides a detailed description of a data source switching method, apparatus, device, and computer-readable storage medium. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, those skilled in the art will recognize that, based on the ideas of the present invention, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A method for switching data sources, characterized in that, include: Map the current request to an asynchronous message processing method, and use the asynchronous message method to determine the request parameters corresponding to the current request; The main thread's data source is switched using dynamic proxies based on the data source information carried in the request parameters. During the logic processing, when it is determined that an asynchronous task annotation exists, the data source identifier of the main thread is obtained, and an asynchronous subtask is started through the thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic; Set the data source for the asynchronous subtask according to the data source identifier; Once the asynchronous subtask has been completed, the data source is removed.
2. The data source switching method according to claim 1, characterized in that, Before obtaining the main thread's data source identifier and starting an asynchronous subtask through the thread pool when an asynchronous task annotation is determined to exist during the logical processing, the following steps are also included: Custom initial thread pool; The initial thread pool inherits the parameters of the mall's public thread pool to obtain the thread pool; wherein, the mall's public thread pool includes data source switching logic.
3. The data source switching method according to claim 1, characterized in that, After removing the data source when it is determined that the asynchronous subtask has completed execution, the method further includes: Determine whether the asynchronous thread information corresponding to the asynchronous subtask is consistent with the main thread information; When they match, reset the data source for the main thread.
4. The data source switching method according to claim 1, characterized in that, The step of switching the main thread's data source based on the data source information carried in the request parameters using a dynamic proxy includes: The main thread's data source is switched based on the data source information carried in the request parameters using aspect-oriented programming.
5. The data source switching method according to any one of claims 1 to 4, characterized in that, The data source switching process also includes: Monitor the data source switching process and obtain data source switching logs; When it is determined that the data source switch is complete, logical analysis is performed based on the data source switch log to determine whether the switch logic is consistent with the preset switch logic of the data source. If there is a discrepancy, determine whether to send a data source switching error message or determine whether to return to the state before the data source switching.
6. The data source switching method according to claim 1, characterized in that, The process of switching the data source between the main thread and the asynchronous subtask also includes: Check the current thread's flag; If the flag indicates that the current thread has switched data sources and the current data source has not changed, then the switching operation is skipped; If the flag indicates that the current thread has not switched data sources, or that the current data source is invalid, then a data source switching operation is performed, and the flag is updated.
7. The data source switching method according to claim 1, characterized in that, After switching the main thread's data source based on the data source information carried in the request parameters using a dynamic proxy, the process further includes: Determine if a scheduled task exists in the aspect where the data source is switched; When the existence of the scheduled task is determined, the scheduled task is triggered, and the site annotation is intercepted in the aspect of data source switching; Determine whether the site annotation specifies a target site; When the site annotation does not specify the target site, the site to be processed is determined to be all sites by default, and all sites are processed according to the business logic corresponding to each site in the all sites. When the site annotation specifies the target site, it is determined that the target site will be processed according to the business logic corresponding to each site in the target site; The processed site information is stored in the corresponding data source.
8. A data source switching device, characterized in that, include: The request parameter determination module is used to map the current request to an asynchronous message processing method and use the asynchronous message method to determine the request parameters corresponding to the current request. The main thread data source switching module is used to switch the main thread's data source based on the data source information carried in the request parameters through a dynamic proxy. The asynchronous subtask initiation module is used to obtain the data source identifier of the main thread when it is determined that an asynchronous task annotation exists during the logic processing, and start an asynchronous subtask through a thread pool; wherein, the thread pool includes asynchronous subtask data source switching logic; An asynchronous subtask data source setting module is used to set the data source of the asynchronous subtask according to the data source identifier; The data source removal module is used to remove the data source when it is determined that the asynchronous subtask has been completed.
9. A data source switching device, characterized in that, include: Memory, used to store computer programs; A processor, configured to implement the steps of the data source switching method as described in any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the data source switching method as described in any one of claims 1 to 7.