事务嵌套的那些事儿
关于事务嵌套,以前了解一些,但总是属于模棱两可,平时处理这种问题时候也是依照前人的经验,但至于为什么这么做,还真是知其然不知其所以然。(博客园的代码展开为什么不能展开呢?还请各位大侠指点!) 今天一个同事问我关于事务的问题,我就用代码给他举
关于事务嵌套,以前了解一些,但总是属于模棱两可,平时处理这种问题时候也是依照前人的经验,但至于为什么这么做,网站空间,还真是“知其然不知其所以然”。(博客园的代码展开为什么不能展开呢?还请各位大侠指点!)
今天一个同事问我关于事务的问题,我就用代码给他举例测试,在测试的过程中我遇到了一点小问题,但在继续测试的时候,我解决了这个问题,也让我对事务的嵌套有了更加深刻的认识。
废话不再多说了,开始正题。
本文的目的是跟大家讨论一下关于嵌套事务的相关问题,美国服务器,所以有关事务的基础知识和概念,本文假设读者已经了解。
嵌套事务一般的使用场景是一些公用的,虚拟主机,最小单元的业务逻辑,这些业务逻辑很多情况下都是被另外一些更加复杂,更加完整的业务逻辑调用。
为了更加贴近实际,本文的例子尽量接近真实业务,在此我们拿一个电子商务网站的订单支付来进行举例,具体例子如下
提交订单之后,支付订单(扣除账户余额)并更新订单的状态。
根据业务,我们创建三个表
会员表
账户变动记录表
订单表
建表语句如下:
CREATE TABLE T_Users( Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,--自增编号 UserName NVARCHAR(50) NOT NULL,--用户名,保持唯一 UserMoney DECIMAL(9,2) NOT NULL DEFAULT 0--用户账户余额,不能小于 ) CREATE TABLE T_MoneyLog( Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,--自增编号 UserName NVARCHAR(50) NOT NULL, --用户名 ChangeType INT NOT NULL,--账户变动类型(1支付订单,发送短信,提交参会申请) MoneyBefore DECIMAL(9,2) NOT NULL DEFAULT 0, --会员账户变动前余额 ChangeMoney DECIMAL(9,2) NOT NULL DEFAULT 0, --变动的金额 MoneyAfter DECIMAL(9,2) NOT NULL DEFAULT 0, --会员账户变动后余额 Remark NVARCHAR(100), --账户变动备注 AddTime DATETIME NOT NULL DEFAULT GETDATE() --变动时间 ) CREATE TABLE T_Order( Id INT IDENTITY(1,1) NOT NULL, --自增编号 OrderId VARCHAR(20) NOT NULL PRIMARY KEY,--订单号 SumMoney DECIMAL(9,2) NOT NULL DEFAULT 0,--订单总共需要支付费用 OrderStatus INT NOT NULL DEFAULT 0,--订单状态(未支付,已支付) AddTime DATETIME NOT NULL DEFAULT GETDATE(),--订单提交时间 PayTime DATETIME NULL--订单支付时间 )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Nested Generic Functions Generic functions in Go 1.18 allow the creation of functions that apply to multiple types, and nested generic functions can create reusable code hierarchies: Generic functions can be nested within each other, creating a nested code reuse structure. By composing filters and mapping functions into a pipeline, you can create reusable type-safe pipelines. Nested generic functions provide a powerful tool for creating reusable, type-safe code, making your code more efficient and maintainable.

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

How to use Vue form processing to implement recursive nesting of forms Introduction: As the complexity of front-end data processing and form processing continues to increase, we need a flexible way to handle complex forms. As a popular JavaScript framework, Vue provides us with many powerful tools and features to handle recursive nesting of forms. This article will introduce how to use Vue to handle such complex forms, and attach code examples. 1. Recursive nesting of forms In some scenarios, we may need to deal with recursive nesting.

Nested exception handling is implemented in C++ through nested try-catch blocks, allowing new exceptions to be raised within the exception handler. The nested try-catch steps are as follows: 1. The outer try-catch block handles all exceptions, including those thrown by the inner exception handler. 2. The inner try-catch block handles specific types of exceptions, and if an out-of-scope exception occurs, control is given to the external exception handler.

The PHP Data Objects (PDO) extension provides efficient and object-oriented interaction with database servers. Its advanced query and update capabilities enable developers to perform complex database operations, improving performance and code maintainability. This article will delve into the advanced query and update functions of PDO and guide you to master its powerful functions. Advanced queries: Using placeholders and bound parameters Placeholders and bound parameters are important tools for improving query performance and security. Placeholders use question marks (?) to represent replaceable parameters in the query, while bind parameters allow you to specify the data type and value of each parameter. By using these methods, you can avoid SQL injection attacks and improve performance because the database engine can optimize queries ahead of time. //Use placeholder $stmt=$

Tables are a fundamental and crucial aspect of web development and are used to present information in an orderly and clear format. However, there may be situations where more complex data needs to be presented, requiring the use of nested tables. Nested tables are tables located within other table cells. In this article, we'll walk you through the process of building nested tables in HTML, with meticulously detailed explanations complete with illustrations to help you understand the concepts more effectively. Whether you are a newbie or an experienced web designer, this article will provide you with the knowledge and expertise you need to become proficient in creating nested tables using HTML. Before we start exploring making nested tables, it's necessary to understand the basic makeup of HTML tables. HTML tables are implemented through the <table> element.

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq
