CN-122019064-A - Fine granularity transaction control method, system, device, electronic equipment and storage medium
Abstract
The application discloses a fine-grained transaction control method, a system, a device, electronic equipment and a storage medium, which belong to the field of computer software, wherein the method comprises the steps of receiving a transaction control request through a static call interface, routing the transaction control request to a corresponding transaction template method, binding preset transaction attributes through declarative configuration by the transaction template method, fixing logic of a method body to execute a functional interface object transmitted through parameters, and calling the transaction template method to enable a business logic code block to be executed in a transaction context defined by the transaction attributes. The developer can independently and flexibly start the transaction with specific attribute aiming at different logic fragments in the same method, and the defects of code redundancy, strong invasiveness and insufficient intuitionistic unification of API in the prior art are overcome.
Inventors
- WU ZEHUA
- ZHANG HONGWEI
- GAN YUANXING
- LEI BO
- ZHU GUANGHUI
Assignees
- 杉数科技(北京)有限公司
- 上海杉数网络科技有限公司
- 北京杉数智能科技有限公司
- 广州杉数科技有限公司
Dates
- Publication Date
- 20260512
- Application Date
- 20260203
Claims (10)
- 1. A fine-grained transaction control method, comprising: Receiving a transaction control request through a static call interface, wherein the request comprises a business logic code block and a transaction attribute parameter; routing the transaction control request to a corresponding transaction template method, wherein the transaction template method is bound with preset transaction attributes through declarative configuration, and logic of a method body is fixed to execute a functional interface object transmitted through parameters; the transaction template method is invoked such that the business logic code blocks execute within a transaction context defined by the transaction attributes.
- 2. The method of claim 1, wherein the step of receiving the transaction control request through the static call interface comprises: Receiving a call request from a developer and input parameters, wherein the call request comprises a static tool class for requesting call, and the input parameters comprise the business logic code block and the transaction attribute parameters; Packaging the business logic code block into a functional interface object; Generating the transaction execution request based on the functional interface object and the transaction attribute parameter; And acquiring a static calling method according to the static tool class, and receiving the transaction control request through a static calling interface.
- 3. The method of claim 1, wherein the step of routing the transaction control request to a corresponding transaction template method comprises: Determining a transaction template method serving as a calling target according to the propagation behavior parameters in the transaction control request and preset routing logic; and taking the business logic code block as a parameter to be transmitted into the transaction template method.
- 4. The method of claim 1, wherein the step of routing the transaction control request to a corresponding transaction template method further comprises: The transaction execution request is routed to the corresponding transaction template method by pre-constructing a mapping relation table of the transaction control request and the transaction template method, or And calling a corresponding transaction template method by taking the transaction control request as a judging condition through presetting a set of condition judging statement.
- 5. The method of claim 1, wherein the step of invoking the transaction template method such that the business logic code block is executed in a transaction context defined by the transaction attributes comprises: Analyzing the transaction attribute from the transaction template method; and determining a transaction manager for executing the current transaction according to the transaction attribute, judging the current thread transaction state, and creating or adding a corresponding transaction context according to the judging result.
- 6. The method of claim 5, wherein the step of determining a transaction manager executing the current transaction based on the transaction attributes and determining the current thread transaction state thereof, and creating or joining the corresponding transaction context based on the determination result comprises: When the transaction attribute contains a new transaction, judging whether an active transaction exists in the current thread of the transaction manager, if so, suspending the current active transaction and creating a new transaction context, otherwise, directly creating the new transaction context; When the transaction attribute does not contain a new transaction, judging whether the current thread of the transaction manager has an active transaction, if so, adding the current active transaction, otherwise, creating a new transaction context.
- 7. A fine-grained transaction control system, comprising: the receiving module is used for receiving a transaction control request through a static call interface, wherein the request comprises a business logic code block and a transaction attribute parameter; The routing module is used for routing the transaction control request to a corresponding transaction template method, wherein the transaction template method is bound with preset transaction attributes through declarative configuration, and logic of a method body is fixed to execute a functional interface object which is transmitted through parameters; and the calling module is used for calling the transaction template method so that the business logic code block is executed in the transaction context defined by the transaction attribute.
- 8. A fine-grained transaction control device, comprising: a static tool class layer configured to provide a global static call interface for receiving business logic code blocks and transaction attribute parameters encapsulated in functional interface objects; The routing adaptation layer is in communication connection with the static tool class layer and is configured to receive the business logic code blocks and the transaction attribute parameters and route the execution request to the corresponding transaction template method according to the transaction attribute parameters; The agent execution layer is in communication connection with the route adaptation layer and comprises one or more transaction template methods, wherein each transaction template method is bound with a specific transaction attribute through declarative configuration, and a method body of the agent execution layer is configured to execute an incoming functional interface object; Wherein the static tool class layer, the route adaptation layer, and the proxy execution layer work cooperatively such that the business logic code blocks execute in a transaction context defined by the bound transaction attributes.
- 9. An electronic device comprising a processor and a memory, wherein the memory stores program code that, when executed by the processor, causes the processor to perform the method of any of claims 1-6.
- 10. A computer readable storage medium comprising program code for causing an electronic device to perform the method of any one of claims 1-6 when the storage medium is run on the electronic device.
Description
Fine granularity transaction control method, system, device, electronic equipment and storage medium Technical Field The present application relates to the field of computer software technologies, and in particular, to a fine-grained transaction control method, system, device, electronic apparatus, and storage medium. Background In the development of Java enterprise-level applications based on a Spring framework, transaction management is a core mechanism for ensuring the atomicity, consistency, isolation and durability (ACID) of database operations, and is important for ensuring the integrity and consistency of business data. The Spring framework provides a developer with powerful and abstract transaction control capability through two main posts of declarative transaction management and programming transaction management. However, both of these mainstream schemes have inherent drawbacks in achieving fine and flexible transaction control. Declarative transaction management allows developers to automatically manage the life cycle of transactions by a Spring framework at runtime through facet oriented programming (AOP) by adding @ Transactional comments to Spring managed beans (typically Service layer classes or methods), and configuring properties of propagation behavior, isolation level, etc., where @ Transactional comments can only work on the whole method level, if a transaction with a specific propagation behavior needs to be independently started on a specific code section (e.g., several lines of key code) inside the method, the developers are typically forced to split this code section into a separate new method and add comments, which severely destroys the natural structure, logical cohesiveness, and readability of the code, and programmatic transaction management needs to more finely control the context of the transaction boundary. The developer explicitly uses TransactionTemplate examples in a programming mode, and writes transaction logic in an execution method, and the mode needs to explicitly rely on and inject TransactionTemplate examples each time, and writes template codes of the execution method, so that the grammar is complicated and the code invasiveness is strong. Based on this, there is an urgent need in the art for a new transaction control scheme that can achieve code block level fine-grained control while taking into account the simplicity of declarative transactions and the flexibility of programmatic transactions. Disclosure of Invention The embodiment of the application provides a fine-granularity transaction control method, a system, a device, electronic equipment and a storage medium, which construct a fine-granularity transaction control architecture based on a functional interface and a static tool class, solve the contradiction between the control granularity and the flexibility of the traditional Spring transaction management scheme, and realize the technical effect of dynamically and flexibly controlling transaction boundaries and propagation behaviors at the code block level in a nearly declarative concise grammar. In one aspect, an embodiment of the present application provides a fine-grained transaction control method, including: Receiving a transaction control request through a static call interface, wherein the request comprises a business logic code block and a transaction attribute parameter; routing the transaction control request to a corresponding transaction template method, wherein the transaction template method is bound with preset transaction attributes through declarative configuration, and logic of a method body is fixed to execute a functional interface object transmitted through parameters; the transaction template method is invoked such that the business logic code blocks execute within a transaction context defined by the transaction attributes. In one possible embodiment, the step of receiving a transaction control request through a static call interface includes: Receiving a call request from a developer and input parameters, wherein the call request comprises a static tool class for requesting call, and the input parameters comprise the business logic code block and the transaction attribute parameters; Packaging the business logic code block into a functional interface object; Generating the transaction execution request based on the functional interface object and the transaction attribute parameter; And acquiring a static calling method according to the static tool class, and receiving the transaction control request through a static calling interface. In one possible embodiment, the step of routing the transaction control request to a corresponding transaction template method includes: Determining a transaction template method serving as a calling target according to the propagation behavior parameters in the transaction control request and preset routing logic; and taking the business logic code block as a parameter to be transmitted into the tra